X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FUtils.pm;h=ac20a70e2e89ec924e159ff40235b35be4c0def1;hb=6bfff75e6a0a42fa27a73ee23dbdf2e92c97caad;hp=c76a247e09b7766c3be98811220003757fd6ef45;hpb=41ca9ba7190b75d9ee3adebf37188309cad986a4;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Utils.pm b/lib/Catalyst/Utils.pm index c76a247..ac20a70 100644 --- a/lib/Catalyst/Utils.pm +++ b/lib/Catalyst/Utils.pm @@ -1,12 +1,12 @@ package Catalyst::Utils; use strict; -use attributes (); use Catalyst::Exception; use File::Spec; use HTTP::Request; use Path::Class; use URI; +use Class::Inspector; =head1 NAME @@ -20,11 +20,9 @@ See L. =head1 METHODS -=over 4 +=head2 appprefix($class) -=item appprefix($class) - -Returns the application prefix for the class + MyApp::Foo becomes myapp_foo =cut @@ -35,17 +33,7 @@ sub appprefix { return $class; } -=item attrs($coderef) - -Returns attributes for coderef in a arrayref - -=cut - -sub attrs { attributes::get( $_[0] ) || [] } - -=item class2appclass($class); - -Returns the appclass for class. +=head2 class2appclass($class); MyApp::C::Foo::Bar becomes MyApp My::App::C::Foo::Bar becomes My::App @@ -61,9 +49,7 @@ sub class2appclass { return $appname; } -=item class2classprefix($class); - -Returns the classprefix for class. +=head2 class2classprefix($class); MyApp::C::Foo::Bar becomes MyApp::C My::App::C::Foo::Bar becomes My::App::C @@ -79,9 +65,7 @@ sub class2classprefix { return $prefix; } -=item class2classsuffix($class); - -Returns the classsuffix for class. +=head2 class2classsuffix($class); MyApp::C::Foo::Bar becomes C::Foo::Bar @@ -94,7 +78,7 @@ sub class2classsuffix { return $class; } -=item class2env($class); +=head2 class2env($class); Returns the environment name for class. @@ -109,11 +93,11 @@ sub class2env { return uc($class); } -=item class2prefix( $class, $case ); +=head2 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 @@ -128,9 +112,9 @@ sub class2prefix { return $prefix; } -=item class2tempdir( $class [, $create ] ); +=head2 class2tempdir( $class [, $create ] ); -Returns a tempdir for class. If create is true it will try to create the path. +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 @@ -157,7 +141,7 @@ sub class2tempdir { return $tmpdir->stringify; } -=item home($class) +=head2 home($class) Returns home directory for given class. @@ -190,11 +174,11 @@ sub home { return $home; } -=item prefix($class, $name); +=head2 prefix($class, $name); Returns a prefixed action. - MyApp::C::Foo::Bar, yada becomes /foo/bar/yada + MyApp::C::Foo::Bar, yada becomes foo/bar/yada =cut @@ -205,30 +189,59 @@ sub prefix { return $name; } -=item reflect_actions($class); +=head2 request($uri) + +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; + } + else { + $request = URI->new( 'http://localhost' . $request )->canonical; + } + } + unless ( ref $request eq 'HTTP::Request' ) { + $request = HTTP::Request->new( 'GET', $request ); + } + return $request; +} + +=head2 ensure_class_loaded($class_name) -Returns an arrayref containing all actions of a component class. +Loads the class unless it already has been loaded. =cut -sub reflect_actions { - my $class = shift; - my $actions = []; - eval '$actions = $class->_action_cache'; +sub ensure_class_loaded { + my $class = shift; - if ($@) { - Catalyst::Exception->throw( message => - qq/Couldn't reflect actions of component "$class", "$@"/ ); + return if Class::Inspector->loaded( $class ); # if a symbol entry exists we don't load again + + # this hack is so we don't overwrite $@ if the load did not generate an error + my $error; + { + local $@; + eval "require $class"; + $error = $@; } - return $actions; + die $error if $error; + die "require $class was successful but the package is not defined" + unless Class::Inspector->loaded($class); + + return 1; } -=back =head1 AUTHOR Sebastian Riedel, C +Yuval Kogman, C =head1 COPYRIGHT