Commit | Line | Data |
d3082fac |
1 | package Catalyst::ScriptRole; |
2 | use Moose::Role; |
d3082fac |
3 | use Pod::Usage; |
0e4038c6 |
4 | use MooseX::Getopt; |
b1ededd4 |
5 | use Catalyst::EngineLoader; |
8c848c33 |
6 | use Moose::Util::TypeConstraints; |
7 | use Catalyst::Utils qw/ ensure_class_loaded /; |
d3082fac |
8 | use namespace::autoclean; |
9 | |
8c848c33 |
10 | subtype 'Catalyst::ScriptRole::LoadableClass', |
11 | as 'ClassName'; |
12 | coerce 'Catalyst::ScriptRole::LoadableClass', |
13 | from 'Str', |
14 | via { ensure_class_loaded($_); 1 }; |
15 | |
19529022 |
16 | with 'MooseX::Getopt' => { |
8ba29999 |
17 | -version => 0.48, |
9c74923d |
18 | -excludes => [qw/ |
19529022 |
19 | _getopt_spec_warnings |
20 | _getopt_spec_exception |
8ba29999 |
21 | print_usage_text |
19529022 |
22 | /], |
23 | }; |
d3082fac |
24 | |
25 | has application_name => ( |
ad8b4c91 |
26 | traits => ['NoGetopt'], |
8c848c33 |
27 | isa => 'Str', |
ad8b4c91 |
28 | is => 'ro', |
d3082fac |
29 | required => 1, |
30 | ); |
31 | |
532f0516 |
32 | has loader_class => ( |
8c848c33 |
33 | isa => 'Catalyst::ScriptRole::LoadableClass', |
532f0516 |
34 | is => 'ro', |
35 | coerce => 1, |
b1ededd4 |
36 | default => 'Catalyst::EngineLoader', |
532f0516 |
37 | documentation => 'The class to use to detect and load the PSGI engine', |
38 | ); |
39 | |
40 | has _loader => ( |
41 | isa => 'Plack::Loader', |
42 | default => sub { |
acbecf08 |
43 | my $self = shift; |
44 | $self->loader_class->new(application_name => $self->application_name); |
532f0516 |
45 | }, |
46 | handles => { |
47 | load_engine => 'load', |
48 | autoload_engine => 'auto', |
49 | }, |
50 | lazy => 1, |
51 | ); |
52 | |
19529022 |
53 | sub _getopt_spec_exception {} |
54 | |
55 | sub _getopt_spec_warnings { |
56 | shift; |
57 | warn @_; |
58 | } |
59 | |
8ba29999 |
60 | =for Pod::Coverage |
61 | print_usage_text |
62 | =cut |
63 | sub print_usage_text { |
d3082fac |
64 | my $self = shift; |
65 | pod2usage(); |
66 | exit 0; |
67 | } |
68 | |
d3082fac |
69 | sub run { |
70 | my $self = shift; |
71 | $self->_run_application; |
72 | } |
73 | |
74 | sub _application_args { |
aee7cdcc |
75 | my $self = shift; |
76 | return { |
77 | argv => $self->ARGV, |
78 | extra_argv => $self->extra_argv, |
79 | } |
d3082fac |
80 | } |
81 | |
c821df21 |
82 | sub _plack_loader_args { |
acbecf08 |
83 | my $self = shift; |
84 | my @app_args = $self->_application_args; |
c821df21 |
85 | return (port => $app_args[0]); |
86 | } |
87 | |
aee7cdcc |
88 | sub _plack_engine_name {} |
89 | |
d3082fac |
90 | sub _run_application { |
91 | my $self = shift; |
92 | my $app = $self->application_name; |
93 | Class::MOP::load_class($app); |
c821df21 |
94 | my $server; |
aee7cdcc |
95 | if (my $e = $self->_plack_engine_name ) { |
96 | $server = $self->load_engine($e, $self->_plack_loader_args); |
c821df21 |
97 | } |
98 | else { |
532f0516 |
99 | $server = $self->autoload_engine($self->_plack_loader_args); |
c821df21 |
100 | } |
4b0f97fc |
101 | $app->run($self->_application_args, $server); |
d3082fac |
102 | } |
103 | |
104 | 1; |
1628b022 |
105 | |
106 | =head1 NAME |
107 | |
108 | Catalyst::ScriptRole - Common functionality for Catalyst scripts. |
109 | |
110 | =head1 SYNOPSIS |
111 | |
12aa6ca4 |
112 | package MyApp::Script::Foo; |
113 | use Moose; |
114 | use namespace::autoclean; |
111d3c9a |
115 | |
2c298960 |
116 | with 'Catalyst::ScriptRole'; |
111d3c9a |
117 | |
ad8b4c91 |
118 | sub _application_args { ... } |
111d3c9a |
119 | |
1628b022 |
120 | =head1 DESCRIPTION |
121 | |
12aa6ca4 |
122 | Role with the common functionality of Catalyst scripts. |
123 | |
124 | =head1 METHODS |
125 | |
126 | =head2 run |
127 | |
128 | The method invoked to run the application. |
129 | |
130 | =head1 ATTRIBUTES |
131 | |
132 | =head2 application_name |
133 | |
134 | The name of the application class, e.g. MyApp |
135 | |
136 | =head1 SEE ALSO |
137 | |
138 | L<Catalyst> |
139 | |
140 | L<MooseX::Getopt> |
1628b022 |
141 | |
142 | =head1 AUTHORS |
143 | |
144 | Catalyst Contributors, see Catalyst.pm |
145 | |
146 | =head1 COPYRIGHT |
147 | |
148 | This library is free software, you can redistribute it and/or modify |
149 | it under the same terms as Perl itself. |
150 | |
151 | =cut |