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