Add a way to specify additional psgi values in the psgi env via Catalyst::Test.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRole.pm
1 package Catalyst::ScriptRole;
2 use Moose::Role;
3 use Plack::Runner;
4 use MooseX::Types::Moose qw/Str Bool/;
5 use Pod::Usage;
6 use MooseX::Getopt;
7 use namespace::autoclean;
8
9 with 'MooseX::Getopt' => {
10     excludes => [qw/
11         _getopt_spec_warnings
12         _getopt_spec_exception
13         _getopt_full_usage
14     /],
15 };
16
17 has application_name => (
18     traits   => ['NoGetopt'],
19     isa      => Str,
20     is       => 'ro',
21     required => 1,
22 );
23
24 has help => (
25     traits        => ['Getopt'],
26     isa           => Bool,
27     is            => 'ro',
28     documentation => 'Display this help and exit',
29     cmd_aliases   => ['?', 'h'],
30 );
31
32 sub _getopt_spec_exception {}
33
34 sub _getopt_spec_warnings {
35     shift;
36     warn @_;
37 }
38
39 sub _getopt_full_usage {
40     my $self = shift;
41     pod2usage();
42     exit 0;
43 }
44
45 before run => sub {
46     my $self = shift;
47     $self->_getopt_full_usage if $self->help;
48 };
49
50 sub run {
51     my $self = shift;
52     $self->_run_application;
53 }
54
55 sub _application_args {
56     ()
57 }
58
59 sub _run_application {
60     my $self = shift;
61     my $app = $self->application_name;
62     Class::MOP::load_class($app);
63     $app->run($self->_application_args);
64 }
65
66 1;
67
68 =head1 NAME
69
70 Catalyst::ScriptRole - Common functionality for Catalyst scripts.
71
72 =head1 SYNOPSIS
73
74     package MyApp::Script::Foo;
75     use Moose;
76     use namespace::autoclean;
77
78     with 'Catalyst::ScriptRole';
79
80     sub _application_args { ... }
81
82 =head1 DESCRIPTION
83
84 Role with the common functionality of Catalyst scripts.
85
86 =head1 METHODS
87
88 =head2 run
89
90 The method invoked to run the application.
91
92 =head1 ATTRIBUTES
93
94 =head2 application_name
95
96 The name of the application class, e.g. MyApp
97
98 =head1 SEE ALSO
99
100 L<Catalyst>
101
102 L<MooseX::Getopt>
103
104 =head1 AUTHORS
105
106 Catalyst Contributors, see Catalyst.pm
107
108 =head1 COPYRIGHT
109
110 This library is free software, you can redistribute it and/or modify
111 it under the same terms as Perl itself.
112
113 =cut