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