1 package Catalyst::ScriptRunner;
6 use Class::Load qw/ load_first_existing_class load_optional_class /;
7 use namespace::autoclean -also => 'subclass_with_traits';
10 sub find_script_class {
11 my ($self, $app, $script) = @_;
12 return load_first_existing_class("${app}::Script::${script}", "Catalyst::Script::$script");
15 sub find_script_traits {
16 my ($self, @try) = @_;
18 return grep { load_optional_class($_) } @try;
21 sub subclass_with_traits {
22 my ($base, @traits) = @_;
24 my $meta = Class::MOP::class_of($base)->create_anon_class(
25 superclasses => [ $base ],
29 $meta->add_method(meta => sub { $meta });
35 my ($self, $appclass, $scriptclass) = @_;
37 lib->import(File::Spec->catdir($FindBin::Bin, '..', 'lib'));
39 my $class = $self->find_script_class($appclass, $scriptclass);
41 my @possible_traits = ("${appclass}::TraitFor::Script::${scriptclass}", "${appclass}::TraitFor::Script");
42 my @traits = $self->find_script_traits(@possible_traits);
44 $class = subclass_with_traits($class, @traits)
47 $class->new_with_options( application_name => $appclass )->run;
50 __PACKAGE__->meta->make_immutable;
54 Catalyst::ScriptRunner - The Catalyst Framework script runner
58 # Will run MyApp::Script::Server if it exists, otherwise
59 # will run Catalyst::Script::Server.
60 Catalyst::ScriptRunner->run('MyApp', 'Server');
64 This class is responsible for running scripts, either in the application specific namespace
65 (e.g. C<MyApp::Script::Server>), or the Catalyst namespace (e.g. C<Catalyst::Script::Server>)
69 =head2 run ($application_class, $scriptclass)
71 Called with two parameters, the application class (e.g. MyApp)
72 and the script class, (i.e. one of Server/FastCGI/CGI/Create/Test)
76 Catalyst Contributors, see Catalyst.pm
80 This library is free software. You can redistribute it and/or modify it under
81 the same terms as Perl itself.