Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Engine; |
2 | |
3 | use strict; |
424b2705 |
4 | use base qw/Class::Data::Inheritable Class::Accessor::Fast/; |
d70195d8 |
5 | use attributes (); |
fc7ec1d9 |
6 | use UNIVERSAL::require; |
6dc87a0f |
7 | use CGI::Cookie; |
fc7ec1d9 |
8 | use Data::Dumper; |
9 | use HTML::Entities; |
10 | use HTTP::Headers; |
11 | use Time::HiRes qw/gettimeofday tv_interval/; |
0f7ecc53 |
12 | use Text::ASCIITable; |
fc7ec1d9 |
13 | use Catalyst::Request; |
146554c5 |
14 | use Catalyst::Request::Upload; |
fc7ec1d9 |
15 | use Catalyst::Response; |
16 | |
17 | require Module::Pluggable::Fast; |
18 | |
99fe1710 |
19 | # For pretty dumps |
fc7ec1d9 |
20 | $Data::Dumper::Terse = 1; |
21 | |
1abd6db7 |
22 | __PACKAGE__->mk_classdata('components'); |
b768faa3 |
23 | __PACKAGE__->mk_accessors(qw/request response state/); |
fc7ec1d9 |
24 | |
fc7ec1d9 |
25 | *comp = \&component; |
26 | *req = \&request; |
27 | *res = \&response; |
28 | |
06e1b616 |
29 | # For backwards compatibility |
30 | *finalize_output = \&finalize_body; |
31 | |
99fe1710 |
32 | # For statistics |
fc7ec1d9 |
33 | our $COUNT = 1; |
34 | our $START = time; |
35 | |
36 | =head1 NAME |
37 | |
38 | Catalyst::Engine - The Catalyst Engine |
39 | |
40 | =head1 SYNOPSIS |
41 | |
42 | See L<Catalyst>. |
43 | |
44 | =head1 DESCRIPTION |
45 | |
23f9d934 |
46 | =head1 METHODS |
fc7ec1d9 |
47 | |
23f9d934 |
48 | =over 4 |
49 | |
23f9d934 |
50 | =item $c->benchmark($coderef) |
fc7ec1d9 |
51 | |
52 | Takes a coderef with arguments and returns elapsed time as float. |
53 | |
54 | my ( $elapsed, $status ) = $c->benchmark( sub { return 1 } ); |
55 | $c->log->info( sprintf "Processing took %f seconds", $elapsed ); |
56 | |
57 | =cut |
58 | |
59 | sub benchmark { |
60 | my $c = shift; |
61 | my $code = shift; |
62 | my $time = [gettimeofday]; |
63 | my @return = &$code(@_); |
64 | my $elapsed = tv_interval $time; |
65 | return wantarray ? ( $elapsed, @return ) : $elapsed; |
66 | } |
67 | |
23f9d934 |
68 | =item $c->comp($name) |
69 | |
70 | =item $c->component($name) |
fc7ec1d9 |
71 | |
72 | Get a component object by name. |
73 | |
74 | $c->comp('MyApp::Model::MyModel')->do_stuff; |
75 | |
76 | Regex search for a component. |
77 | |
78 | $c->comp('mymodel')->do_stuff; |
79 | |
80 | =cut |
81 | |
82 | sub component { |
83 | my ( $c, $name ) = @_; |
99fe1710 |
84 | |
fc7ec1d9 |
85 | if ( my $component = $c->components->{$name} ) { |
86 | return $component; |
87 | } |
99fe1710 |
88 | |
fc7ec1d9 |
89 | else { |
90 | for my $component ( keys %{ $c->components } ) { |
91 | return $c->components->{$component} if $component =~ /$name/i; |
92 | } |
93 | } |
99fe1710 |
94 | |
fc7ec1d9 |
95 | } |
96 | |
a554cc3b |
97 | =item $c->error |
23f9d934 |
98 | |
a554cc3b |
99 | =item $c->error($error, ...) |
23f9d934 |
100 | |
a554cc3b |
101 | =item $c->error($arrayref) |
fc7ec1d9 |
102 | |
a554cc3b |
103 | Returns an arrayref containing error messages. |
fc7ec1d9 |
104 | |
a554cc3b |
105 | my @error = @{ $c->error }; |
fc7ec1d9 |
106 | |
107 | Add a new error. |
108 | |
a554cc3b |
109 | $c->error('Something bad happened'); |
fc7ec1d9 |
110 | |
111 | =cut |
112 | |
a554cc3b |
113 | sub error { |
fc7ec1d9 |
114 | my $c = shift; |
a554cc3b |
115 | my $error = ref $_[0] eq 'ARRAY' ? $_[0] : [@_]; |
116 | push @{ $c->{error} }, @$error; |
117 | return $c->{error}; |
fc7ec1d9 |
118 | } |
119 | |
6dc87a0f |
120 | =item $c->execute($class, $coderef) |
121 | |
122 | Execute a coderef in given class and catch exceptions. |
123 | Errors are available via $c->error. |
124 | |
125 | =cut |
126 | |
127 | sub execute { |
128 | my ( $c, $class, $code ) = @_; |
91571b7b |
129 | $class = $c->components->{$class} || $class; |
6dc87a0f |
130 | $c->state(0); |
39de91b0 |
131 | my $callsub = ( caller(1) )[3]; |
99fe1710 |
132 | |
6dc87a0f |
133 | eval { |
134 | if ( $c->debug ) |
135 | { |
136 | my $action = $c->actions->{reverse}->{"$code"}; |
137 | $action = "/$action" unless $action =~ /\-\>/; |
fb13403c |
138 | $action = "-> $action" if $callsub =~ /forward$/; |
6dc87a0f |
139 | my ( $elapsed, @state ) = |
140 | $c->benchmark( $code, $class, $c, @{ $c->req->args } ); |
0f7ecc53 |
141 | push @{ $c->{stats} }, [ $action, sprintf( '%fs', $elapsed ) ]; |
6dc87a0f |
142 | $c->state(@state); |
143 | } |
144 | else { $c->state( &$code( $class, $c, @{ $c->req->args } ) ) } |
145 | }; |
99fe1710 |
146 | |
6dc87a0f |
147 | if ( my $error = $@ ) { |
b9ffe28b |
148 | |
149 | unless ( ref $error ) { |
150 | chomp $error; |
151 | $error = qq/Caught exception "$error"/; |
152 | } |
153 | |
6dc87a0f |
154 | $c->log->error($error); |
b9ffe28b |
155 | $c->error($error); |
6dc87a0f |
156 | $c->state(0); |
157 | } |
158 | return $c->state; |
159 | } |
160 | |
23f9d934 |
161 | =item $c->finalize |
fc7ec1d9 |
162 | |
ca39d576 |
163 | Finalize request. |
fc7ec1d9 |
164 | |
165 | =cut |
166 | |
167 | sub finalize { |
168 | my $c = shift; |
23f9d934 |
169 | |
6dc87a0f |
170 | $c->finalize_cookies; |
171 | |
49490aab |
172 | if ( my $location = $c->response->redirect ) { |
23f9d934 |
173 | $c->log->debug(qq/Redirecting to "$location"/) if $c->debug; |
6dc87a0f |
174 | $c->response->header( Location => $location ); |
e7c0c583 |
175 | $c->response->status(302) if $c->response->status !~ /^3\d\d$/; |
6dc87a0f |
176 | } |
177 | |
969647fd |
178 | if ( $#{ $c->error } >= 0 ) { |
179 | $c->finalize_error; |
23f9d934 |
180 | } |
181 | |
8fbcd90c |
182 | if ( !$c->response->body_length && $c->response->status !~ /^(1|3)\d\d$/ ) { |
969647fd |
183 | $c->finalize_error; |
184 | } |
fc7ec1d9 |
185 | |
8fbcd90c |
186 | if ( $c->response->body_length && !$c->response->content_length ) { |
187 | $c->response->content_length( $c->response->body_length ); |
fc7ec1d9 |
188 | } |
969647fd |
189 | |
fc7ec1d9 |
190 | my $status = $c->finalize_headers; |
06e1b616 |
191 | $c->finalize_body; |
fc7ec1d9 |
192 | return $status; |
193 | } |
194 | |
c539a1c3 |
195 | =item $c->finalize_output |
196 | |
197 | alias to finalize_body |
198 | |
06e1b616 |
199 | =item $c->finalize_body |
200 | |
201 | Finalize body. |
202 | |
203 | =cut |
204 | |
205 | sub finalize_body { } |
206 | |
6dc87a0f |
207 | =item $c->finalize_cookies |
208 | |
209 | Finalize cookies. |
210 | |
211 | =cut |
212 | |
213 | sub finalize_cookies { |
214 | my $c = shift; |
215 | |
216 | while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) { |
217 | my $cookie = CGI::Cookie->new( |
218 | -name => $name, |
219 | -value => $cookie->{value}, |
220 | -expires => $cookie->{expires}, |
221 | -domain => $cookie->{domain}, |
222 | -path => $cookie->{path}, |
223 | -secure => $cookie->{secure} || 0 |
224 | ); |
225 | |
226 | $c->res->headers->push_header( 'Set-Cookie' => $cookie->as_string ); |
227 | } |
228 | } |
229 | |
969647fd |
230 | =item $c->finalize_error |
231 | |
ca39d576 |
232 | Finalize error. |
969647fd |
233 | |
234 | =cut |
235 | |
236 | sub finalize_error { |
237 | my $c = shift; |
238 | |
239 | $c->res->headers->content_type('text/html'); |
240 | my $name = $c->config->{name} || 'Catalyst Application'; |
241 | |
242 | my ( $title, $error, $infos ); |
243 | if ( $c->debug ) { |
244 | $error = join '<br/>', @{ $c->error }; |
245 | $error ||= 'No output'; |
246 | $title = $name = "$name on Catalyst $Catalyst::VERSION"; |
247 | my $req = encode_entities Dumper $c->req; |
248 | my $res = encode_entities Dumper $c->res; |
249 | my $stash = encode_entities Dumper $c->stash; |
250 | $infos = <<""; |
251 | <br/> |
252 | <b><u>Request</u></b><br/> |
253 | <pre>$req</pre> |
254 | <b><u>Response</u></b><br/> |
255 | <pre>$res</pre> |
256 | <b><u>Stash</u></b><br/> |
257 | <pre>$stash</pre> |
258 | |
259 | } |
260 | else { |
261 | $title = $name; |
262 | $error = ''; |
263 | $infos = <<""; |
264 | <pre> |
265 | (en) Please come back later |
266 | (de) Bitte versuchen sie es spaeter nocheinmal |
267 | (nl) Gelieve te komen later terug |
268 | (no) Vennligst prov igjen senere |
269 | (fr) Veuillez revenir plus tard |
270 | (es) Vuelto por favor mas adelante |
271 | (pt) Voltado por favor mais tarde |
272 | (it) Ritornato prego più successivamente |
273 | </pre> |
274 | |
275 | $name = ''; |
276 | } |
e060fe05 |
277 | $c->res->body( <<"" ); |
969647fd |
278 | <html> |
279 | <head> |
280 | <title>$title</title> |
281 | <style type="text/css"> |
282 | body { |
283 | font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana, |
284 | Tahoma, Arial, helvetica, sans-serif; |
285 | color: #ddd; |
286 | background-color: #eee; |
287 | margin: 0px; |
288 | padding: 0px; |
289 | } |
290 | div.box { |
291 | background-color: #ccc; |
292 | border: 1px solid #aaa; |
293 | padding: 4px; |
294 | margin: 10px; |
295 | -moz-border-radius: 10px; |
296 | } |
297 | div.error { |
298 | background-color: #977; |
299 | border: 1px solid #755; |
300 | padding: 8px; |
301 | margin: 4px; |
302 | margin-bottom: 10px; |
303 | -moz-border-radius: 10px; |
304 | } |
305 | div.infos { |
306 | background-color: #797; |
307 | border: 1px solid #575; |
308 | padding: 8px; |
309 | margin: 4px; |
310 | margin-bottom: 10px; |
311 | -moz-border-radius: 10px; |
312 | } |
313 | div.name { |
314 | background-color: #779; |
315 | border: 1px solid #557; |
316 | padding: 8px; |
317 | margin: 4px; |
318 | -moz-border-radius: 10px; |
319 | } |
320 | </style> |
321 | </head> |
322 | <body> |
323 | <div class="box"> |
324 | <div class="error">$error</div> |
325 | <div class="infos">$infos</div> |
326 | <div class="name">$name</div> |
327 | </div> |
328 | </body> |
329 | </html> |
330 | |
331 | } |
332 | |
23f9d934 |
333 | =item $c->finalize_headers |
fc7ec1d9 |
334 | |
ca39d576 |
335 | Finalize headers. |
fc7ec1d9 |
336 | |
337 | =cut |
338 | |
339 | sub finalize_headers { } |
340 | |
4f81d9ba |
341 | =item $c->handler( $class, $engine ) |
fc7ec1d9 |
342 | |
ca39d576 |
343 | Handles the request. |
fc7ec1d9 |
344 | |
345 | =cut |
346 | |
6dc87a0f |
347 | sub handler { |
348 | my ( $class, $engine ) = @_; |
fc7ec1d9 |
349 | |
350 | # Always expect worst case! |
351 | my $status = -1; |
352 | eval { |
d41516b2 |
353 | my @stats = (); |
99fe1710 |
354 | |
fc7ec1d9 |
355 | my $handler = sub { |
6dc87a0f |
356 | my $c = $class->prepare($engine); |
d41516b2 |
357 | $c->{stats} = \@stats; |
63b763c5 |
358 | $c->dispatch; |
fc7ec1d9 |
359 | return $c->finalize; |
360 | }; |
99fe1710 |
361 | |
fc7ec1d9 |
362 | if ( $class->debug ) { |
363 | my $elapsed; |
364 | ( $elapsed, $status ) = $class->benchmark($handler); |
365 | $elapsed = sprintf '%f', $elapsed; |
6ddb9f01 |
366 | my $av = sprintf '%.3f', ( $elapsed == 0 ? '??' : (1 / $elapsed) ); |
0f7ecc53 |
367 | my $t = Text::ASCIITable->new; |
368 | $t->setCols( 'Action', 'Time' ); |
3f36a3a3 |
369 | $t->setColWidth( 'Action', 64, 1 ); |
370 | $t->setColWidth( 'Time', 9, 1 ); |
0822f9a4 |
371 | |
cd677e12 |
372 | for my $stat (@stats) { $t->addRow( $stat->[0], $stat->[1] ) } |
0f7ecc53 |
373 | $class->log->info( "Request took $elapsed" . "s ($av/s)", |
374 | $t->draw ); |
fc7ec1d9 |
375 | } |
376 | else { $status = &$handler } |
99fe1710 |
377 | |
fc7ec1d9 |
378 | }; |
99fe1710 |
379 | |
fc7ec1d9 |
380 | if ( my $error = $@ ) { |
381 | chomp $error; |
382 | $class->log->error(qq/Caught exception in engine "$error"/); |
383 | } |
99fe1710 |
384 | |
fc7ec1d9 |
385 | $COUNT++; |
386 | return $status; |
387 | } |
388 | |
3f822a28 |
389 | =item $c->prepare($engine) |
fc7ec1d9 |
390 | |
a554cc3b |
391 | Turns the engine-specific request( Apache, CGI ... ) |
392 | into a Catalyst context . |
fc7ec1d9 |
393 | |
394 | =cut |
395 | |
396 | sub prepare { |
4f81d9ba |
397 | my ( $class, $engine ) = @_; |
99fe1710 |
398 | |
fc7ec1d9 |
399 | my $c = bless { |
400 | request => Catalyst::Request->new( |
401 | { |
402 | arguments => [], |
8fbcd90c |
403 | body => undef, |
fc7ec1d9 |
404 | cookies => {}, |
405 | headers => HTTP::Headers->new, |
406 | parameters => {}, |
407 | snippets => [], |
408 | uploads => {} |
409 | } |
410 | ), |
411 | response => Catalyst::Response->new( |
8fbcd90c |
412 | { |
413 | body => undef, |
414 | cookies => {}, |
415 | headers => HTTP::Headers->new, |
416 | status => 200 |
417 | } |
fc7ec1d9 |
418 | ), |
b768faa3 |
419 | stash => {}, |
420 | state => 0 |
fc7ec1d9 |
421 | }, $class; |
99fe1710 |
422 | |
fc7ec1d9 |
423 | if ( $c->debug ) { |
424 | my $secs = time - $START || 1; |
425 | my $av = sprintf '%.3f', $COUNT / $secs; |
1a0250cb |
426 | $c->log->debug('**********************************'); |
fc7ec1d9 |
427 | $c->log->debug("* Request $COUNT ($av/s) [$$]"); |
1a0250cb |
428 | $c->log->debug('**********************************'); |
fc7ec1d9 |
429 | $c->res->headers->header( 'X-Catalyst' => $Catalyst::VERSION ); |
430 | } |
99fe1710 |
431 | |
4f81d9ba |
432 | $c->prepare_request($engine); |
8fbcd90c |
433 | $c->prepare_connection; |
ac733264 |
434 | $c->prepare_headers; |
1a80619d |
435 | $c->prepare_cookies; |
8fbcd90c |
436 | $c->prepare_path; |
06e1b616 |
437 | $c->prepare_action; |
99fe1710 |
438 | |
0556eb49 |
439 | my $method = $c->req->method || ''; |
440 | my $path = $c->req->path || ''; |
441 | my $hostname = $c->req->hostname || ''; |
442 | my $address = $c->req->address || ''; |
06e1b616 |
443 | |
0556eb49 |
444 | $c->log->debug(qq/"$method" request for "$path" from $hostname($address)/) |
445 | if $c->debug; |
99fe1710 |
446 | |
06e1b616 |
447 | if ( $c->request->method eq 'POST' and $c->request->content_length ) { |
448 | |
449 | if ( $c->req->content_type eq 'application/x-www-form-urlencoded' ) { |
450 | $c->prepare_parameters; |
451 | } |
452 | elsif ( $c->req->content_type eq 'multipart/form-data' ) { |
453 | $c->prepare_parameters; |
454 | $c->prepare_uploads; |
455 | } |
456 | else { |
457 | $c->prepare_body; |
458 | } |
459 | } |
460 | |
461 | if ( $c->request->method eq 'GET' ) { |
462 | $c->prepare_parameters; |
463 | } |
c85ff642 |
464 | |
465 | if ( $c->debug && keys %{ $c->req->params } ) { |
0f7ecc53 |
466 | my $t = Text::ASCIITable->new; |
467 | $t->setCols( 'Key', 'Value' ); |
0822f9a4 |
468 | $t->setColWidth( 'Key', 37, 1 ); |
469 | $t->setColWidth( 'Value', 36, 1 ); |
f78172f1 |
470 | for my $key ( sort keys %{ $c->req->params } ) { |
6d1ab915 |
471 | my $param = $c->req->params->{$key}; |
472 | my $value = defined($param) ? $param : ''; |
cd677e12 |
473 | $t->addRow( $key, $value ); |
c85ff642 |
474 | } |
0f7ecc53 |
475 | $c->log->debug( 'Parameters are', $t->draw ); |
c85ff642 |
476 | } |
99fe1710 |
477 | |
fc7ec1d9 |
478 | return $c; |
479 | } |
480 | |
23f9d934 |
481 | =item $c->prepare_action |
fc7ec1d9 |
482 | |
ca39d576 |
483 | Prepare action. |
fc7ec1d9 |
484 | |
485 | =cut |
486 | |
487 | sub prepare_action { |
488 | my $c = shift; |
489 | my $path = $c->req->path; |
490 | my @path = split /\//, $c->req->path; |
491 | $c->req->args( \my @args ); |
99fe1710 |
492 | |
fc7ec1d9 |
493 | while (@path) { |
7833fdfc |
494 | $path = join '/', @path; |
0169d3a8 |
495 | if ( my $result = ${ $c->get_action($path) }[0] ) { |
fc7ec1d9 |
496 | |
497 | # It's a regex |
498 | if ($#$result) { |
7e5adedd |
499 | my $match = $result->[1]; |
500 | my @snippets = @{ $result->[2] }; |
81f6fc50 |
501 | $c->log->debug( |
502 | qq/Requested action is "$path" and matched "$match"/) |
fc7ec1d9 |
503 | if $c->debug; |
504 | $c->log->debug( |
505 | 'Snippets are "' . join( ' ', @snippets ) . '"' ) |
506 | if ( $c->debug && @snippets ); |
507 | $c->req->action($match); |
508 | $c->req->snippets( \@snippets ); |
509 | } |
99fe1710 |
510 | |
fc7ec1d9 |
511 | else { |
512 | $c->req->action($path); |
81f6fc50 |
513 | $c->log->debug(qq/Requested action is "$path"/) if $c->debug; |
fc7ec1d9 |
514 | } |
99fe1710 |
515 | |
fc7ec1d9 |
516 | $c->req->match($path); |
fc7ec1d9 |
517 | last; |
518 | } |
519 | unshift @args, pop @path; |
520 | } |
99fe1710 |
521 | |
fc7ec1d9 |
522 | unless ( $c->req->action ) { |
ac733264 |
523 | $c->req->action('default'); |
87e67021 |
524 | $c->req->match(''); |
fc7ec1d9 |
525 | } |
99fe1710 |
526 | |
5783a9a5 |
527 | $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' ) |
528 | if ( $c->debug && @args ); |
fc7ec1d9 |
529 | } |
530 | |
06e1b616 |
531 | =item $c->prepare_body |
532 | |
533 | Prepare message body. |
534 | |
535 | =cut |
536 | |
537 | sub prepare_body { } |
538 | |
c9afa5fc |
539 | =item $c->prepare_connection |
0556eb49 |
540 | |
ca39d576 |
541 | Prepare connection. |
0556eb49 |
542 | |
543 | =cut |
544 | |
545 | sub prepare_connection { } |
546 | |
c9afa5fc |
547 | =item $c->prepare_cookies |
fc7ec1d9 |
548 | |
ca39d576 |
549 | Prepare cookies. |
fc7ec1d9 |
550 | |
551 | =cut |
552 | |
6dc87a0f |
553 | sub prepare_cookies { |
554 | my $c = shift; |
555 | |
556 | if ( my $header = $c->request->header('Cookie') ) { |
557 | $c->req->cookies( { CGI::Cookie->parse($header) } ); |
558 | } |
559 | } |
fc7ec1d9 |
560 | |
23f9d934 |
561 | =item $c->prepare_headers |
fc7ec1d9 |
562 | |
ca39d576 |
563 | Prepare headers. |
fc7ec1d9 |
564 | |
565 | =cut |
566 | |
567 | sub prepare_headers { } |
568 | |
23f9d934 |
569 | =item $c->prepare_parameters |
fc7ec1d9 |
570 | |
ca39d576 |
571 | Prepare parameters. |
fc7ec1d9 |
572 | |
573 | =cut |
574 | |
575 | sub prepare_parameters { } |
576 | |
23f9d934 |
577 | =item $c->prepare_path |
fc7ec1d9 |
578 | |
ca39d576 |
579 | Prepare path and base. |
fc7ec1d9 |
580 | |
581 | =cut |
582 | |
583 | sub prepare_path { } |
584 | |
23f9d934 |
585 | =item $c->prepare_request |
fc7ec1d9 |
586 | |
ca39d576 |
587 | Prepare the engine request. |
fc7ec1d9 |
588 | |
589 | =cut |
590 | |
591 | sub prepare_request { } |
592 | |
23f9d934 |
593 | =item $c->prepare_uploads |
fc7ec1d9 |
594 | |
ca39d576 |
595 | Prepare uploads. |
fc7ec1d9 |
596 | |
597 | =cut |
598 | |
599 | sub prepare_uploads { } |
600 | |
c9afa5fc |
601 | =item $c->run |
602 | |
ca39d576 |
603 | Starts the engine. |
c9afa5fc |
604 | |
605 | =cut |
606 | |
607 | sub run { } |
608 | |
61b1e958 |
609 | =item $c->request |
fc7ec1d9 |
610 | |
ca39d576 |
611 | =item $c->req |
23f9d934 |
612 | |
ca39d576 |
613 | Returns a C<Catalyst::Request> object. |
fc7ec1d9 |
614 | |
ca39d576 |
615 | my $req = $c->req; |
61b1e958 |
616 | |
617 | =item $c->response |
618 | |
ca39d576 |
619 | =item $c->res |
620 | |
fc7ec1d9 |
621 | Returns a C<Catalyst::Response> object. |
622 | |
623 | my $res = $c->res; |
624 | |
23f9d934 |
625 | =item $class->setup |
fc7ec1d9 |
626 | |
ca39d576 |
627 | Setup. |
fc7ec1d9 |
628 | |
629 | MyApp->setup; |
630 | |
631 | =cut |
632 | |
633 | sub setup { |
634 | my $self = shift; |
635 | $self->setup_components; |
636 | if ( $self->debug ) { |
637 | my $name = $self->config->{name} || 'Application'; |
638 | $self->log->info("$name powered by Catalyst $Catalyst::VERSION"); |
639 | } |
640 | } |
641 | |
23f9d934 |
642 | =item $class->setup_components |
fc7ec1d9 |
643 | |
ca39d576 |
644 | Setup components. |
fc7ec1d9 |
645 | |
646 | =cut |
647 | |
648 | sub setup_components { |
649 | my $self = shift; |
650 | |
651 | # Components |
652 | my $class = ref $self || $self; |
653 | eval <<""; |
654 | package $class; |
655 | import Module::Pluggable::Fast |
656 | name => '_components', |
657 | search => [ |
658 | '$class\::Controller', '$class\::C', |
659 | '$class\::Model', '$class\::M', |
660 | '$class\::View', '$class\::V' |
661 | ]; |
662 | |
663 | if ( my $error = $@ ) { |
664 | chomp $error; |
f88238ea |
665 | die qq/Couldn't load components "$error"/; |
fc7ec1d9 |
666 | } |
99fe1710 |
667 | |
fc7ec1d9 |
668 | $self->components( {} ); |
1abd6db7 |
669 | my @comps; |
ac733264 |
670 | for my $comp ( $self->_components($self) ) { |
671 | $self->components->{ ref $comp } = $comp; |
1abd6db7 |
672 | push @comps, $comp; |
4cf083b1 |
673 | } |
99fe1710 |
674 | |
5fbed090 |
675 | my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } ); |
676 | $t->setCols('Class'); |
677 | $t->setColWidth( 'Class', 75, 1 ); |
cd677e12 |
678 | $t->addRow($_) for keys %{ $self->components }; |
5fbed090 |
679 | $self->log->debug( 'Loaded components', $t->draw ) |
680 | if ( @{ $t->{tbl_rows} } && $self->debug ); |
99fe1710 |
681 | |
1abd6db7 |
682 | $self->setup_actions( [ $self, @comps ] ); |
fc7ec1d9 |
683 | } |
684 | |
63b763c5 |
685 | =item $c->state |
686 | |
687 | Contains the return value of the last executed action. |
688 | |
23f9d934 |
689 | =item $c->stash |
fc7ec1d9 |
690 | |
ca39d576 |
691 | Returns a hashref containing all your data. |
fc7ec1d9 |
692 | |
693 | $c->stash->{foo} ||= 'yada'; |
694 | print $c->stash->{foo}; |
695 | |
696 | =cut |
697 | |
698 | sub stash { |
699 | my $self = shift; |
700 | if ( $_[0] ) { |
701 | my $stash = $_[1] ? {@_} : $_[0]; |
702 | while ( my ( $key, $val ) = each %$stash ) { |
703 | $self->{stash}->{$key} = $val; |
704 | } |
705 | } |
706 | return $self->{stash}; |
707 | } |
708 | |
23f9d934 |
709 | =back |
710 | |
fc7ec1d9 |
711 | =head1 AUTHOR |
712 | |
713 | Sebastian Riedel, C<sri@cpan.org> |
714 | |
715 | =head1 COPYRIGHT |
716 | |
717 | This program is free software, you can redistribute it and/or modify it under |
718 | the same terms as Perl itself. |
719 | |
720 | =cut |
721 | |
722 | 1; |