1 package Catalyst::Setup;
4 use base qw/Class::Data::Inheritable/;
5 use Catalyst::Exception;
11 __PACKAGE__->mk_classdata($_) for qw/arguments dispatcher engine log/;
15 Catalyst::Setup - The Catalyst Setup class
41 # Initialize our data structure
42 $class->components( {} );
44 $class->setup_components;
46 if ( $class->debug ) {
47 my $t = Text::ASCIITable->new;
48 $t->setOptions( 'hide_HeadRow', 1 );
49 $t->setOptions( 'hide_HeadLine', 1 );
51 $t->setColWidth( 'Class', 75, 1 );
52 $t->addRow($_) for sort keys %{ $class->components };
53 $class->log->debug( "Loaded components:\n" . $t->draw )
54 if ( @{ $t->{tbl_rows} } );
57 # Add our self to components, since we are also a component
58 $class->components->{$class} = $class;
60 $class->setup_actions;
62 if ( $class->debug ) {
63 my $name = $class->config->{name} || 'Application';
64 $class->log->info("$name powered by Catalyst $Catalyst::VERSION");
68 =item $c->setup_components
74 sub setup_components {
78 my ( $component, $context ) = @_;
80 unless ( $component->isa('Catalyst::Base') ) {
84 my $suffix = Catalyst::Utils::class2classsuffix($component);
85 my $config = $class->config->{$suffix} || {};
89 eval { $instance = $component->new( $context, $config ); };
91 if ( my $error = $@ ) {
95 Catalyst::Exception->throw(
96 message => qq/Couldn't instantiate component "$component", "$error"/
104 Module::Pluggable::Fast->import(
105 name => '_components',
107 "$class\::Controller", "$class\::C",
108 "$class\::Model", "$class\::M",
109 "$class\::View", "$class\::V"
111 callback => $callback
115 if ( my $error = $@ ) {
119 Catalyst::Exception->throw(
120 message => qq/Couldn't load components "$error"/
124 for my $component ( $class->_components($class) ) {
125 $class->components->{ ref $component || $component } = $component;
129 =item $c->setup_dispatcher
133 sub setup_dispatcher {
134 my ( $class, $dispatcher ) = @_;
137 $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
140 if ( $ENV{CATALYST_DISPATCHER} ) {
141 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
144 if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
145 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
148 unless ( $dispatcher ) {
149 $dispatcher = 'Catalyst::Dispatcher';
152 $dispatcher->require;
155 Catalyst::Exception->throw(
156 message => qq/Couldn't load dispatcher "$dispatcher", "$@"/
162 push @{"$class\::ISA"}, $dispatcher;
165 $class->dispatcher($dispatcher);
168 =item $c->setup_engine
173 my ( $class, $engine ) = @_;
176 $engine = 'Catalyst::Engine::' . $engine;
179 if ( $ENV{CATALYST_ENGINE} ) {
180 $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
183 if ( $ENV{ uc($class) . '_ENGINE' } ) {
184 $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
187 if ( ! $engine && $ENV{MOD_PERL} ) {
189 my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
192 $version =~ s/(\.[^.]+)\./$1/g;
194 if ( $software eq 'mod_perl') {
196 if ( $version >= 1.99922 ) {
198 $engine = 'Catalyst::Engine::Apache::MP20';
200 if ( Apache2::Request->require ) {
201 $engine = 'Catalyst::Engine::Apache::MP20::Apreq';
205 elsif ( $version >= 1.9901 ) {
207 $engine = 'Catalyst::Engine::Apache::MP19';
209 if ( Apache::Request->require ) {
210 $engine = 'Catalyst::Engine::Apache::MP19::Apreq';
214 elsif ( $version >= 1.24 ) {
216 $engine = 'Catalyst::Engine::Apache::MP13';
218 if ( Apache::Request->require ) {
219 $engine = 'Catalyst::Engine::Apache::MP13::Apreq';
224 Catalyst::Exception->throw(
225 message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/
230 elsif ( $software eq 'Zeus-Perl' ) {
231 $engine = 'Catalyst::Engine::Zeus';
235 Catalyst::Exception->throw(
236 message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/
242 $engine = 'Catalyst::Engine::CGI';
248 Catalyst::Exception->throw(
249 message => qq/Couldn't load engine "$engine", "$@"/
255 push @{"$class\::ISA"}, $engine;
258 $class->engine($engine);
266 my ( $class, $home ) = @_;
268 if ( $ENV{CATALYST_HOME} ) {
269 $home = $ENV{CATALYST_HOME};
272 if ( $ENV{ uc($class) . '_HOME' } ) {
273 $home = $ENV{ uc($class) . '_HOME' };
277 $home = Catalyst::Utils::home($class);
281 $class->config->{home} = $home;
282 $class->config->{root} = dir($home)->subdir('root');
291 my ( $class, $debug ) = @_;
293 unless ( $class->log ) {
294 $class->log( Catalyst::Log->new );
297 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) {
299 *{"$class\::debug"} = sub { 1 };
300 $class->log->debug('Debug messages enabled');
304 =item $c->setup_plugins
309 my ( $class, $plugins ) = @_;
311 for my $plugin ( @$plugins ) {
313 $plugin = "Catalyst::Plugin::$plugin";
318 Catalyst::Exception->throw(
319 message => qq/Couldn't load plugin "$plugin", "$@"/
325 push @{"$class\::ISA"}, $plugin;
334 Sebastian Riedel, C<sri@cpan.org>
335 Christian Hansen, C<ch@ngmedia.com>
339 This program is free software, you can redistribute it and/or modify
340 it under the same terms as Perl itself.