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