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