Merge branch 'master' into psgi
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRole.pm
CommitLineData
d3082fac 1package Catalyst::ScriptRole;
2use Moose::Role;
3use MooseX::Types::Moose qw/Str Bool/;
4use Pod::Usage;
0e4038c6 5use MooseX::Getopt;
532f0516 6use Catalyst::Engine::Loader;
7use MooseX::Types::LoadableClass qw/LoadableClass/;
d3082fac 8use namespace::autoclean;
9
19529022 10with 'MooseX::Getopt' => {
9c74923d 11 -excludes => [qw/
19529022 12 _getopt_spec_warnings
13 _getopt_spec_exception
2b8d5598 14 _getopt_full_usage
19529022 15 /],
16};
d3082fac 17
18has application_name => (
ad8b4c91 19 traits => ['NoGetopt'],
20 isa => Str,
21 is => 'ro',
d3082fac 22 required => 1,
23);
24
532f0516 25has 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
33has _loader => (
34 isa => 'Plack::Loader',
35 default => sub {
36 shift->loader_class->new
37 },
38 handles => {
39 load_engine => 'load',
40 autoload_engine => 'auto',
41 },
42 lazy => 1,
43);
44
19529022 45sub _getopt_spec_exception {}
46
47sub _getopt_spec_warnings {
48 shift;
49 warn @_;
50}
51
2b8d5598 52sub _getopt_full_usage {
d3082fac 53 my $self = shift;
54 pod2usage();
55 exit 0;
56}
57
d3082fac 58sub run {
59 my $self = shift;
60 $self->_run_application;
61}
62
63sub _application_args {
64 ()
65}
66
c821df21 67sub _plack_loader_args {
68 my @app_args = shift->_application_args;
69 return (port => $app_args[0]);
70}
71
d3082fac 72sub _run_application {
73 my $self = shift;
74 my $app = $self->application_name;
75 Class::MOP::load_class($app);
c821df21 76 my $server;
77 if (my $e = $self->can('_plack_engine_name') ) {
532f0516 78 $server = $self->load_engine($self->$e, $self->_plack_loader_args);
c821df21 79 }
80 else {
532f0516 81 $server = $self->autoload_engine($self->_plack_loader_args);
c821df21 82 }
4b0f97fc 83 $app->run($self->_application_args, $server);
d3082fac 84}
85
861;
1628b022 87
88=head1 NAME
89
90Catalyst::ScriptRole - Common functionality for Catalyst scripts.
91
92=head1 SYNOPSIS
93
12aa6ca4 94 package MyApp::Script::Foo;
95 use Moose;
96 use namespace::autoclean;
111d3c9a 97
2c298960 98 with 'Catalyst::ScriptRole';
111d3c9a 99
ad8b4c91 100 sub _application_args { ... }
111d3c9a 101
1628b022 102=head1 DESCRIPTION
103
12aa6ca4 104Role with the common functionality of Catalyst scripts.
105
106=head1 METHODS
107
108=head2 run
109
110The method invoked to run the application.
111
112=head1 ATTRIBUTES
113
114=head2 application_name
115
116The name of the application class, e.g. MyApp
117
118=head1 SEE ALSO
119
120L<Catalyst>
121
122L<MooseX::Getopt>
1628b022 123
124=head1 AUTHORS
125
126Catalyst Contributors, see Catalyst.pm
127
128=head1 COPYRIGHT
129
130This library is free software, you can redistribute it and/or modify
131it under the same terms as Perl itself.
132
133=cut