Implement automatic role loading for script classes.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRunner.pm
CommitLineData
291722a8 1package Catalyst::ScriptRunner;
2use Moose;
12a3e3b5 3use FindBin;
4use lib;
5use File::Spec;
7f2e015b 6use namespace::autoclean -also => 'subclass_with_traits';
7use Try::Tiny;
8
9sub find_script_class {
10 my ($self, $app, $script) = @_;
11 my $class = "${app}::Script::${script}";
12
13 try {
14 Class::MOP::load_class($class);
15 }
16 catch {
17 confess $_ unless /Can't locate/;
18 $class = "Catalyst::Script::$script";
19 };
20
21 Class::MOP::load_class($class);
22 return $class;
23}
24
25sub find_script_traits {
26 my ($self, @try) = @_;
27
28 my @traits;
29 for my $try (@try) {
30 try {
31 Class::MOP::load_class($try);
32 push @traits, $try;
33 }
34 catch {
35 confess $_ unless /^Can't locate/;
36 };
37 }
38
39 return @traits;
40}
41
42sub subclass_with_traits {
43 my ($base, @traits) = @_;
44
45 my $meta = Class::MOP::class_of($base)->create_anon_class(
46 superclasses => [ $base ],
47 roles => [ @traits ],
48 cache => 1,
49 );
50 $meta->add_method(meta => sub { $meta });
51
52 return $meta->name;
53}
291722a8 54
cc999ce2 55sub run {
7f2e015b 56 my ($self, $appclass, $scriptclass) = @_;
c1c59374 57
12a3e3b5 58 lib->import(File::Spec->catdir($FindBin::Bin, '..', 'lib'));
59
7f2e015b 60 my $class = $self->find_script_class($appclass, $scriptclass);
61
62 my @possible_traits = ("${appclass}::TraitFor::Script::${scriptclass}", "${appclass}::TraitFor::Script");
63 my @traits = $self->find_script_traits(@possible_traits);
64
65 $class = subclass_with_traits($class, @traits)
66 if @traits;
67
68 $class->new_with_options( application_name => $appclass )->run;
cc999ce2 69}
d3082fac 70
71__PACKAGE__->meta->make_immutable;
72
73=head1 NAME
74
1628b022 75Catalyst::ScriptRunner - The Catalyst Framework script runner
d3082fac 76
77=head1 SYNOPSIS
78
1628b022 79 # Will run MyApp::Script::Server if it exists, otherwise
80 # will run Catalyst::Script::Server.
81 Catalyst::ScriptRunner->run('MyApp', 'Server');
d3082fac 82
83=head1 DESCRIPTION
84
85This 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>)
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
d3082fac 95=head1 AUTHORS
96
97Catalyst Contributors, see Catalyst.pm
98
99=head1 COPYRIGHT
100
101This library is free software. You can redistribute it and/or modify it under
102the same terms as Perl itself.
103
104=cut