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