Merged 5.49_01 (r1339) from refactored branch to trunk
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Setup.pm
CommitLineData
5d9a6d47 1package Catalyst::Setup;
2
3use strict;
4use Catalyst::Exception;
5use Catalyst::Log;
6use Catalyst::Utils;
7use Path::Class;
8use Text::ASCIITable;
9
10dd6896 10require Module::Pluggable::Fast;
5da4f9c6 11
5d9a6d47 12=head1 NAME
13
14Catalyst::Setup - The Catalyst Setup class
15
16=head1 SYNOPSIS
17
18See L<Catalyst>.
19
20=head1 DESCRIPTION
21
22=head1 METHODS
23
24=over 4
25
5da4f9c6 26=item $c->setup
27
28Setup.
29
30 $c->setup;
31
32=cut
33
34sub setup {
10dd6896 35 my ( $class, @arguments ) = @_;
36
37 unless ( $class->isa('Catalyst') ) {
38
39 Catalyst::Exception->throw(
40 message => qq/'$class' does not inherit from Catalyst/
41 );
42 }
43
44 if ( $class->arguments ) {
45 @arguments = ( @arguments, @{ $class->arguments } );
46 }
47
48 # Process options
49 my $flags = { };
50
51 foreach (@arguments) {
52
53 if ( /^-Debug$/ ) {
54 $flags->{log} = ( $flags->{log} ) ? 'debug,' . $flags->{log} : 'debug';
55 }
56 elsif (/^-(\w+)=?(.*)$/) {
57 $flags->{ lc $1 } = $2;
58 }
59 else {
60 push @{ $flags->{plugins} }, $_;
61 }
62 }
63
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} );
69
70 for my $flag ( sort keys %{ $flags } ) {
71
72 if ( my $code = $class->can( 'setup_' . $flag ) ) {
73 &$code( $class, delete $flags->{$flag} );
74 }
75 else {
76 $class->log->warn(qq/Unknown flag "$flag"/);
77 }
78 }
79
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 ) );
85
86
87 if ( $class->debug ) {
88
89 my @plugins = ();
90
91 {
92 no strict 'refs';
93 @plugins = grep { /^Catalyst::Plugin/ } @{"$class\::ISA"};
94 }
95
96 if ( @plugins ) {
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 );
104 }
105
106 my $dispatcher = $class->dispatcher;
107 my $engine = $class->engine;
108 my $home = $class->config->{home};
109
110 $class->log->debug(qq/Loaded dispatcher "$dispatcher"/);
111 $class->log->debug(qq/Loaded engine "$engine"/);
112
113 $home
114 ? ( -d $home )
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/);
118 }
5da4f9c6 119
120 # Call plugins setup
121 $class->NEXT::setup;
122
123 # Initialize our data structure
124 $class->components( {} );
125
126 $class->setup_components;
127
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} } );
137 }
138
139 # Add our self to components, since we are also a component
140 $class->components->{$class} = $class;
141
142 $class->setup_actions;
143
144 if ( $class->debug ) {
145 my $name = $class->config->{name} || 'Application';
146 $class->log->info("$name powered by Catalyst $Catalyst::VERSION");
147 }
148}
149
5993f5a2 150=item $c->setup_components
5d9a6d47 151
152Setup components.
153
154=cut
155
156sub setup_components {
157 my $class = shift;
158
159 my $callback = sub {
160 my ( $component, $context ) = @_;
161
162 unless ( $component->isa('Catalyst::Base') ) {
163 return $component;
164 }
165
166 my $suffix = Catalyst::Utils::class2classsuffix($component);
167 my $config = $class->config->{$suffix} || {};
168
169 my $instance;
170
171 eval { $instance = $component->new( $context, $config ); };
172
173 if ( my $error = $@ ) {
174
175 chomp $error;
176
177 Catalyst::Exception->throw(
178 message => qq/Couldn't instantiate component "$component", "$error"/
179 );
180 }
181
182 return $instance;
183 };
184
185 eval {
186 Module::Pluggable::Fast->import(
187 name => '_components',
188 search => [
189 "$class\::Controller", "$class\::C",
190 "$class\::Model", "$class\::M",
191 "$class\::View", "$class\::V"
192 ],
193 callback => $callback
194 );
195 };
196
197 if ( my $error = $@ ) {
198
199 chomp $error;
200
201 Catalyst::Exception->throw(
202 message => qq/Couldn't load components "$error"/
203 );
204 }
205
206 for my $component ( $class->_components($class) ) {
207 $class->components->{ ref $component || $component } = $component;
208 }
209}
210
211=item $c->setup_dispatcher
212
213=cut
214
215sub setup_dispatcher {
216 my ( $class, $dispatcher ) = @_;
217
218 if ( $dispatcher ) {
219 $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
220 }
221
222 if ( $ENV{CATALYST_DISPATCHER} ) {
223 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
224 }
225
226 if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
227 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
228 }
229
230 unless ( $dispatcher ) {
231 $dispatcher = 'Catalyst::Dispatcher';
232 }
233
234 $dispatcher->require;
235
236 if ( $@ ) {
237 Catalyst::Exception->throw(
238 message => qq/Couldn't load dispatcher "$dispatcher", "$@"/
239 );
240 }
241
242 {
243 no strict 'refs';
244 push @{"$class\::ISA"}, $dispatcher;
245 }
246
247 $class->dispatcher($dispatcher);
248}
249
250=item $c->setup_engine
251
252=cut
253
254sub setup_engine {
255 my ( $class, $engine ) = @_;
256
257 if ( $engine ) {
258 $engine = 'Catalyst::Engine::' . $engine;
259 }
260
261 if ( $ENV{CATALYST_ENGINE} ) {
262 $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
263 }
264
265 if ( $ENV{ uc($class) . '_ENGINE' } ) {
266 $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
267 }
268
269 if ( ! $engine && $ENV{MOD_PERL} ) {
270
271 my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
272
273 $version =~ s/_//g;
274 $version =~ s/(\.[^.]+)\./$1/g;
275
276 if ( $software eq 'mod_perl') {
277
278 if ( $version >= 1.99922 ) {
279
280 $engine = 'Catalyst::Engine::Apache::MP20';
281
282 if ( Apache2::Request->require ) {
283 $engine = 'Catalyst::Engine::Apache::MP20::Apreq';
284 }
285 }
286
287 elsif ( $version >= 1.9901 ) {
288
289 $engine = 'Catalyst::Engine::Apache::MP19';
290
291 if ( Apache::Request->require ) {
292 $engine = 'Catalyst::Engine::Apache::MP19::Apreq';
293 }
294 }
295
296 elsif ( $version >= 1.24 ) {
297
298 $engine = 'Catalyst::Engine::Apache::MP13';
299
300 if ( Apache::Request->require ) {
301 $engine = 'Catalyst::Engine::Apache::MP13::Apreq';
302 }
303 }
304
305 else {
306 Catalyst::Exception->throw(
307 message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/
308 );
309 }
310 }
311
312 elsif ( $software eq 'Zeus-Perl' ) {
313 $engine = 'Catalyst::Engine::Zeus';
314 }
315
316 else {
317 Catalyst::Exception->throw(
318 message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/
319 );
320 }
321 }
322
323 unless ( $engine ) {
324 $engine = 'Catalyst::Engine::CGI';
325 }
326
327 $engine->require;
328
329 if ( $@ ) {
330 Catalyst::Exception->throw(
331 message => qq/Couldn't load engine "$engine", "$@"/
332 );
333 }
334
335 {
336 no strict 'refs';
337 push @{"$class\::ISA"}, $engine;
338 }
339
340 $class->engine($engine);
341}
342
343=item $c->setup_home
344
345=cut
346
347sub setup_home {
348 my ( $class, $home ) = @_;
349
350 if ( $ENV{CATALYST_HOME} ) {
351 $home = $ENV{CATALYST_HOME};
352 }
353
354 if ( $ENV{ uc($class) . '_HOME' } ) {
355 $home = $ENV{ uc($class) . '_HOME' };
356 }
357
358 unless ( $home ) {
359 $home = Catalyst::Utils::home($class);
360 }
361
362 if ( $home ) {
e0143040 363 $class->config->{home} ||= $home;
364 $class->config->{root} ||= dir($home)->subdir('root');
5d9a6d47 365 }
366}
367
368=item $c->setup_log
369
370=cut
371
372sub setup_log {
373 my ( $class, $debug ) = @_;
374
375 unless ( $class->log ) {
376 $class->log( Catalyst::Log->new );
377 }
378
379 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) {
380 no strict 'refs';
381 *{"$class\::debug"} = sub { 1 };
382 $class->log->debug('Debug messages enabled');
383 }
384}
385
386=item $c->setup_plugins
387
388=cut
389
390sub setup_plugins {
391 my ( $class, $plugins ) = @_;
392
393 for my $plugin ( @$plugins ) {
394
395 $plugin = "Catalyst::Plugin::$plugin";
396
397 $plugin->require;
398
399 if ( $@ ) {
400 Catalyst::Exception->throw(
401 message => qq/Couldn't load plugin "$plugin", "$@"/
402 );
403 }
404
405 {
406 no strict 'refs';
407 push @{"$class\::ISA"}, $plugin;
408 }
409 }
410}
411
412=back
413
414=head1 AUTHOR
415
416Sebastian Riedel, C<sri@cpan.org>
417Christian Hansen, C<ch@ngmedia.com>
418
419=head1 COPYRIGHT
420
421This program is free software, you can redistribute it and/or modify
422it under the same terms as Perl itself.
423
424=cut
425
4261;