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