1 package Catalyst::Setup;
4 use Catalyst::Exception;
12 Catalyst::Setup - The Catalyst Setup class
24 =item $c->setup_components
30 sub setup_components {
34 my ( $component, $context ) = @_;
36 unless ( $component->isa('Catalyst::Base') ) {
40 my $suffix = Catalyst::Utils::class2classsuffix($component);
41 my $config = $class->config->{$suffix} || {};
45 eval { $instance = $component->new( $context, $config ); };
47 if ( my $error = $@ ) {
51 Catalyst::Exception->throw(
52 message => qq/Couldn't instantiate component "$component", "$error"/
60 Module::Pluggable::Fast->import(
61 name => '_components',
63 "$class\::Controller", "$class\::C",
64 "$class\::Model", "$class\::M",
65 "$class\::View", "$class\::V"
71 if ( my $error = $@ ) {
75 Catalyst::Exception->throw(
76 message => qq/Couldn't load components "$error"/
80 for my $component ( $class->_components($class) ) {
81 $class->components->{ ref $component || $component } = $component;
85 =item $c->setup_dispatcher
89 sub setup_dispatcher {
90 my ( $class, $dispatcher ) = @_;
93 $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
96 if ( $ENV{CATALYST_DISPATCHER} ) {
97 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
100 if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
101 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
104 unless ( $dispatcher ) {
105 $dispatcher = 'Catalyst::Dispatcher';
108 $dispatcher->require;
111 Catalyst::Exception->throw(
112 message => qq/Couldn't load dispatcher "$dispatcher", "$@"/
118 push @{"$class\::ISA"}, $dispatcher;
121 $class->dispatcher($dispatcher);
124 =item $c->setup_engine
129 my ( $class, $engine ) = @_;
132 $engine = 'Catalyst::Engine::' . $engine;
135 if ( $ENV{CATALYST_ENGINE} ) {
136 $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
139 if ( $ENV{ uc($class) . '_ENGINE' } ) {
140 $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
143 if ( ! $engine && $ENV{MOD_PERL} ) {
145 my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
148 $version =~ s/(\.[^.]+)\./$1/g;
150 if ( $software eq 'mod_perl') {
152 if ( $version >= 1.99922 ) {
154 $engine = 'Catalyst::Engine::Apache::MP20';
156 if ( Apache2::Request->require ) {
157 $engine = 'Catalyst::Engine::Apache::MP20::Apreq';
161 elsif ( $version >= 1.9901 ) {
163 $engine = 'Catalyst::Engine::Apache::MP19';
165 if ( Apache::Request->require ) {
166 $engine = 'Catalyst::Engine::Apache::MP19::Apreq';
170 elsif ( $version >= 1.24 ) {
172 $engine = 'Catalyst::Engine::Apache::MP13';
174 if ( Apache::Request->require ) {
175 $engine = 'Catalyst::Engine::Apache::MP13::Apreq';
180 Catalyst::Exception->throw(
181 message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/
186 elsif ( $software eq 'Zeus-Perl' ) {
187 $engine = 'Catalyst::Engine::Zeus';
191 Catalyst::Exception->throw(
192 message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/
198 $engine = 'Catalyst::Engine::CGI';
204 Catalyst::Exception->throw(
205 message => qq/Couldn't load engine "$engine", "$@"/
211 push @{"$class\::ISA"}, $engine;
214 $class->engine($engine);
222 my ( $class, $home ) = @_;
224 if ( $ENV{CATALYST_HOME} ) {
225 $home = $ENV{CATALYST_HOME};
228 if ( $ENV{ uc($class) . '_HOME' } ) {
229 $home = $ENV{ uc($class) . '_HOME' };
233 $home = Catalyst::Utils::home($class);
237 $class->config->{home} = $home;
238 $class->config->{root} = dir($home)->subdir('root');
247 my ( $class, $debug ) = @_;
249 unless ( $class->log ) {
250 $class->log( Catalyst::Log->new );
253 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) {
255 *{"$class\::debug"} = sub { 1 };
256 $class->log->debug('Debug messages enabled');
260 =item $c->setup_plugins
265 my ( $class, $plugins ) = @_;
267 for my $plugin ( @$plugins ) {
269 $plugin = "Catalyst::Plugin::$plugin";
274 Catalyst::Exception->throw(
275 message => qq/Couldn't load plugin "$plugin", "$@"/
281 push @{"$class\::ISA"}, $plugin;
290 Sebastian Riedel, C<sri@cpan.org>
291 Christian Hansen, C<ch@ngmedia.com>
295 This program is free software, you can redistribute it and/or modify
296 it under the same terms as Perl itself.