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