6bea8c1a0d35c2058a674a8d7bcfe5f2a8de5489
[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::Engine::Loader;
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 help => (
26     traits        => ['Getopt'],
27     isa           => Bool,
28     is            => 'ro',
29     documentation => 'Display this help and exit',
30     cmd_aliases   => ['?', 'h'],
31 );
32
33 has loader_class => (
34     isa => LoadableClass,
35     is => 'ro',
36     coerce => 1,
37     default => 'Catalyst::Engine::Loader',
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         shift->loader_class->new
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 sub _getopt_full_usage {
61     my $self = shift;
62     pod2usage();
63     exit 0;
64 }
65
66 before run => sub {
67     my $self = shift;
68     $self->_getopt_full_usage if $self->help;
69 };
70
71 sub run {
72     my $self = shift;
73     $self->_run_application;
74 }
75
76 sub _application_args {
77     ()
78 }
79
80 sub _plack_loader_args {
81     my @app_args = shift->_application_args;
82     return (port => $app_args[0]);
83 }
84
85 sub _run_application {
86     my $self = shift;
87     my $app = $self->application_name;
88     Class::MOP::load_class($app);
89     my $server;
90     if (my $e = $self->can('_plack_engine_name') ) {
91         $server = $self->load_engine($self->$e, $self->_plack_loader_args);
92     }
93     else {
94         $server = $self->autoload_engine($self->_plack_loader_args);
95     }
96     $app->run($self->_application_args, $server);
97 }
98
99 1;
100
101 =head1 NAME
102
103 Catalyst::ScriptRole - Common functionality for Catalyst scripts.
104
105 =head1 SYNOPSIS
106
107     package MyApp::Script::Foo;
108     use Moose;
109     use namespace::autoclean;
110
111     with 'Catalyst::ScriptRole';
112
113     sub _application_args { ... }
114
115 =head1 DESCRIPTION
116
117 Role with the common functionality of Catalyst scripts.
118
119 =head1 METHODS
120
121 =head2 run
122
123 The method invoked to run the application.
124
125 =head1 ATTRIBUTES
126
127 =head2 application_name
128
129 The name of the application class, e.g. MyApp
130
131 =head1 SEE ALSO
132
133 L<Catalyst>
134
135 L<MooseX::Getopt>
136
137 =head1 AUTHORS
138
139 Catalyst Contributors, see Catalyst.pm
140
141 =head1 COPYRIGHT
142
143 This library is free software, you can redistribute it and/or modify
144 it under the same terms as Perl itself.
145
146 =cut