Added temporary workaround for debug memory leak in Text::ASCIITable
[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;
9edb1eb3 98 undef $t->{tiedarr}; # work-around for a memory leak
10dd6896 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 );
105 }
106
107 my $dispatcher = $class->dispatcher;
108 my $engine = $class->engine;
109 my $home = $class->config->{home};
110
111 $class->log->debug(qq/Loaded dispatcher "$dispatcher"/);
112 $class->log->debug(qq/Loaded engine "$engine"/);
113
114 $home
115 ? ( -d $home )
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/);
119 }
5da4f9c6 120
121 # Call plugins setup
122 $class->NEXT::setup;
123
124 # Initialize our data structure
125 $class->components( {} );
126
127 $class->setup_components;
128
129 if ( $class->debug ) {
130 my $t = Text::ASCIITable->new;
9edb1eb3 131 undef $t->{tiedarr}; # work-around for a memory leak
5da4f9c6 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} } );
139 }
140
141 # Add our self to components, since we are also a component
142 $class->components->{$class} = $class;
143
144 $class->setup_actions;
145
146 if ( $class->debug ) {
147 my $name = $class->config->{name} || 'Application';
148 $class->log->info("$name powered by Catalyst $Catalyst::VERSION");
149 }
150}
151
5993f5a2 152=item $c->setup_components
5d9a6d47 153
154Setup components.
155
156=cut
157
158sub setup_components {
159 my $class = shift;
160
161 my $callback = sub {
162 my ( $component, $context ) = @_;
163
164 unless ( $component->isa('Catalyst::Base') ) {
165 return $component;
166 }
167
168 my $suffix = Catalyst::Utils::class2classsuffix($component);
169 my $config = $class->config->{$suffix} || {};
170
171 my $instance;
172
173 eval { $instance = $component->new( $context, $config ); };
174
175 if ( my $error = $@ ) {
176
177 chomp $error;
178
179 Catalyst::Exception->throw(
180 message => qq/Couldn't instantiate component "$component", "$error"/
181 );
182 }
183
184 return $instance;
185 };
186
187 eval {
188 Module::Pluggable::Fast->import(
189 name => '_components',
190 search => [
191 "$class\::Controller", "$class\::C",
192 "$class\::Model", "$class\::M",
193 "$class\::View", "$class\::V"
194 ],
195 callback => $callback
196 );
197 };
198
199 if ( my $error = $@ ) {
200
201 chomp $error;
202
203 Catalyst::Exception->throw(
204 message => qq/Couldn't load components "$error"/
205 );
206 }
207
208 for my $component ( $class->_components($class) ) {
209 $class->components->{ ref $component || $component } = $component;
210 }
211}
212
213=item $c->setup_dispatcher
214
215=cut
216
217sub setup_dispatcher {
218 my ( $class, $dispatcher ) = @_;
219
220 if ( $dispatcher ) {
221 $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
222 }
223
224 if ( $ENV{CATALYST_DISPATCHER} ) {
225 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
226 }
227
228 if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
229 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
230 }
231
232 unless ( $dispatcher ) {
233 $dispatcher = 'Catalyst::Dispatcher';
234 }
235
236 $dispatcher->require;
237
238 if ( $@ ) {
239 Catalyst::Exception->throw(
240 message => qq/Couldn't load dispatcher "$dispatcher", "$@"/
241 );
242 }
243
244 {
245 no strict 'refs';
246 push @{"$class\::ISA"}, $dispatcher;
247 }
248
249 $class->dispatcher($dispatcher);
250}
251
252=item $c->setup_engine
253
254=cut
255
256sub setup_engine {
257 my ( $class, $engine ) = @_;
258
259 if ( $engine ) {
260 $engine = 'Catalyst::Engine::' . $engine;
261 }
262
263 if ( $ENV{CATALYST_ENGINE} ) {
264 $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
265 }
266
267 if ( $ENV{ uc($class) . '_ENGINE' } ) {
268 $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
269 }
270
271 if ( ! $engine && $ENV{MOD_PERL} ) {
272
273 my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
274
275 $version =~ s/_//g;
276 $version =~ s/(\.[^.]+)\./$1/g;
277
278 if ( $software eq 'mod_perl') {
279
280 if ( $version >= 1.99922 ) {
281
282 $engine = 'Catalyst::Engine::Apache::MP20';
283
284 if ( Apache2::Request->require ) {
285 $engine = 'Catalyst::Engine::Apache::MP20::Apreq';
286 }
287 }
288
289 elsif ( $version >= 1.9901 ) {
290
291 $engine = 'Catalyst::Engine::Apache::MP19';
292
293 if ( Apache::Request->require ) {
294 $engine = 'Catalyst::Engine::Apache::MP19::Apreq';
295 }
296 }
297
298 elsif ( $version >= 1.24 ) {
299
300 $engine = 'Catalyst::Engine::Apache::MP13';
301
302 if ( Apache::Request->require ) {
303 $engine = 'Catalyst::Engine::Apache::MP13::Apreq';
304 }
305 }
306
307 else {
308 Catalyst::Exception->throw(
309 message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/
310 );
311 }
312 }
313
314 elsif ( $software eq 'Zeus-Perl' ) {
315 $engine = 'Catalyst::Engine::Zeus';
316 }
317
318 else {
319 Catalyst::Exception->throw(
320 message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/
321 );
322 }
323 }
324
325 unless ( $engine ) {
326 $engine = 'Catalyst::Engine::CGI';
327 }
328
329 $engine->require;
330
331 if ( $@ ) {
332 Catalyst::Exception->throw(
333 message => qq/Couldn't load engine "$engine", "$@"/
334 );
335 }
336
337 {
338 no strict 'refs';
339 push @{"$class\::ISA"}, $engine;
340 }
341
342 $class->engine($engine);
343}
344
345=item $c->setup_home
346
347=cut
348
349sub setup_home {
350 my ( $class, $home ) = @_;
351
352 if ( $ENV{CATALYST_HOME} ) {
353 $home = $ENV{CATALYST_HOME};
354 }
355
356 if ( $ENV{ uc($class) . '_HOME' } ) {
357 $home = $ENV{ uc($class) . '_HOME' };
358 }
359
360 unless ( $home ) {
361 $home = Catalyst::Utils::home($class);
362 }
363
364 if ( $home ) {
e0143040 365 $class->config->{home} ||= $home;
366 $class->config->{root} ||= dir($home)->subdir('root');
5d9a6d47 367 }
368}
369
370=item $c->setup_log
371
372=cut
373
374sub setup_log {
375 my ( $class, $debug ) = @_;
376
377 unless ( $class->log ) {
378 $class->log( Catalyst::Log->new );
379 }
380
381 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) {
382 no strict 'refs';
383 *{"$class\::debug"} = sub { 1 };
384 $class->log->debug('Debug messages enabled');
385 }
386}
387
388=item $c->setup_plugins
389
390=cut
391
392sub setup_plugins {
393 my ( $class, $plugins ) = @_;
394
395 for my $plugin ( @$plugins ) {
396
397 $plugin = "Catalyst::Plugin::$plugin";
398
399 $plugin->require;
400
401 if ( $@ ) {
402 Catalyst::Exception->throw(
403 message => qq/Couldn't load plugin "$plugin", "$@"/
404 );
405 }
406
407 {
408 no strict 'refs';
409 push @{"$class\::ISA"}, $plugin;
410 }
411 }
412}
413
414=back
415
416=head1 AUTHOR
417
418Sebastian Riedel, C<sri@cpan.org>
419Christian Hansen, C<ch@ngmedia.com>
420
421=head1 COPYRIGHT
422
423This program is free software, you can redistribute it and/or modify
424it under the same terms as Perl itself.
425
426=cut
427
4281;