X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FScriptRunner.pm;h=7b803771e339b7623e069adb819db4e41539e105;hp=26ca0fdbfa9dd0a0b2f315793d53bf92494b5092;hb=15c40696c8e1e840553573af61037032cc16768d;hpb=7f2e015bc9ef152ad836524da16299bd86a5a22b diff --git a/lib/Catalyst/ScriptRunner.pm b/lib/Catalyst/ScriptRunner.pm index 26ca0fd..7b80377 100644 --- a/lib/Catalyst/ScriptRunner.pm +++ b/lib/Catalyst/ScriptRunner.pm @@ -3,40 +3,20 @@ use Moose; use FindBin; use lib; use File::Spec; +use Class::Load qw/ load_first_existing_class load_optional_class /; +use Catalyst::Utils; use namespace::autoclean -also => 'subclass_with_traits'; use Try::Tiny; sub find_script_class { my ($self, $app, $script) = @_; - my $class = "${app}::Script::${script}"; - - try { - Class::MOP::load_class($class); - } - catch { - confess $_ unless /Can't locate/; - $class = "Catalyst::Script::$script"; - }; - - Class::MOP::load_class($class); - return $class; + return load_first_existing_class("${app}::Script::${script}", "Catalyst::Script::$script"); } sub find_script_traits { my ($self, @try) = @_; - my @traits; - for my $try (@try) { - try { - Class::MOP::load_class($try); - push @traits, $try; - } - catch { - confess $_ unless /^Can't locate/; - }; - } - - return @traits; + return grep { load_optional_class($_) } @try; } sub subclass_with_traits { @@ -55,7 +35,9 @@ sub subclass_with_traits { sub run { my ($self, $appclass, $scriptclass) = @_; - lib->import(File::Spec->catdir($FindBin::Bin, '..', 'lib')); + if (my $home = Catalyst::Utils::find_home_unloaded_in_checkout()) { + lib->import(File::Spec->catdir($home, 'lib')); + } my $class = $self->find_script_class($appclass, $scriptclass); @@ -69,6 +51,7 @@ sub run { } __PACKAGE__->meta->make_immutable; +1; =head1 NAME @@ -82,8 +65,23 @@ Catalyst::ScriptRunner - The Catalyst Framework script runner =head1 DESCRIPTION -This class is responsible for running scripts, either in the application specific namespace -(e.g. C), or the Catalyst namespace (e.g. C) +This class is responsible for loading and running scripts, either in the +application specific namespace +(e.g. C), or the Catalyst namespace (e.g. C). + +If your application contains a custom script, then it will be used in preference to the generic +script, and is expected to sub-class the standard script. + +=head1 TRAIT LOADING + +Catalyst will automatically load and apply roles to the scripts in your +application. + +C will be loaded if present, and will be applied to B +scripts. + +C will be loaded (if present) and for script +individually. =head1 METHODS @@ -92,6 +90,16 @@ This class is responsible for running scripts, either in the application specifi Called with two parameters, the application class (e.g. MyApp) and the script class, (i.e. one of Server/FastCGI/CGI/Create/Test) +=head2 find_script_class ($appname, $script_name) + +Finds and loads the class for the script, trying the application specific +script first, and falling back to the generic script. Returns the script +which was loaded. + +=head2 find_script_traits ($appname, @try) + +Finds and loads a set of traits. Returns the list of traits which were loaded. + =head1 AUTHORS Catalyst Contributors, see Catalyst.pm