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; |
c8e0714d |
7 | use Catalyst::Utils; |
eefc03e1 |
8 | use namespace::clean -except => [ 'meta' ]; |
d3082fac |
9 | |
8c848c33 |
10 | subtype 'Catalyst::ScriptRole::LoadableClass', |
11 | as 'ClassName'; |
12 | coerce 'Catalyst::ScriptRole::LoadableClass', |
13 | from 'Str', |
c8e0714d |
14 | via { Catalyst::Utils::ensure_class_loaded($_); $_ }; |
8c848c33 |
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 | sub print_usage_text { |
d3082fac |
61 | my $self = shift; |
62 | pod2usage(); |
63 | exit 0; |
64 | } |
65 | |
d3082fac |
66 | sub run { |
67 | my $self = shift; |
68 | $self->_run_application; |
69 | } |
70 | |
71 | sub _application_args { |
aee7cdcc |
72 | my $self = shift; |
73 | return { |
74 | argv => $self->ARGV, |
75 | extra_argv => $self->extra_argv, |
76 | } |
d3082fac |
77 | } |
78 | |
c821df21 |
79 | sub _plack_loader_args { |
acbecf08 |
80 | my $self = shift; |
81 | my @app_args = $self->_application_args; |
c821df21 |
82 | return (port => $app_args[0]); |
83 | } |
84 | |
aee7cdcc |
85 | sub _plack_engine_name {} |
86 | |
d3082fac |
87 | sub _run_application { |
88 | my $self = shift; |
89 | my $app = $self->application_name; |
c8e0714d |
90 | Catalyst::Utils::ensure_class_loaded($app); |
c821df21 |
91 | my $server; |
aee7cdcc |
92 | if (my $e = $self->_plack_engine_name ) { |
93 | $server = $self->load_engine($e, $self->_plack_loader_args); |
c821df21 |
94 | } |
95 | else { |
532f0516 |
96 | $server = $self->autoload_engine($self->_plack_loader_args); |
c821df21 |
97 | } |
4b0f97fc |
98 | $app->run($self->_application_args, $server); |
d3082fac |
99 | } |
100 | |
101 | 1; |
1628b022 |
102 | |
103 | =head1 NAME |
104 | |
105 | Catalyst::ScriptRole - Common functionality for Catalyst scripts. |
106 | |
107 | =head1 SYNOPSIS |
108 | |
12aa6ca4 |
109 | package MyApp::Script::Foo; |
110 | use Moose; |
111 | use namespace::autoclean; |
111d3c9a |
112 | |
2c298960 |
113 | with 'Catalyst::ScriptRole'; |
111d3c9a |
114 | |
ad8b4c91 |
115 | sub _application_args { ... } |
111d3c9a |
116 | |
1628b022 |
117 | =head1 DESCRIPTION |
118 | |
12aa6ca4 |
119 | Role with the common functionality of Catalyst scripts. |
120 | |
121 | =head1 METHODS |
122 | |
123 | =head2 run |
124 | |
125 | The method invoked to run the application. |
126 | |
d0a1ec36 |
127 | =head2 print_usage_text |
128 | |
d072f850 |
129 | Prints out the usage text for the script you tried to invoke. |
d0a1ec36 |
130 | |
12aa6ca4 |
131 | =head1 ATTRIBUTES |
132 | |
133 | =head2 application_name |
134 | |
135 | The name of the application class, e.g. MyApp |
136 | |
137 | =head1 SEE ALSO |
138 | |
139 | L<Catalyst> |
140 | |
141 | L<MooseX::Getopt> |
1628b022 |
142 | |
143 | =head1 AUTHORS |
144 | |
145 | Catalyst Contributors, see Catalyst.pm |
146 | |
147 | =head1 COPYRIGHT |
148 | |
149 | This library is free software, you can redistribute it and/or modify |
150 | it under the same terms as Perl itself. |
151 | |
152 | =cut |