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