X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FScriptRunner.pm;h=7619e615823da5c5ca606e7b679560dfc8a1eeb3;hb=ca5d34dfc295012a749a8454d2b213580e03d55c;hp=ee2b1c13f74595b267784ae2de3fa44aea364c0c;hpb=bb48c5566ee0170fd913574d2393f381d214f86c;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/ScriptRunner.pm b/lib/Catalyst/ScriptRunner.pm index ee2b1c1..7619e61 100644 --- a/lib/Catalyst/ScriptRunner.pm +++ b/lib/Catalyst/ScriptRunner.pm @@ -3,24 +3,52 @@ use Moose; use FindBin; use lib; use File::Spec; -use namespace::autoclean; +use Class::Load qw/ load_first_existing_class load_optional_class /; +use namespace::autoclean -also => 'subclass_with_traits'; +use Try::Tiny; + +sub find_script_class { + my ($self, $app, $script) = @_; + return load_first_existing_class("${app}::Script::${script}", "Catalyst::Script::$script"); +} + +sub find_script_traits { + my ($self, @try) = @_; + + return grep { load_optional_class($_) } @try; +} + +sub subclass_with_traits { + my ($base, @traits) = @_; + + my $meta = Class::MOP::class_of($base)->create_anon_class( + superclasses => [ $base ], + roles => [ @traits ], + cache => 1, + ); + $meta->add_method(meta => sub { $meta }); + + return $meta->name; +} sub run { - my ($self, $class, $scriptclass, %args) = @_; - my $classtoload = "${class}::Script::$scriptclass"; + my ($self, $appclass, $scriptclass) = @_; lib->import(File::Spec->catdir($FindBin::Bin, '..', 'lib')); - unless ( eval { Class::MOP::load_class($classtoload) } ) { - warn("Could not load $classtoload - falling back to Catalyst::Script::$scriptclass : $@\n") - if $@ !~ /Can't locate/; - $classtoload = "Catalyst::Script::$scriptclass"; - Class::MOP::load_class($classtoload); - } - $classtoload->new_with_options( application_name => $class, %args )->run; + my $class = $self->find_script_class($appclass, $scriptclass); + + my @possible_traits = ("${appclass}::TraitFor::Script::${scriptclass}", "${appclass}::TraitFor::Script"); + my @traits = $self->find_script_traits(@possible_traits); + + $class = subclass_with_traits($class, @traits) + if @traits; + + $class->new_with_options( application_name => $appclass )->run; } __PACKAGE__->meta->make_immutable; +1; =head1 NAME @@ -34,8 +62,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 @@ -44,6 +87,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