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