Changelog updates
[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 {
acbecf08 36 my $self = shift;
37 $self->loader_class->new(application_name => $self->application_name);
532f0516 38 },
39 handles => {
40 load_engine => 'load',
41 autoload_engine => 'auto',
42 },
43 lazy => 1,
44);
45
19529022 46sub _getopt_spec_exception {}
47
48sub _getopt_spec_warnings {
49 shift;
50 warn @_;
51}
52
2b8d5598 53sub _getopt_full_usage {
d3082fac 54 my $self = shift;
55 pod2usage();
56 exit 0;
57}
58
d3082fac 59sub run {
60 my $self = shift;
61 $self->_run_application;
62}
63
64sub _application_args {
65 ()
66}
67
c821df21 68sub _plack_loader_args {
acbecf08 69 my $self = shift;
70 my @app_args = $self->_application_args;
c821df21 71 return (port => $app_args[0]);
72}
73
d3082fac 74sub _run_application {
75 my $self = shift;
76 my $app = $self->application_name;
77 Class::MOP::load_class($app);
c821df21 78 my $server;
79 if (my $e = $self->can('_plack_engine_name') ) {
532f0516 80 $server = $self->load_engine($self->$e, $self->_plack_loader_args);
c821df21 81 }
82 else {
532f0516 83 $server = $self->autoload_engine($self->_plack_loader_args);
c821df21 84 }
4b0f97fc 85 $app->run($self->_application_args, $server);
d3082fac 86}
87
881;
1628b022 89
90=head1 NAME
91
92Catalyst::ScriptRole - Common functionality for Catalyst scripts.
93
94=head1 SYNOPSIS
95
12aa6ca4 96 package MyApp::Script::Foo;
97 use Moose;
98 use namespace::autoclean;
111d3c9a 99
2c298960 100 with 'Catalyst::ScriptRole';
111d3c9a 101
ad8b4c91 102 sub _application_args { ... }
111d3c9a 103
1628b022 104=head1 DESCRIPTION
105
12aa6ca4 106Role with the common functionality of Catalyst scripts.
107
108=head1 METHODS
109
110=head2 run
111
112The method invoked to run the application.
113
114=head1 ATTRIBUTES
115
116=head2 application_name
117
118The name of the application class, e.g. MyApp
119
120=head1 SEE ALSO
121
122L<Catalyst>
123
124L<MooseX::Getopt>
1628b022 125
126=head1 AUTHORS
127
128Catalyst Contributors, see Catalyst.pm
129
130=head1 COPYRIGHT
131
132This library is free software, you can redistribute it and/or modify
133it under the same terms as Perl itself.
134
135=cut