Added JavaScript to debug screen, to show and hide specific dumps
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
CommitLineData
68a748b9 1package Catalyst::Dispatcher;
1abd6db7 2
3use strict;
fbcc39ad 4use base 'Class::Accessor::Fast';
a2f2cde9 5use Catalyst::Exception;
f05af9ba 6use Catalyst::Utils;
fbcc39ad 7use Catalyst::Action;
b7aebc12 8use Catalyst::ActionContainer;
b96f127f 9use Catalyst::DispatchType::Default;
bcccee4e 10use Catalyst::DispatchType::Index;
1abd6db7 11use Text::ASCIITable;
1abd6db7 12use Tree::Simple;
13use Tree::Simple::Visitor::FindByPath;
14
fbcc39ad 15# Stringify to class
16use overload '""' => sub { return ref shift }, fallback => 1;
17
91d4abc5 18__PACKAGE__->mk_accessors(qw/tree dispatch_types registered_dispatch_types/);
6d030e6f 19
20# Preload these action types
21our @PRELOAD = qw/Path Regex/;
1abd6db7 22
2d1d8f91 23# Postload these action types
24our @POSTLOAD = qw/Index Default/;
25
1abd6db7 26=head1 NAME
27
9c053379 28Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 29
30=head1 SYNOPSIS
31
32See L<Catalyst>.
33
34=head1 DESCRIPTION
35
36=head1 METHODS
37
38=over 4
39
fbcc39ad 40=item $self->detach( $c, $command [, \@arguments ] )
6ef62eb2 41
42=cut
43
44sub detach {
fbcc39ad 45 my ( $self, $c, $command, @args ) = @_;
bd7d2e94 46 $c->forward( $command, @args ) if $command;
fbcc39ad 47 die $Catalyst::DETACH;
6ef62eb2 48}
49
fbcc39ad 50=item $self->dispatch($c)
1abd6db7 51
52=cut
53
54sub dispatch {
fbcc39ad 55 my ( $self, $c ) = @_;
cfd04b0c 56
66e28e3f 57 if ( $c->action ) {
b0bb11ec 58 $c->forward( join( '/', '', $c->namespace, '_DISPATCH' ) );
fbcc39ad 59 }
60
61 else {
1abd6db7 62 my $path = $c->req->path;
63 my $error = $path
64 ? qq/Unknown resource "$path"/
65 : "No default action defined";
66 $c->log->error($error) if $c->debug;
67 $c->error($error);
68 }
69}
70
fbcc39ad 71=item $self->forward( $c, $command [, \@arguments ] )
1abd6db7 72
73=cut
74
75sub forward {
fbcc39ad 76 my $self = shift;
1abd6db7 77 my $c = shift;
78 my $command = shift;
99fe1710 79
1abd6db7 80 unless ($command) {
81 $c->log->debug('Nothing to forward to') if $c->debug;
82 return 0;
83 }
99fe1710 84
e0fc6749 85 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
99fe1710 86
a9dc674c 87 my $result;
fbcc39ad 88
8199eac3 89 my $command_copy = $command;
90
91 unless ( $command_copy =~ s/^\/// ) {
91d4abc5 92 my $namespace = $c->namespace;
5d68b17b 93 $command_copy = "${namespace}/${command}";
1abd6db7 94 }
99fe1710 95
8199eac3 96 unless ( $command_copy =~ /\// ) {
a9dc674c 97 $result = $c->get_action( $command_copy, '/' );
8199eac3 98 }
e494bd6b 99 else {
8199eac3 100 my @extra_args;
101 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
102 my $tail = $2;
a9dc674c 103 $result = $c->get_action( $tail, $1 );
104 if ( $result ) {
8199eac3 105 $command = $tail;
106 push( @{$arguments}, @extra_args );
107 last DESCEND;
108 }
109 unshift( @extra_args, $tail );
110 }
e494bd6b 111 }
99fe1710 112
a9dc674c 113 unless ( $result ) {
bd7d2e94 114
fbcc39ad 115 unless ( $c->components->{$command} ) {
bd7d2e94 116 my $error =
117qq/Couldn't forward to command "$command". Invalid action or component./;
3b2ed580 118 $c->error($error);
119 $c->log->debug($error) if $c->debug;
1abd6db7 120 return 0;
121 }
bd7d2e94 122
1f6bb799 123 my $class = $command;
1abd6db7 124 my $method = shift || 'process';
99fe1710 125
1f6bb799 126 if ( my $code = $c->components->{$class}->can($method) ) {
fbcc39ad 127 my $action = Catalyst::Action->new(
128 {
6b239949 129 name => $method,
fbcc39ad 130 code => $code,
131 reverse => "$class->$method",
11bd4e3e 132 class => $class,
fbcc39ad 133 namespace => $class,
134 }
135 );
a9dc674c 136 $result = $action;
fbcc39ad 137 }
138
139 else {
bd7d2e94 140 my $error =
141 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 142 $c->error($error);
143 $c->log->debug($error)
1abd6db7 144 if $c->debug;
145 return 0;
146 }
99fe1710 147
1abd6db7 148 }
bd7d2e94 149
150 local $c->request->{arguments} = [ @{$arguments} ];
91d4abc5 151 local $c->{namespace} = $result->namespace;
99fe1710 152
a9dc674c 153 $result->execute($c);
99fe1710 154
1abd6db7 155 return $c->state;
156}
157
fbcc39ad 158=item $self->prepare_action($c)
159
160=cut
161
162sub prepare_action {
163 my ( $self, $c ) = @_;
164 my $path = $c->req->path;
165 my @path = split /\//, $c->req->path;
166 $c->req->args( \my @args );
167
78d760bb 168 push( @path, '/' ) unless @path; # Root action
169
b96f127f 170 DESCEND: while (@path) {
fbcc39ad 171 $path = join '/', @path;
fbcc39ad 172
78d760bb 173 $path = '' if $path eq '/'; # Root action
174
22f3a8dd 175 # Check out dispatch types to see if any will handle the path at
176 # this level
177
78d760bb 178 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 179 last DESCEND if $type->match( $c, $path );
66e28e3f 180 }
b96f127f 181
22f3a8dd 182 # If not, move the last part path to args
183
b96f127f 184 unshift @args, pop @path;
fbcc39ad 185 }
186
187 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
188 if ( $c->debug && @args );
189}
190
a9dc674c 191=item $self->get_action( $c, $action, $namespace )
1abd6db7 192
193=cut
194
195sub get_action {
79a3189a 196 my ( $self, $c, $name, $namespace ) = @_;
197 return unless $name;
bcccee4e 198 $namespace ||= '';
772ab8ae 199 $namespace = '' if $namespace eq '/';
99fe1710 200
78d760bb 201 my @match = $self->get_containers($namespace);
c8d9780f 202
79a3189a 203 return unless @match;
772ab8ae 204
79a3189a 205 if ( my $action = $match[-1]->get_action( $c, $name ) )
a9dc674c 206 {
79a3189a 207 return $action if $action->namespace eq $namespace;
1abd6db7 208 }
1abd6db7 209}
210
a9dc674c 211=item $self->get_actions( $c, $action, $namespace )
212
213=cut
214
215sub get_actions {
216 my ( $self, $c, $action, $namespace ) = @_;
217 return [] unless $action;
218 $namespace ||= '';
219 $namespace = '' if $namespace eq '/';
220
221 my @match = $self->get_containers($namespace);
222
79a3189a 223 return map { $_->get_action($c, $action) } @match;
a9dc674c 224}
225
cfd04b0c 226=item $self->get_containers( $namespace )
227
228=cut
229
230sub get_containers {
231 my ( $self, $namespace ) = @_;
232
90ce41ba 233 # If the namespace is / just return the root ActionContainer
234
78d760bb 235 return ( $self->tree->getNodeValue )
236 if ( !$namespace || ( $namespace eq '/' ) );
cfd04b0c 237
90ce41ba 238 # Use a visitor to recurse down the tree finding the ActionContainers
239 # for each namespace in the chain.
240
cfd04b0c 241 my $visitor = Tree::Simple::Visitor::FindByPath->new;
78d760bb 242 my @path = split( '/', $namespace );
243 $visitor->setSearchPath(@path);
cfd04b0c 244 $self->tree->accept($visitor);
245
246 my @match = $visitor->getResults;
78d760bb 247 @match = ( $self->tree ) unless @match;
cfd04b0c 248
78d760bb 249 if ( !defined $visitor->getResult ) {
90ce41ba 250
251 # If we don't manage to match, the visitor doesn't return the last
252 # node is matched, so foo/bar/baz would only find the 'foo' node,
253 # not the foo and foo/bar nodes as it should. This does another
254 # single-level search to see if that's the case, and the 'last unless'
255 # should catch any failures - or short-circuit this if this *is* a
256 # bug in the visitor and gets fixed.
257
78d760bb 258 my $extra = $path[ ( scalar @match ) - 1 ];
cfd04b0c 259 last unless $extra;
260 $visitor->setSearchPath($extra);
261 $match[-1]->accept($visitor);
78d760bb 262 push( @match, $visitor->getResult ) if defined $visitor->getResult;
cfd04b0c 263 }
264
265 return map { $_->getNodeValue } @match;
266}
267
79a3189a 268sub register {
269 my ( $self, $c, $action ) = @_;
270
271 my $namespace = $action->namespace;
272 my $parent = $self->tree;
273 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 274
11bd4e3e 275 if ($namespace) {
276 for my $part ( split '/', $namespace ) {
1abd6db7 277 $visitor->setSearchPath($part);
278 $parent->accept($visitor);
b7aebc12 279 my $child = $visitor->getResult;
78d760bb 280
b7aebc12 281 unless ($child) {
90ce41ba 282
283 # Create a new tree node and an ActionContainer to form
284 # its value.
285
78d760bb 286 my $container =
287 Catalyst::ActionContainer->new(
288 { part => $part, actions => {} } );
b7aebc12 289 $child = $parent->addChild( Tree::Simple->new($container) );
290 $visitor->setSearchPath($part);
291 $parent->accept($visitor);
292 $child = $visitor->getResult;
293 }
78d760bb 294
b7aebc12 295 $parent = $child;
1abd6db7 296 }
1abd6db7 297 }
99fe1710 298
90ce41ba 299 # Set the method value
79a3189a 300 $parent->getNodeValue->actions->{$action->name} = $action;
fbcc39ad 301
91d4abc5 302 my $registered = $self->registered_dispatch_types;
303
304 foreach my $key (keys %{$action->attributes}) {
305 my $class = "Catalyst::DispatchType::$key";
306 unless ( $registered->{$class} ) {
307 eval "require $class";
308 push( @{ $self->dispatch_types }, $class->new ) unless $@;
309 $registered->{$class} = 1;
310 }
311 }
312
22f3a8dd 313 # Pass the action to our dispatch types so they can register it if reqd.
b96f127f 314 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 315 $type->register( $c, $action );
b96f127f 316 }
1abd6db7 317}
318
fbcc39ad 319=item $self->setup_actions( $class, $component )
1abd6db7 320
321=cut
322
323sub setup_actions {
11bd4e3e 324 my ( $self, $c ) = @_;
99fe1710 325
6d030e6f 326 $self->dispatch_types( [] );
91d4abc5 327 $self->registered_dispatch_types( {} );
12e28165 328
6d030e6f 329 # Preload action types
330 for my $type (@PRELOAD) {
331 my $class = "Catalyst::DispatchType::$type";
332 eval "require $class";
333 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
334 if $@;
335 push @{ $self->dispatch_types }, $class->new;
91d4abc5 336 $self->registered_dispatch_types->{$class} = 1;
6d030e6f 337 }
b96f127f 338
12e28165 339 # We use a tree
78d760bb 340 my $container =
341 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
b7aebc12 342 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
e494bd6b 343
91d4abc5 344 $c->register_actions( $c );
99fe1710 345
91d4abc5 346 foreach my $comp ( values %{$c->components} ) {
347 $comp->register_actions( $c ) if $comp->can('register_actions');
1abd6db7 348 }
e494bd6b 349
2d1d8f91 350 # Postload action types
351 for my $type (@POSTLOAD) {
352 my $class = "Catalyst::DispatchType::$type";
353 eval "require $class";
354 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
355 if $@;
356 push @{ $self->dispatch_types }, $class->new;
357 }
6d030e6f 358
11bd4e3e 359 return unless $c->debug;
99fe1710 360
1abd6db7 361 my $privates = Text::ASCIITable->new;
5fbed090 362 $privates->setCols( 'Private', 'Class' );
363 $privates->setColWidth( 'Private', 36, 1 );
364 $privates->setColWidth( 'Class', 37, 1 );
99fe1710 365
1abd6db7 366 my $walker = sub {
367 my ( $walker, $parent, $prefix ) = @_;
368 $prefix .= $parent->getNodeValue || '';
369 $prefix .= '/' unless $prefix =~ /\/$/;
b7aebc12 370 my $node = $parent->getNodeValue->actions;
99fe1710 371
78d760bb 372 for my $action ( keys %{$node} ) {
b7aebc12 373 my $action_obj = $node->{$action};
b0bb11ec 374 next
375 if ( ( $action =~ /^_.*/ )
376 && ( !$c->config->{show_internal_actions} ) );
11bd4e3e 377 $privates->addRow( "$prefix$action", $action_obj->class );
1abd6db7 378 }
99fe1710 379
1abd6db7 380 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
381 };
99fe1710 382
1abd6db7 383 $walker->( $walker, $self->tree, '' );
11bd4e3e 384 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
a268a011 385 if ( @{ $privates->{tbl_rows} } );
99fe1710 386
a9cbd748 387 # List all public actions
11bd4e3e 388 $_->list($c) for @{ $self->dispatch_types };
1abd6db7 389}
390
1abd6db7 391=back
392
393=head1 AUTHOR
394
395Sebastian Riedel, C<sri@cpan.org>
396
397=head1 COPYRIGHT
398
399This program is free software, you can redistribute it and/or modify it under
400the same terms as Perl itself.
401
402=cut
403
4041;