1 package Catalyst::Setup;
4 use Catalyst::Exception;
10 require Module::Pluggable::Fast;
14 Catalyst::Setup - The Catalyst Setup class
35 my ( $class, @arguments ) = @_;
37 unless ( $class->isa('Catalyst') ) {
39 Catalyst::Exception->throw(
40 message => qq/'$class' does not inherit from Catalyst/
44 if ( $class->arguments ) {
45 @arguments = ( @arguments, @{ $class->arguments } );
51 foreach (@arguments) {
54 $flags->{log} = ( $flags->{log} ) ? 'debug,' . $flags->{log} : 'debug';
56 elsif (/^-(\w+)=?(.*)$/) {
57 $flags->{ lc $1 } = $2;
60 push @{ $flags->{plugins} }, $_;
64 $class->setup_log ( delete $flags->{log} );
65 $class->setup_plugins ( delete $flags->{plugins} );
66 $class->setup_dispatcher ( delete $flags->{dispatcher} );
67 $class->setup_engine ( delete $flags->{engine} );
68 $class->setup_home ( delete $flags->{home} );
70 for my $flag ( sort keys %{ $flags } ) {
72 if ( my $code = $class->can( 'setup_' . $flag ) ) {
73 &$code( $class, delete $flags->{$flag} );
76 $class->log->warn(qq/Unknown flag "$flag"/);
80 $class->log->warn( "You are running an old helper script! "
81 . "Please update your scripts by regenerating the "
82 . "application and copying over the new scripts." )
83 if ( $ENV{CATALYST_SCRIPT_GEN}
84 && ( $ENV{CATALYST_SCRIPT_GEN} < $Catalyst::CATALYST_SCRIPT_GEN ) );
87 if ( $class->debug ) {
93 @plugins = grep { /^Catalyst::Plugin/ } @{"$class\::ISA"};
97 my $t = Text::ASCIITable->new;
98 $t->setOptions( 'hide_HeadRow', 1 );
99 $t->setOptions( 'hide_HeadLine', 1 );
100 $t->setCols('Class');
101 $t->setColWidth( 'Class', 75, 1 );
102 $t->addRow($_) for @plugins;
103 $class->log->debug( "Loaded plugins:\n" . $t->draw );
106 my $dispatcher = $class->dispatcher;
107 my $engine = $class->engine;
108 my $home = $class->config->{home};
110 $class->log->debug(qq/Loaded dispatcher "$dispatcher"/);
111 $class->log->debug(qq/Loaded engine "$engine"/);
115 ? $class->log->debug(qq/Found home "$home"/)
116 : $class->log->debug(qq/Home "$home" doesn't exist/)
117 : $class->log->debug(q/Couldn't find home/);
123 # Initialize our data structure
124 $class->components( {} );
126 $class->setup_components;
128 if ( $class->debug ) {
129 my $t = Text::ASCIITable->new;
130 $t->setOptions( 'hide_HeadRow', 1 );
131 $t->setOptions( 'hide_HeadLine', 1 );
132 $t->setCols('Class');
133 $t->setColWidth( 'Class', 75, 1 );
134 $t->addRow($_) for sort keys %{ $class->components };
135 $class->log->debug( "Loaded components:\n" . $t->draw )
136 if ( @{ $t->{tbl_rows} } );
139 # Add our self to components, since we are also a component
140 $class->components->{$class} = $class;
142 $class->setup_actions;
144 if ( $class->debug ) {
145 my $name = $class->config->{name} || 'Application';
146 $class->log->info("$name powered by Catalyst $Catalyst::VERSION");
150 =item $c->setup_components
156 sub setup_components {
160 my ( $component, $context ) = @_;
162 unless ( $component->isa('Catalyst::Base') ) {
166 my $suffix = Catalyst::Utils::class2classsuffix($component);
167 my $config = $class->config->{$suffix} || {};
171 eval { $instance = $component->new( $context, $config ); };
173 if ( my $error = $@ ) {
177 Catalyst::Exception->throw(
178 message => qq/Couldn't instantiate component "$component", "$error"/
186 Module::Pluggable::Fast->import(
187 name => '_components',
189 "$class\::Controller", "$class\::C",
190 "$class\::Model", "$class\::M",
191 "$class\::View", "$class\::V"
193 callback => $callback
197 if ( my $error = $@ ) {
201 Catalyst::Exception->throw(
202 message => qq/Couldn't load components "$error"/
206 for my $component ( $class->_components($class) ) {
207 $class->components->{ ref $component || $component } = $component;
211 =item $c->setup_dispatcher
215 sub setup_dispatcher {
216 my ( $class, $dispatcher ) = @_;
219 $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
222 if ( $ENV{CATALYST_DISPATCHER} ) {
223 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
226 if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
227 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
230 unless ( $dispatcher ) {
231 $dispatcher = 'Catalyst::Dispatcher';
234 $dispatcher->require;
237 Catalyst::Exception->throw(
238 message => qq/Couldn't load dispatcher "$dispatcher", "$@"/
244 push @{"$class\::ISA"}, $dispatcher;
247 $class->dispatcher($dispatcher);
250 =item $c->setup_engine
255 my ( $class, $engine ) = @_;
258 $engine = 'Catalyst::Engine::' . $engine;
261 if ( $ENV{CATALYST_ENGINE} ) {
262 $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
265 if ( $ENV{ uc($class) . '_ENGINE' } ) {
266 $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
269 if ( ! $engine && $ENV{MOD_PERL} ) {
271 my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
274 $version =~ s/(\.[^.]+)\./$1/g;
276 if ( $software eq 'mod_perl') {
278 if ( $version >= 1.99922 ) {
280 $engine = 'Catalyst::Engine::Apache::MP20';
282 if ( Apache2::Request->require ) {
283 $engine = 'Catalyst::Engine::Apache::MP20::Apreq';
287 elsif ( $version >= 1.9901 ) {
289 $engine = 'Catalyst::Engine::Apache::MP19';
291 if ( Apache::Request->require ) {
292 $engine = 'Catalyst::Engine::Apache::MP19::Apreq';
296 elsif ( $version >= 1.24 ) {
298 $engine = 'Catalyst::Engine::Apache::MP13';
300 if ( Apache::Request->require ) {
301 $engine = 'Catalyst::Engine::Apache::MP13::Apreq';
306 Catalyst::Exception->throw(
307 message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/
312 elsif ( $software eq 'Zeus-Perl' ) {
313 $engine = 'Catalyst::Engine::Zeus';
317 Catalyst::Exception->throw(
318 message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/
324 $engine = 'Catalyst::Engine::CGI';
330 Catalyst::Exception->throw(
331 message => qq/Couldn't load engine "$engine", "$@"/
337 push @{"$class\::ISA"}, $engine;
340 $class->engine($engine);
348 my ( $class, $home ) = @_;
350 if ( $ENV{CATALYST_HOME} ) {
351 $home = $ENV{CATALYST_HOME};
354 if ( $ENV{ uc($class) . '_HOME' } ) {
355 $home = $ENV{ uc($class) . '_HOME' };
359 $home = Catalyst::Utils::home($class);
363 $class->config->{home} ||= $home;
364 $class->config->{root} ||= dir($home)->subdir('root');
373 my ( $class, $debug ) = @_;
375 unless ( $class->log ) {
376 $class->log( Catalyst::Log->new );
379 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) {
381 *{"$class\::debug"} = sub { 1 };
382 $class->log->debug('Debug messages enabled');
386 =item $c->setup_plugins
391 my ( $class, $plugins ) = @_;
393 for my $plugin ( @$plugins ) {
395 $plugin = "Catalyst::Plugin::$plugin";
400 Catalyst::Exception->throw(
401 message => qq/Couldn't load plugin "$plugin", "$@"/
407 push @{"$class\::ISA"}, $plugin;
416 Sebastian Riedel, C<sri@cpan.org>
417 Christian Hansen, C<ch@ngmedia.com>
421 This program is free software, you can redistribute it and/or modify
422 it under the same terms as Perl itself.