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