1 package Catalyst::ScriptRunner;
6 use namespace::autoclean -also => 'subclass_with_traits';
9 sub find_script_class {
10 my ($self, $app, $script) = @_;
11 my $class = "${app}::Script::${script}";
14 Class::MOP::load_class($class);
17 confess $_ unless /Can't locate/;
18 $class = "Catalyst::Script::$script";
21 Class::MOP::load_class($class);
25 sub find_script_traits {
26 my ($self, @try) = @_;
31 Class::MOP::load_class($try);
35 confess $_ unless /^Can't locate/;
42 sub subclass_with_traits {
43 my ($base, @traits) = @_;
45 my $meta = Class::MOP::class_of($base)->create_anon_class(
46 superclasses => [ $base ],
50 $meta->add_method(meta => sub { $meta });
56 my ($self, $appclass, $scriptclass) = @_;
58 lib->import(File::Spec->catdir($FindBin::Bin, '..', 'lib'));
60 my $class = $self->find_script_class($appclass, $scriptclass);
62 my @possible_traits = ("${appclass}::TraitFor::Script::${scriptclass}", "${appclass}::TraitFor::Script");
63 my @traits = $self->find_script_traits(@possible_traits);
65 $class = subclass_with_traits($class, @traits)
68 $class->new_with_options( application_name => $appclass )->run;
71 __PACKAGE__->meta->make_immutable;
75 Catalyst::ScriptRunner - The Catalyst Framework script runner
79 # Will run MyApp::Script::Server if it exists, otherwise
80 # will run Catalyst::Script::Server.
81 Catalyst::ScriptRunner->run('MyApp', 'Server');
85 This class is responsible for running scripts, either in the application specific namespace
86 (e.g. C<MyApp::Script::Server>), or the Catalyst namespace (e.g. C<Catalyst::Script::Server>)
90 =head2 run ($application_class, $scriptclass)
92 Called with two parameters, the application class (e.g. MyApp)
93 and the script class, (i.e. one of Server/FastCGI/CGI/Create/Test)
97 Catalyst Contributors, see Catalyst.pm
101 This library is free software. You can redistribute it and/or modify it under
102 the same terms as Perl itself.