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