X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FUtils.pm;h=c3145d3f8d03e5b726ef2e5cd8c99678ca73d166;hb=640faa87f0c572b58acd22124bfa6f6c59106873;hp=e329648d632882966bfee3479f0024e2a4ffa592;hpb=5d9a6d472d46896c9f878011e34485de7508c326;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Utils.pm b/lib/Catalyst/Utils.pm index e329648..c3145d3 100644 --- a/lib/Catalyst/Utils.pm +++ b/lib/Catalyst/Utils.pm @@ -1,8 +1,8 @@ package Catalyst::Utils; use strict; -use attributes (); use Catalyst::Exception; +use File::Spec; use HTTP::Request; use Path::Class; use URI; @@ -21,18 +21,21 @@ See L. =over 4 -=item attrs($coderef) +=item appprefix($class) -Returns attributes for coderef in a arrayref + MyApp::Foo becomes myapp_foo =cut -sub attrs { attributes::get( $_[0] ) || [] } +sub appprefix { + my $class = shift; + $class =~ s/\:\:/_/g; + $class = lc($class); + return $class; +} =item class2appclass($class); -Returns the appclass for class. - MyApp::C::Foo::Bar becomes MyApp My::App::C::Foo::Bar becomes My::App @@ -49,8 +52,6 @@ sub class2appclass { =item class2classprefix($class); -Returns the classprefix for class. - MyApp::C::Foo::Bar becomes MyApp::C My::App::C::Foo::Bar becomes My::App::C @@ -67,8 +68,6 @@ sub class2classprefix { =item class2classsuffix($class); -Returns the classsuffix for class. - MyApp::C::Foo::Bar becomes C::Foo::Bar =cut @@ -82,7 +81,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 @@ -97,9 +96,9 @@ sub class2env { =item class2prefix( $class, $case ); -Returns the prefix for class. +Returns the uri prefix for a class. If case is false the prefix is converted to lowercase. - My::App::C::Foo::Bar becomes /foo/bar + My::App::C::Foo::Bar becomes foo/bar =cut @@ -114,6 +113,35 @@ sub class2prefix { return $prefix; } +=item class2tempdir( $class [, $create ] ); + +Returns a tempdir for a 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 +164,7 @@ sub home { { $home = $subdir; } + # clean up relative path: # MyApp/script/.. -> MyApp my ($lastdir) = $home->dir_list( -1, 1 ); @@ -150,7 +179,7 @@ sub home { Returns a prefixed action. - MyApp::C::Foo::Bar, yada becomes /foo/bar/yada + MyApp::C::Foo::Bar, yada becomes foo/bar/yada =cut @@ -161,37 +190,15 @@ sub prefix { return $name; } -=item reflect_actions($class); - -Returns an arrayref containing all actions of a component class. - -=cut - -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", "$@"/ - ); - } - - return $actions; -} - -=item request($string); +=item request($uri) -Returns an C from a string. +Returns an L object for a uri. =cut sub request { my $request = shift; - unless ( ref $request ) { - if ( $request =~ m/http/i ) { $request = URI->new($request)->canonical; } @@ -199,11 +206,9 @@ sub request { $request = URI->new( 'http://localhost' . $request )->canonical; } } - unless ( ref $request eq 'HTTP::Request' ) { $request = HTTP::Request->new( 'GET', $request ); } - return $request; }