Merge branch 'bugs/rt-75731'
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRunner.pm
CommitLineData
291722a8 1package Catalyst::ScriptRunner;
2use Moose;
12a3e3b5 3use FindBin;
4use lib;
5use File::Spec;
725afced 6use Class::Load qw/ load_first_existing_class load_optional_class /;
0607947f 7use Catalyst::Utils;
7f2e015b 8use Try::Tiny;
eefc03e1 9use namespace::clean -except => [ 'meta' ];
7f2e015b 10
11sub find_script_class {
12 my ($self, $app, $script) = @_;
725afced 13 return load_first_existing_class("${app}::Script::${script}", "Catalyst::Script::$script");
7f2e015b 14}
15
16sub find_script_traits {
17 my ($self, @try) = @_;
18
725afced 19 return grep { load_optional_class($_) } @try;
7f2e015b 20}
21
eefc03e1 22no namespace::clean;
7f2e015b 23sub subclass_with_traits {
24 my ($base, @traits) = @_;
25
26 my $meta = Class::MOP::class_of($base)->create_anon_class(
27 superclasses => [ $base ],
28 roles => [ @traits ],
29 cache => 1,
30 );
31 $meta->add_method(meta => sub { $meta });
32
33 return $meta->name;
34}
eefc03e1 35use namespace::clean;
291722a8 36
cc999ce2 37sub run {
7f2e015b 38 my ($self, $appclass, $scriptclass) = @_;
c1c59374 39
94d7a76e 40 if (grep { -f File::Spec->catfile($FindBin::Bin, '..', $_) } Catalyst::Utils::dist_indicator_file_list()) {
a8946dc8 41 lib->import(File::Spec->catdir($FindBin::Bin, '..', 'lib'));
0607947f 42 }
12a3e3b5 43
7f2e015b 44 my $class = $self->find_script_class($appclass, $scriptclass);
45
46 my @possible_traits = ("${appclass}::TraitFor::Script::${scriptclass}", "${appclass}::TraitFor::Script");
47 my @traits = $self->find_script_traits(@possible_traits);
48
49 $class = subclass_with_traits($class, @traits)
50 if @traits;
51
52 $class->new_with_options( application_name => $appclass )->run;
cc999ce2 53}
d3082fac 54
55__PACKAGE__->meta->make_immutable;
f4de8c99 561;
d3082fac 57
58=head1 NAME
59
1628b022 60Catalyst::ScriptRunner - The Catalyst Framework script runner
d3082fac 61
62=head1 SYNOPSIS
63
1628b022 64 # Will run MyApp::Script::Server if it exists, otherwise
65 # will run Catalyst::Script::Server.
66 Catalyst::ScriptRunner->run('MyApp', 'Server');
d3082fac 67
68=head1 DESCRIPTION
69
ff7f2049 70This class is responsible for loading and running scripts, either in the
71application specific namespace
383c5be6 72(e.g. C<MyApp::Script::Server>), or the Catalyst namespace (e.g. C<Catalyst::Script::Server>).
73
74If your application contains a custom script, then it will be used in preference to the generic
75script, and is expected to sub-class the standard script.
76
77=head1 TRAIT LOADING
78
ff7f2049 79Catalyst will automatically load and apply roles to the scripts in your
80application.
383c5be6 81
82C<MyApp::TraitFor::Script> will be loaded if present, and will be applied to B<ALL>
83scripts.
84
85C<MyApp::TraitFor::Script::XXXX> will be loaded (if present) and for script
86individually.
d3082fac 87
12aa6ca4 88=head1 METHODS
89
90=head2 run ($application_class, $scriptclass)
91
bb48c556 92Called with two parameters, the application class (e.g. MyApp)
12aa6ca4 93and the script class, (i.e. one of Server/FastCGI/CGI/Create/Test)
94
859ab869 95=head2 find_script_class ($appname, $script_name)
96
97Finds and loads the class for the script, trying the application specific
98script first, and falling back to the generic script. Returns the script
99which was loaded.
100
101=head2 find_script_traits ($appname, @try)
102
103Finds and loads a set of traits. Returns the list of traits which were loaded.
104
d3082fac 105=head1 AUTHORS
106
107Catalyst Contributors, see Catalyst.pm
108
109=head1 COPYRIGHT
110
111This library is free software. You can redistribute it and/or modify it under
112the same terms as Perl itself.
113
114=cut