public apply_default_middlewares method
[catagits/Catalyst-Runtime.git] / lib / Catalyst / EngineLoader.pm
CommitLineData
b1ededd4 1package Catalyst::EngineLoader;
532f0516 2use Moose;
3use Catalyst::Exception;
acbecf08 4use Catalyst::Utils;
532f0516 5use namespace::autoclean;
6
7extends 'Plack::Loader';
8
acbecf08 9has application_name => (
10 isa => 'Str',
11 is => 'ro',
12 required => 1,
13);
14
a26a6adb 15has requested_engine => (
16 is => 'ro',
17 isa => 'Str',
18 predicate => 'has_requested_engine',
1085c936 19);
20
21sub needs_psgi_engine_compat_hack {
22 my ($self) = @_;
a26a6adb 23 return $self->has_requested_engine
24 && $self->requested_engine eq 'PSGI';
1085c936 25}
26
1e5dad00 27has catalyst_engine_class => (
28 isa => 'Str',
29 is => 'rw',
30 lazy => 1,
31 builder => '_guess_catalyst_engine_class',
32);
33
34sub _guess_catalyst_engine_class {
35 my $self = shift;
a26a6adb 36 my $old_engine = $self->has_requested_engine
37 ? $self->requested_engine
38 : Catalyst::Utils::env_value($self->application_name, 'ENGINE');
1e5dad00 39 if (!defined $old_engine) {
40 return 'Catalyst::Engine';
41 }
86c4012a 42 elsif ($old_engine =~ /^(PSGI|CGI|FastCGI|HTTP|Apache.*)$/) {
1e5dad00 43 return 'Catalyst::Engine';
44 }
0ea8962d 45 else {
46 return 'Catalyst::Engine::' . $old_engine;
1e5dad00 47 }
48}
49
532f0516 50around guess => sub {
51 my ($orig, $self) = (shift, shift);
52 my $engine = $self->$orig(@_);
53 if ($engine eq 'Standalone') {
54 if ( $ENV{MOD_PERL} ) {
55 my ( $software, $version ) =
d7132282 56 $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
532f0516 57 $version =~ s/_//g;
d7132282 58 $version =~ s/(\.[^.]+)\./$1/g;
59
532f0516 60 if ( $software eq 'mod_perl' ) {
61 if ( $version >= 1.99922 ) {
62 $engine = 'Apache2';
63 }
64
65 elsif ( $version >= 1.9901 ) {
66 Catalyst::Exception->throw( message => 'Plack does not have a mod_perl 1.99 handler' );
67 $engine = 'Apache2::MP19';
68 }
69
70 elsif ( $version >= 1.24 ) {
71 $engine = 'Apache1';
72 }
73
74 else {
75 Catalyst::Exception->throw( message =>
76 qq/Unsupported mod_perl version: $ENV{MOD_PERL}/ );
77 }
78 }
79 }
80 }
d7132282 81
acbecf08 82 my $old_engine = Catalyst::Utils::env_value($self->application_name, 'ENGINE');
83 if (!defined $old_engine) { # Not overridden
84 }
cac8fd6e 85 elsif ($old_engine =~ /^(PSGI|CGI|HTTP|Apache.*)$/) {
acbecf08 86 # Trust autodetect
87 }
cac8fd6e 88 elsif ($old_engine eq 'FastCGI') {
89 $engine = 'FCGI';
90 }
acbecf08 91 elsif ($old_engine eq "HTTP::Prefork") { # Too bad if you're customising, we don't handle options
92 # write yourself a script to collect and pass in the options
93 $engine = "Starman";
94 }
95 elsif ($old_engine eq "HTTP::POE") {
96 Catalyst::Exception->throw("HTTP::POE engine no longer works, recommend you use Twiggy instead");
97 }
98 elsif ($old_engine eq "Zeus") {
99 Catalyst::Exception->throw("Zeus engine no longer works");
100 }
101 else {
102 warn("You asked for an unrecognised engine '$old_engine' which is no longer supported, this has been ignored.\n");
103 }
104
532f0516 105 return $engine;
106};
107
acbecf08 108# Force constructor inlining
109__PACKAGE__->meta->make_immutable( replace_constructor => 1 );
df1fa879 110
532f0516 1111;
df1fa879 112
113__END__
114
115=head1 NAME
116
b1ededd4 117Catalyst::EngineLoader - The Catalyst Engine Loader
df1fa879 118
119=head1 SYNOPSIS
120
121See L<Catalyst>.
122
123=head1 DESCRIPTION
124
125Wrapper on L<Plack::Loader> which resets the ::Engine if you are using some
126version of mod_perl.
127
128=head1 AUTHORS
129
130Catalyst Contributors, see Catalyst.pm
131
132=head1 COPYRIGHT
133
134This library is free software. You can redistribute it and/or modify it under
135the same terms as Perl itself.
136
1085c936 137=begin Pod::Coverage
138
139needs_psgi_engine_compat_hack
140
141=end Pod::Coverage
142
df1fa879 143=cut