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 undef $t->{tiedarr}; # work-around for a memory leak
99 $t->setOptions( 'hide_HeadRow', 1 );
100 $t->setOptions( 'hide_HeadLine', 1 );
101 $t->setCols('Class');
102 $t->setColWidth( 'Class', 75, 1 );
103 $t->addRow($_) for @plugins;
104 $class->log->debug( "Loaded plugins:\n" . $t->draw );
107 my $dispatcher = $class->dispatcher;
108 my $engine = $class->engine;
109 my $home = $class->config->{home};
111 $class->log->debug(qq/Loaded dispatcher "$dispatcher"/);
112 $class->log->debug(qq/Loaded engine "$engine"/);
116 ? $class->log->debug(qq/Found home "$home"/)
117 : $class->log->debug(qq/Home "$home" doesn't exist/)
118 : $class->log->debug(q/Couldn't find home/);
124 # Initialize our data structure
125 $class->components( {} );
127 $class->setup_components;
129 if ( $class->debug ) {
130 my $t = Text::ASCIITable->new;
131 undef $t->{tiedarr}; # work-around for a memory leak
132 $t->setOptions( 'hide_HeadRow', 1 );
133 $t->setOptions( 'hide_HeadLine', 1 );
134 $t->setCols('Class');
135 $t->setColWidth( 'Class', 75, 1 );
136 $t->addRow($_) for sort keys %{ $class->components };
137 $class->log->debug( "Loaded components:\n" . $t->draw )
138 if ( @{ $t->{tbl_rows} } );
141 # Add our self to components, since we are also a component
142 $class->components->{$class} = $class;
144 $class->setup_actions;
146 if ( $class->debug ) {
147 my $name = $class->config->{name} || 'Application';
148 $class->log->info("$name powered by Catalyst $Catalyst::VERSION");
152 =item $c->setup_components
158 sub setup_components {
162 my ( $component, $context ) = @_;
164 unless ( $component->isa('Catalyst::Base') ) {
168 my $suffix = Catalyst::Utils::class2classsuffix($component);
169 my $config = $class->config->{$suffix} || {};
173 eval { $instance = $component->new( $context, $config ); };
175 if ( my $error = $@ ) {
179 Catalyst::Exception->throw(
180 message => qq/Couldn't instantiate component "$component", "$error"/
188 Module::Pluggable::Fast->import(
189 name => '_components',
191 "$class\::Controller", "$class\::C",
192 "$class\::Model", "$class\::M",
193 "$class\::View", "$class\::V"
195 callback => $callback
199 if ( my $error = $@ ) {
203 Catalyst::Exception->throw(
204 message => qq/Couldn't load components "$error"/
208 for my $component ( $class->_components($class) ) {
209 $class->components->{ ref $component || $component } = $component;
213 =item $c->setup_dispatcher
217 sub setup_dispatcher {
218 my ( $class, $dispatcher ) = @_;
221 $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
224 if ( $ENV{CATALYST_DISPATCHER} ) {
225 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
228 if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
229 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
232 unless ( $dispatcher ) {
233 $dispatcher = 'Catalyst::Dispatcher';
236 $dispatcher->require;
239 Catalyst::Exception->throw(
240 message => qq/Couldn't load dispatcher "$dispatcher", "$@"/
246 push @{"$class\::ISA"}, $dispatcher;
249 $class->dispatcher($dispatcher);
252 =item $c->setup_engine
257 my ( $class, $engine ) = @_;
260 $engine = 'Catalyst::Engine::' . $engine;
263 if ( $ENV{CATALYST_ENGINE} ) {
264 $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
267 if ( $ENV{ uc($class) . '_ENGINE' } ) {
268 $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
271 if ( ! $engine && $ENV{MOD_PERL} ) {
273 my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
276 $version =~ s/(\.[^.]+)\./$1/g;
278 if ( $software eq 'mod_perl') {
280 if ( $version >= 1.99922 ) {
282 $engine = 'Catalyst::Engine::Apache::MP20';
284 if ( Apache2::Request->require ) {
285 $engine = 'Catalyst::Engine::Apache::MP20::Apreq';
289 elsif ( $version >= 1.9901 ) {
291 $engine = 'Catalyst::Engine::Apache::MP19';
293 if ( Apache::Request->require ) {
294 $engine = 'Catalyst::Engine::Apache::MP19::Apreq';
298 elsif ( $version >= 1.24 ) {
300 $engine = 'Catalyst::Engine::Apache::MP13';
302 if ( Apache::Request->require ) {
303 $engine = 'Catalyst::Engine::Apache::MP13::Apreq';
308 Catalyst::Exception->throw(
309 message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/
314 elsif ( $software eq 'Zeus-Perl' ) {
315 $engine = 'Catalyst::Engine::Zeus';
319 Catalyst::Exception->throw(
320 message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/
326 $engine = 'Catalyst::Engine::CGI';
332 Catalyst::Exception->throw(
333 message => qq/Couldn't load engine "$engine", "$@"/
339 push @{"$class\::ISA"}, $engine;
342 $class->engine($engine);
350 my ( $class, $home ) = @_;
352 if ( $ENV{CATALYST_HOME} ) {
353 $home = $ENV{CATALYST_HOME};
356 if ( $ENV{ uc($class) . '_HOME' } ) {
357 $home = $ENV{ uc($class) . '_HOME' };
361 $home = Catalyst::Utils::home($class);
365 $class->config->{home} ||= $home;
366 $class->config->{root} ||= dir($home)->subdir('root');
375 my ( $class, $debug ) = @_;
377 unless ( $class->log ) {
378 $class->log( Catalyst::Log->new );
381 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) {
383 *{"$class\::debug"} = sub { 1 };
384 $class->log->debug('Debug messages enabled');
388 =item $c->setup_plugins
393 my ( $class, $plugins ) = @_;
395 for my $plugin ( @$plugins ) {
397 $plugin = "Catalyst::Plugin::$plugin";
402 Catalyst::Exception->throw(
403 message => qq/Couldn't load plugin "$plugin", "$@"/
409 push @{"$class\::ISA"}, $plugin;
418 Sebastian Riedel, C<sri@cpan.org>
419 Christian Hansen, C<ch@ngmedia.com>
423 This program is free software, you can redistribute it and/or modify
424 it under the same terms as Perl itself.