Updated helper
[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;
87b85407 11use Text::SimpleTable;
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
d6e0d7e6 115 my $comp;
116
117 unless ( $comp = $c->component($command) ) {
bd7d2e94 118 my $error =
119qq/Couldn't forward to command "$command". Invalid action or component./;
3b2ed580 120 $c->error($error);
121 $c->log->debug($error) if $c->debug;
1abd6db7 122 return 0;
123 }
bd7d2e94 124
d6e0d7e6 125 my $class = ref $comp;
1abd6db7 126 my $method = shift || 'process';
99fe1710 127
d6e0d7e6 128 if ( my $code = $class->can($method) ) {
fbcc39ad 129 my $action = Catalyst::Action->new(
130 {
6b239949 131 name => $method,
fbcc39ad 132 code => $code,
133 reverse => "$class->$method",
11bd4e3e 134 class => $class,
fbcc39ad 135 namespace => $class,
136 }
137 );
a9dc674c 138 $result = $action;
fbcc39ad 139 }
140
141 else {
bd7d2e94 142 my $error =
143 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 144 $c->error($error);
145 $c->log->debug($error)
1abd6db7 146 if $c->debug;
147 return 0;
148 }
99fe1710 149
1abd6db7 150 }
bd7d2e94 151
152 local $c->request->{arguments} = [ @{$arguments} ];
99fe1710 153
a9dc674c 154 $result->execute($c);
99fe1710 155
1abd6db7 156 return $c->state;
157}
158
fbcc39ad 159=item $self->prepare_action($c)
160
161=cut
162
163sub prepare_action {
164 my ( $self, $c ) = @_;
165 my $path = $c->req->path;
166 my @path = split /\//, $c->req->path;
167 $c->req->args( \my @args );
168
78d760bb 169 push( @path, '/' ) unless @path; # Root action
170
b96f127f 171 DESCEND: while (@path) {
fbcc39ad 172 $path = join '/', @path;
fbcc39ad 173
78d760bb 174 $path = '' if $path eq '/'; # Root action
175
22f3a8dd 176 # Check out dispatch types to see if any will handle the path at
177 # this level
178
78d760bb 179 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 180 last DESCEND if $type->match( $c, $path );
66e28e3f 181 }
b96f127f 182
22f3a8dd 183 # If not, move the last part path to args
184
b96f127f 185 unshift @args, pop @path;
fbcc39ad 186 }
187
188 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
189 if ( $c->debug && @args );
190}
191
a9dc674c 192=item $self->get_action( $c, $action, $namespace )
1abd6db7 193
194=cut
195
196sub get_action {
79a3189a 197 my ( $self, $c, $name, $namespace ) = @_;
198 return unless $name;
bcccee4e 199 $namespace ||= '';
772ab8ae 200 $namespace = '' if $namespace eq '/';
99fe1710 201
78d760bb 202 my @match = $self->get_containers($namespace);
c8d9780f 203
79a3189a 204 return unless @match;
772ab8ae 205
79a3189a 206 if ( my $action = $match[-1]->get_action( $c, $name ) )
a9dc674c 207 {
79a3189a 208 return $action if $action->namespace eq $namespace;
1abd6db7 209 }
1abd6db7 210}
211
a9dc674c 212=item $self->get_actions( $c, $action, $namespace )
213
214=cut
215
216sub get_actions {
217 my ( $self, $c, $action, $namespace ) = @_;
218 return [] unless $action;
219 $namespace ||= '';
220 $namespace = '' if $namespace eq '/';
221
222 my @match = $self->get_containers($namespace);
223
79a3189a 224 return map { $_->get_action($c, $action) } @match;
a9dc674c 225}
226
cfd04b0c 227=item $self->get_containers( $namespace )
228
229=cut
230
231sub get_containers {
232 my ( $self, $namespace ) = @_;
233
90ce41ba 234 # If the namespace is / just return the root ActionContainer
235
78d760bb 236 return ( $self->tree->getNodeValue )
237 if ( !$namespace || ( $namespace eq '/' ) );
cfd04b0c 238
90ce41ba 239 # Use a visitor to recurse down the tree finding the ActionContainers
240 # for each namespace in the chain.
241
cfd04b0c 242 my $visitor = Tree::Simple::Visitor::FindByPath->new;
78d760bb 243 my @path = split( '/', $namespace );
244 $visitor->setSearchPath(@path);
cfd04b0c 245 $self->tree->accept($visitor);
246
247 my @match = $visitor->getResults;
78d760bb 248 @match = ( $self->tree ) unless @match;
cfd04b0c 249
78d760bb 250 if ( !defined $visitor->getResult ) {
90ce41ba 251
252 # If we don't manage to match, the visitor doesn't return the last
253 # node is matched, so foo/bar/baz would only find the 'foo' node,
254 # not the foo and foo/bar nodes as it should. This does another
255 # single-level search to see if that's the case, and the 'last unless'
256 # should catch any failures - or short-circuit this if this *is* a
257 # bug in the visitor and gets fixed.
258
78d760bb 259 my $extra = $path[ ( scalar @match ) - 1 ];
cfd04b0c 260 last unless $extra;
261 $visitor->setSearchPath($extra);
262 $match[-1]->accept($visitor);
78d760bb 263 push( @match, $visitor->getResult ) if defined $visitor->getResult;
cfd04b0c 264 }
265
266 return map { $_->getNodeValue } @match;
267}
268
79a3189a 269sub register {
270 my ( $self, $c, $action ) = @_;
271
272 my $namespace = $action->namespace;
273 my $parent = $self->tree;
274 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 275
11bd4e3e 276 if ($namespace) {
277 for my $part ( split '/', $namespace ) {
1abd6db7 278 $visitor->setSearchPath($part);
279 $parent->accept($visitor);
b7aebc12 280 my $child = $visitor->getResult;
78d760bb 281
b7aebc12 282 unless ($child) {
90ce41ba 283
284 # Create a new tree node and an ActionContainer to form
285 # its value.
286
78d760bb 287 my $container =
288 Catalyst::ActionContainer->new(
289 { part => $part, actions => {} } );
b7aebc12 290 $child = $parent->addChild( Tree::Simple->new($container) );
291 $visitor->setSearchPath($part);
292 $parent->accept($visitor);
293 $child = $visitor->getResult;
294 }
78d760bb 295
b7aebc12 296 $parent = $child;
1abd6db7 297 }
1abd6db7 298 }
99fe1710 299
90ce41ba 300 # Set the method value
79a3189a 301 $parent->getNodeValue->actions->{$action->name} = $action;
fbcc39ad 302
91d4abc5 303 my $registered = $self->registered_dispatch_types;
304
305 foreach my $key (keys %{$action->attributes}) {
306 my $class = "Catalyst::DispatchType::$key";
307 unless ( $registered->{$class} ) {
308 eval "require $class";
309 push( @{ $self->dispatch_types }, $class->new ) unless $@;
310 $registered->{$class} = 1;
311 }
312 }
313
22f3a8dd 314 # Pass the action to our dispatch types so they can register it if reqd.
b96f127f 315 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 316 $type->register( $c, $action );
b96f127f 317 }
1abd6db7 318}
319
fbcc39ad 320=item $self->setup_actions( $class, $component )
1abd6db7 321
322=cut
323
324sub setup_actions {
11bd4e3e 325 my ( $self, $c ) = @_;
99fe1710 326
6d030e6f 327 $self->dispatch_types( [] );
91d4abc5 328 $self->registered_dispatch_types( {} );
12e28165 329
6d030e6f 330 # Preload action types
331 for my $type (@PRELOAD) {
332 my $class = "Catalyst::DispatchType::$type";
333 eval "require $class";
334 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
335 if $@;
336 push @{ $self->dispatch_types }, $class->new;
91d4abc5 337 $self->registered_dispatch_types->{$class} = 1;
6d030e6f 338 }
b96f127f 339
12e28165 340 # We use a tree
78d760bb 341 my $container =
342 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
b7aebc12 343 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
e494bd6b 344
91d4abc5 345 $c->register_actions( $c );
99fe1710 346
91d4abc5 347 foreach my $comp ( values %{$c->components} ) {
348 $comp->register_actions( $c ) if $comp->can('register_actions');
1abd6db7 349 }
e494bd6b 350
2d1d8f91 351 # Postload action types
352 for my $type (@POSTLOAD) {
353 my $class = "Catalyst::DispatchType::$type";
354 eval "require $class";
355 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
356 if $@;
357 push @{ $self->dispatch_types }, $class->new;
358 }
6d030e6f 359
11bd4e3e 360 return unless $c->debug;
99fe1710 361
f69e18e4 362 my $privates
363 = Text::SimpleTable->new( [ 36, 'Private' ], [ 37, 'Class' ] );
99fe1710 364
87b85407 365 my $has_private = 0;
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} ) );
87b85407 377 $privates->row( "$prefix$action", $action_obj->class );
378 $has_private = 1;
1abd6db7 379 }
99fe1710 380
1abd6db7 381 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
382 };
99fe1710 383
1abd6db7 384 $walker->( $walker, $self->tree, '' );
11bd4e3e 385 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
87b85407 386 if ( $has_private );
99fe1710 387
a9cbd748 388 # List all public actions
11bd4e3e 389 $_->list($c) for @{ $self->dispatch_types };
1abd6db7 390}
391
1abd6db7 392=back
393
394=head1 AUTHOR
395
396Sebastian Riedel, C<sri@cpan.org>
158c88c0 397Matt S Trout, C<mst@shadowcatsystems.co.uk>
1abd6db7 398
399=head1 COPYRIGHT
400
401This program is free software, you can redistribute it and/or modify it under
402the same terms as Perl itself.
403
404=cut
405
4061;