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