X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FUtils.pm;h=3a9a908a8df7ce163e3564cde34ca89de49a483d;hb=22f3a8dd32e5940d87a1d21642fa39c7813bc921;hp=98abdb1c1d6ecf4370e4c1452e4566e13adcc831;hpb=3ad654e0c538876403f3e664f0d9f302ef470770;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Utils.pm b/lib/Catalyst/Utils.pm index 98abdb1..3a9a908 100644 --- a/lib/Catalyst/Utils.pm +++ b/lib/Catalyst/Utils.pm @@ -3,6 +3,7 @@ package Catalyst::Utils; use strict; use attributes (); use Catalyst::Exception; +use File::Spec; use HTTP::Request; use Path::Class; use URI; @@ -21,6 +22,19 @@ See L. =over 4 +=item appprefix($class) + +Returns the application prefix for the class + +=cut + +sub appprefix { + my $class = shift; + $class =~ s/\:\:/_/g; + $class = lc($class); + return $class; +} + =item attrs($coderef) Returns attributes for coderef in a arrayref @@ -82,7 +96,7 @@ sub class2classsuffix { =item class2env($class); -Returns the enviroment name for class. +Returns the environment name for class. MyApp becomes MYAPP My::App becomes MY_APP @@ -91,7 +105,7 @@ Returns the enviroment name for class. sub class2env { my $class = shift || ''; - my $class =~ s/\:\:/_/g; + $class =~ s/\:\:/_/g; return uc($class); } @@ -114,6 +128,35 @@ sub class2prefix { return $prefix; } +=item class2tempdir( $class [, $create ] ); + +Returns a tempdir for class. If create is true it will try to create the path. + + My::App becomes /tmp/my/app + My::App::C::Foo::Bar becomes /tmp/my/app/c/foo/bar + +=cut + +sub class2tempdir { + my $class = shift || ''; + my $create = shift || 0; + my @parts = split '::', lc $class; + + my $tmpdir = dir( File::Spec->tmpdir, @parts )->cleanup; + + if ( $create && !-e $tmpdir ) { + + eval { $tmpdir->mkpath }; + + if ($@) { + Catalyst::Exception->throw( + message => qq/Couldn't create tmpdir '$tmpdir', "$@"/ ); + } + } + + return $tmpdir->stringify; +} + =item home($class) Returns home directory for given class. @@ -136,6 +179,7 @@ sub home { { $home = $subdir; } + # clean up relative path: # MyApp/script/.. -> MyApp my ($lastdir) = $home->dir_list( -1, 1 ); @@ -171,27 +215,24 @@ sub reflect_actions { my $class = shift; my $actions = []; eval '$actions = $class->_action_cache'; - - if ( $@ ) { - Catalyst::Exception->throw( - message => qq/Couldn't reflect actions of component "$class", "$@"/ - ); + + if ($@) { + Catalyst::Exception->throw( message => + qq/Couldn't reflect actions of component "$class", "$@"/ ); } - + return $actions; } -=item request($string); +=item request($request) -Returns an C from a string. +Returns a HTTP::Request object. =cut sub request { my $request = shift; - unless ( ref $request ) { - if ( $request =~ m/http/i ) { $request = URI->new($request)->canonical; } @@ -199,11 +240,9 @@ sub request { $request = URI->new( 'http://localhost' . $request )->canonical; } } - unless ( ref $request eq 'HTTP::Request' ) { $request = HTTP::Request->new( 'GET', $request ); } - return $request; }