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