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