In Catalyst::Test, don't mangle headers of non-HTML responses. RT#79043
[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;
b1ededd4 6use Catalyst::EngineLoader;
532f0516 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,
b1ededd4 29 default => 'Catalyst::EngineLoader',
532f0516 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 {
aee7cdcc 65 my $self = shift;
66 return {
67 argv => $self->ARGV,
68 extra_argv => $self->extra_argv,
69 }
d3082fac 70}
71
c821df21 72sub _plack_loader_args {
acbecf08 73 my $self = shift;
74 my @app_args = $self->_application_args;
c821df21 75 return (port => $app_args[0]);
76}
77
aee7cdcc 78sub _plack_engine_name {}
79
d3082fac 80sub _run_application {
81 my $self = shift;
82 my $app = $self->application_name;
83 Class::MOP::load_class($app);
c821df21 84 my $server;
aee7cdcc 85 if (my $e = $self->_plack_engine_name ) {
86 $server = $self->load_engine($e, $self->_plack_loader_args);
c821df21 87 }
88 else {
532f0516 89 $server = $self->autoload_engine($self->_plack_loader_args);
c821df21 90 }
4b0f97fc 91 $app->run($self->_application_args, $server);
d3082fac 92}
93
941;
1628b022 95
96=head1 NAME
97
98Catalyst::ScriptRole - Common functionality for Catalyst scripts.
99
100=head1 SYNOPSIS
101
12aa6ca4 102 package MyApp::Script::Foo;
103 use Moose;
104 use namespace::autoclean;
111d3c9a 105
2c298960 106 with 'Catalyst::ScriptRole';
111d3c9a 107
ad8b4c91 108 sub _application_args { ... }
111d3c9a 109
1628b022 110=head1 DESCRIPTION
111
12aa6ca4 112Role with the common functionality of Catalyst scripts.
113
114=head1 METHODS
115
116=head2 run
117
118The method invoked to run the application.
119
120=head1 ATTRIBUTES
121
122=head2 application_name
123
124The name of the application class, e.g. MyApp
125
126=head1 SEE ALSO
127
128L<Catalyst>
129
130L<MooseX::Getopt>
1628b022 131
132=head1 AUTHORS
133
134Catalyst Contributors, see Catalyst.pm
135
136=head1 COPYRIGHT
137
138This library is free software, you can redistribute it and/or modify
139it under the same terms as Perl itself.
140
141=cut