Fixed Catalyst pod
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
CommitLineData
68a748b9 1package Catalyst::Dispatcher;
1abd6db7 2
3use strict;
4use base 'Class::Data::Inheritable';
f05af9ba 5use Catalyst::Utils;
1abd6db7 6use Text::ASCIITable;
1abd6db7 7use Tree::Simple;
8use Tree::Simple::Visitor::FindByPath;
9
10__PACKAGE__->mk_classdata($_) for qw/actions tree/;
11
1abd6db7 12=head1 NAME
13
9c053379 14Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 15
16=head1 SYNOPSIS
17
18See L<Catalyst>.
19
20=head1 DESCRIPTION
21
22=head1 METHODS
23
24=over 4
25
6ef62eb2 26=item $c->detach($command)
27
28Like C<forward> but doesn't return.
29
30=cut
31
32sub detach {
33 my ( $c, $command ) = @_;
34 $c->forward($command) if $command;
35 die $Catalyst::Engine::DETACH;
36}
37
1abd6db7 38=item $c->dispatch
39
40Dispatch request to actions.
41
42=cut
43
44sub dispatch {
45 my $c = shift;
46 my $action = $c->req->action;
47 my $namespace = '';
48 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
49 if $action eq 'default';
99fe1710 50
1abd6db7 51 unless ($namespace) {
52 if ( my $result = $c->get_action($action) ) {
e494bd6b 53 $namespace = Catalyst::Utils::class2prefix( $result->[0]->[0]->[0],
54 $c->config->{case_sensitive} );
1abd6db7 55 }
56 }
99fe1710 57
1abd6db7 58 my $default = $action eq 'default' ? $namespace : undef;
2d16b61a 59 my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
1abd6db7 60 $namespace ||= '/';
99fe1710 61
1abd6db7 62 if ( @{$results} ) {
63
64 # Execute last begin
65 $c->state(1);
2d16b61a 66 if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-1] ) {
1abd6db7 67 $c->execute( @{ $begin->[0] } );
68 return if scalar @{ $c->error };
69 }
70
71 # Execute the auto chain
1196a46d 72 my $autorun = 0;
9ddd9d05 73 for my $auto ( @{ $c->get_action( 'auto', $namespace, 1 ) } ) {
74 $autorun++;
1abd6db7 75 $c->execute( @{ $auto->[0] } );
76 return if scalar @{ $c->error };
77 last unless $c->state;
78 }
79
80 # Execute the action or last default
e5f21aa2 81 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
61cfdd57 82 if ( ( my $action = $c->req->action ) && $mkay ) {
2d16b61a 83 if ( my $result = @{ $c->get_action( $action, $default, 1 ) }[-1] )
84 {
1abd6db7 85 $c->execute( @{ $result->[0] } );
86 }
87 }
88
89 # Execute last end
2d16b61a 90 if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
1abd6db7 91 $c->execute( @{ $end->[0] } );
92 return if scalar @{ $c->error };
93 }
94 }
99fe1710 95
1abd6db7 96 else {
97 my $path = $c->req->path;
98 my $error = $path
99 ? qq/Unknown resource "$path"/
100 : "No default action defined";
101 $c->log->error($error) if $c->debug;
102 $c->error($error);
103 }
104}
105
106=item $c->forward($command)
107
108Forward processing to a private action or a method from a class.
109If you define a class without method it will default to process().
110
111 $c->forward('/foo');
112 $c->forward('index');
113 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
114 $c->forward('MyApp::View::TT');
115
116=cut
117
118sub forward {
119 my $c = shift;
120 my $command = shift;
99fe1710 121
1abd6db7 122 unless ($command) {
123 $c->log->debug('Nothing to forward to') if $c->debug;
124 return 0;
125 }
99fe1710 126
1abd6db7 127 my $caller = caller(0);
128 my $namespace = '/';
99fe1710 129
1abd6db7 130 if ( $command =~ /^\// ) {
db0ca23c 131 $command =~ /^\/(.*)\/(\w+)$/;
1abd6db7 132 $namespace = $1 || '/';
db0ca23c 133 $command = $2 || $command;
134 $command =~ s/^\///;
1abd6db7 135 }
99fe1710 136
e494bd6b 137 else {
138 $namespace =
139 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
140 || '/';
141 }
99fe1710 142
1abd6db7 143 my $results = $c->get_action( $command, $namespace );
99fe1710 144
1abd6db7 145 unless ( @{$results} ) {
cd677e12 146 my $class = $command || '';
812a28c9 147 my $path = $class . '.pm';
fe14cf78 148 $path =~ s/::/\//g;
99fe1710 149
812a28c9 150 unless ( $INC{$path} ) {
151 my $error =
152 qq/Couldn't forward to "$class". Invalid or not loaded./;
3b2ed580 153 $c->error($error);
154 $c->log->debug($error) if $c->debug;
1abd6db7 155 return 0;
156 }
812a28c9 157
3245f607 158 unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
812a28c9 159 my $error =
160 qq/Can't forward to "$class". Class is not a Catalyst component./;
3245f607 161 $c->error($error);
162 $c->log->debug($error) if $c->debug;
812a28c9 163 return 0;
3245f607 164 }
99fe1710 165
1abd6db7 166 my $method = shift || 'process';
99fe1710 167
1abd6db7 168 if ( my $code = $class->can($method) ) {
169 $c->actions->{reverse}->{"$code"} = "$class->$method";
170 $results = [ [ [ $class, $code ] ] ];
171 }
99fe1710 172
1abd6db7 173 else {
812a28c9 174 my $error =
175 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 176 $c->error($error);
177 $c->log->debug($error)
1abd6db7 178 if $c->debug;
179 return 0;
180 }
99fe1710 181
1abd6db7 182 }
99fe1710 183
1abd6db7 184 for my $result ( @{$results} ) {
185 $c->execute( @{ $result->[0] } );
186 return if scalar @{ $c->error };
187 last unless $c->state;
188 }
99fe1710 189
1abd6db7 190 return $c->state;
191}
192
2d16b61a 193=item $c->get_action( $action, $namespace, $inherit )
1abd6db7 194
195Get an action in a given namespace.
196
197=cut
198
199sub get_action {
2d16b61a 200 my ( $c, $action, $namespace, $inherit ) = @_;
1abd6db7 201 return [] unless $action;
202 $namespace ||= '';
2d16b61a 203 $inherit ||= 0;
99fe1710 204
1abd6db7 205 if ($namespace) {
206 $namespace = '' if $namespace eq '/';
207 my $parent = $c->tree;
208 my @results;
99fe1710 209
2d16b61a 210 if ($inherit) {
1abd6db7 211 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
212 push @results, [$result] if $result;
213 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 214
1abd6db7 215 for my $part ( split '/', $namespace ) {
216 $visitor->setSearchPath($part);
217 $parent->accept($visitor);
218 my $child = $visitor->getResult;
219 my $uid = $child->getUID if $child;
220 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
221 push @results, [$match] if $match;
222 $parent = $child if $child;
223 }
99fe1710 224
1abd6db7 225 }
99fe1710 226
1abd6db7 227 else {
99fe1710 228
1abd6db7 229 if ($namespace) {
230 my $visitor = Tree::Simple::Visitor::FindByPath->new;
231 $visitor->setSearchPath( split '/', $namespace );
232 $parent->accept($visitor);
233 my $child = $visitor->getResult;
234 my $uid = $child->getUID if $child;
235 my $match = $c->actions->{private}->{$uid}->{$action}
236 if $uid;
237 push @results, [$match] if $match;
238 }
99fe1710 239
1abd6db7 240 else {
241 my $result =
242 $c->actions->{private}->{ $parent->getUID }->{$action};
243 push @results, [$result] if $result;
244 }
99fe1710 245
1abd6db7 246 }
247 return \@results;
248 }
99fe1710 249
1abd6db7 250 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
251 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
99fe1710 252
1abd6db7 253 else {
99fe1710 254
1abd6db7 255 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
256 my $name = $c->actions->{compiled}->[$i]->[0];
257 my $regex = $c->actions->{compiled}->[$i]->[1];
99fe1710 258
2d752b2a 259 if ( my @snippets = ( $action =~ $regex ) ) {
1abd6db7 260 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
261 }
99fe1710 262
1abd6db7 263 }
264 }
265 return [];
266}
267
268=item $c->set_action( $action, $code, $namespace, $attrs )
269
270Set an action in a given namespace.
271
272=cut
273
274sub set_action {
275 my ( $c, $method, $code, $namespace, $attrs ) = @_;
276
e494bd6b 277 my $prefix =
278 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
279 || '';
1abd6db7 280 my %flags;
281
282 for my $attr ( @{$attrs} ) {
0299ba22 283 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
284 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
285 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { $flags{path} = $1 }
286 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
287 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
288 $flags{regex} = $2;
289 }
1abd6db7 290 }
291
d0e524d2 292 if ( $flags{private} && ( keys %flags > 1 ) ) {
8d4e224b 293 $c->log->debug( 'Bad action definition "'
d0e524d2 294 . join( ' ', @{$attrs} )
8d4e224b 295 . qq/" for "$namespace->$method"/ )
296 if $c->debug;
d0e524d2 297 return;
298 }
1abd6db7 299 return unless keys %flags;
300
301 my $parent = $c->tree;
302 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 303
1abd6db7 304 for my $part ( split '/', $prefix ) {
305 $visitor->setSearchPath($part);
306 $parent->accept($visitor);
307 my $child = $visitor->getResult;
99fe1710 308
1abd6db7 309 unless ($child) {
310 $child = $parent->addChild( Tree::Simple->new($part) );
311 $visitor->setSearchPath($part);
312 $parent->accept($visitor);
313 $child = $visitor->getResult;
314 }
99fe1710 315
1abd6db7 316 $parent = $child;
317 }
99fe1710 318
1abd6db7 319 my $uid = $parent->getUID;
320 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
321 my $forward = $prefix ? "$prefix/$method" : $method;
322
323 if ( $flags{path} ) {
324 $flags{path} =~ s/^\w+//;
325 $flags{path} =~ s/\w+$//;
326 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
327 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
328 }
99fe1710 329
1abd6db7 330 if ( $flags{regex} ) {
331 $flags{regex} =~ s/^\w+//;
332 $flags{regex} =~ s/\w+$//;
333 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
334 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
335 }
336
337 my $reverse = $prefix ? "$prefix/$method" : $method;
338
339 if ( $flags{local} || $flags{global} || $flags{path} ) {
99fe1710 340 my $path = $flags{path} || $method;
1abd6db7 341 my $absolute = 0;
99fe1710 342
1abd6db7 343 if ( $path =~ /^\/(.+)/ ) {
344 $path = $1;
345 $absolute = 1;
346 }
99fe1710 347
1abd6db7 348 $absolute = 1 if $flags{global};
349 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
350 $c->actions->{plain}->{$name} = [ $namespace, $code ];
351 }
99fe1710 352
1abd6db7 353 if ( my $regex = $flags{regex} ) {
354 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
355 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
356 }
357
358 $c->actions->{reverse}->{"$code"} = $reverse;
359}
360
361=item $class->setup_actions($component)
362
363Setup actions for a component.
364
365=cut
366
367sub setup_actions {
a268a011 368 my $self = shift;
99fe1710 369
12e28165 370 # These are the core structures
371 $self->actions(
372 {
373 plain => {},
374 private => {},
375 regex => {},
376 compiled => [],
377 reverse => {}
378 }
379 );
380
381 # We use a tree
382 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
e494bd6b 383
a268a011 384 for my $comp ( keys %{ $self->components } ) {
e494bd6b 385
386 # We only setup components that inherit from Catalyst::Base
a268a011 387 next unless $comp->isa('Catalyst::Base');
99fe1710 388
812a28c9 389 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
1abd6db7 390 my ( $code, $attrs ) = @{$action};
391 my $name = '';
392 no strict 'refs';
393 my @cache = ( $comp, @{"$comp\::ISA"} );
394 my %namespaces;
99fe1710 395
1abd6db7 396 while ( my $namespace = shift @cache ) {
397 $namespaces{$namespace}++;
398 for my $isa ( @{"$comp\::ISA"} ) {
399 next if $namespaces{$isa};
400 push @cache, $isa;
401 $namespaces{$isa}++;
402 }
403 }
99fe1710 404
1abd6db7 405 for my $namespace ( keys %namespaces ) {
99fe1710 406
1abd6db7 407 for my $sym ( values %{ $namespace . '::' } ) {
99fe1710 408
1abd6db7 409 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
99fe1710 410
1abd6db7 411 $name = *{$sym}{NAME};
412 $self->set_action( $name, $code, $comp, $attrs );
413 last;
414 }
99fe1710 415
1abd6db7 416 }
99fe1710 417
1abd6db7 418 }
99fe1710 419
1abd6db7 420 }
99fe1710 421
1abd6db7 422 }
e494bd6b 423
a268a011 424 return unless $self->debug;
99fe1710 425
1abd6db7 426 my $actions = $self->actions;
427 my $privates = Text::ASCIITable->new;
5fbed090 428 $privates->setCols( 'Private', 'Class' );
429 $privates->setColWidth( 'Private', 36, 1 );
430 $privates->setColWidth( 'Class', 37, 1 );
99fe1710 431
1abd6db7 432 my $walker = sub {
433 my ( $walker, $parent, $prefix ) = @_;
434 $prefix .= $parent->getNodeValue || '';
435 $prefix .= '/' unless $prefix =~ /\/$/;
436 my $uid = $parent->getUID;
99fe1710 437
1abd6db7 438 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
439 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
d36e81fd 440 $privates->addRow( "$prefix$action", $class );
1abd6db7 441 }
99fe1710 442
1abd6db7 443 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
444 };
99fe1710 445
1abd6db7 446 $walker->( $walker, $self->tree, '' );
447 $self->log->debug( 'Loaded private actions', $privates->draw )
a268a011 448 if ( @{ $privates->{tbl_rows} } );
99fe1710 449
1abd6db7 450 my $publics = Text::ASCIITable->new;
451 $publics->setCols( 'Public', 'Private' );
699e1247 452 $publics->setColWidth( 'Public', 36, 1 );
453 $publics->setColWidth( 'Private', 37, 1 );
99fe1710 454
1abd6db7 455 for my $plain ( sort keys %{ $actions->{plain} } ) {
456 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
7ace516e 457 my $reverse = $self->actions->{reverse}->{$code};
458 $reverse = $reverse ? "/$reverse" : $code;
cd677e12 459 $publics->addRow( "/$plain", $reverse );
1abd6db7 460 }
99fe1710 461
1abd6db7 462 $self->log->debug( 'Loaded public actions', $publics->draw )
a268a011 463 if ( @{ $publics->{tbl_rows} } );
99fe1710 464
1abd6db7 465 my $regexes = Text::ASCIITable->new;
466 $regexes->setCols( 'Regex', 'Private' );
699e1247 467 $regexes->setColWidth( 'Regex', 36, 1 );
468 $regexes->setColWidth( 'Private', 37, 1 );
99fe1710 469
1abd6db7 470 for my $regex ( sort keys %{ $actions->{regex} } ) {
471 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
7ace516e 472 my $reverse = $self->actions->{reverse}->{$code};
473 $reverse = $reverse ? "/$reverse" : $code;
cd677e12 474 $regexes->addRow( $regex, $reverse );
1abd6db7 475 }
99fe1710 476
1abd6db7 477 $self->log->debug( 'Loaded regex actions', $regexes->draw )
a268a011 478 if ( @{ $regexes->{tbl_rows} } );
1abd6db7 479}
480
1abd6db7 481=back
482
483=head1 AUTHOR
484
485Sebastian Riedel, C<sri@cpan.org>
486
487=head1 COPYRIGHT
488
489This program is free software, you can redistribute it and/or modify it under
490the same terms as Perl itself.
491
492=cut
493
4941;