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