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