1 package Catalyst::Dispatcher;
4 use base 'Class::Data::Inheritable';
7 use Text::ASCIITable::Wrap 'wrap';
9 use Tree::Simple::Visitor::FindByPath;
11 __PACKAGE__->mk_classdata($_) for qw/actions tree/;
14 { plain => {}, private => {}, regex => {}, compiled => [], reverse => {} }
16 __PACKAGE__->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
18 memoize('_class2prefix');
22 Catalyst::Dispatch - The Catalyst Dispatcher
36 Dispatch request to actions.
42 my $action = $c->req->action;
44 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
45 if $action eq 'default';
47 if ( my $result = $c->get_action($action) ) {
48 $namespace = _class2prefix( $result->[0]->[0]->[0] );
51 my $default = $action eq 'default' ? $namespace : undef;
52 my $results = $c->get_action( $action, $default );
58 if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) {
59 $c->execute( @{ $begin->[0] } );
60 return if scalar @{ $c->error };
63 # Execute the auto chain
64 for my $auto ( @{ $c->get_action( 'auto', $namespace ) } ) {
65 $c->execute( @{ $auto->[0] } );
66 return if scalar @{ $c->error };
67 last unless $c->state;
70 # Execute the action or last default
71 if ( ( my $action = $c->req->action ) && $c->state ) {
72 if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) {
73 $c->execute( @{ $result->[0] } );
78 if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) {
79 $c->execute( @{ $end->[0] } );
80 return if scalar @{ $c->error };
84 my $path = $c->req->path;
86 ? qq/Unknown resource "$path"/
87 : "No default action defined";
88 $c->log->error($error) if $c->debug;
93 =item $c->forward($command)
95 Forward processing to a private action or a method from a class.
96 If you define a class without method it will default to process().
100 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
101 $c->forward('MyApp::View::TT');
109 $c->log->debug('Nothing to forward to') if $c->debug;
112 my $caller = caller(0);
114 if ( $command =~ /^\// ) {
115 $command =~ /^(.*)\/(\w+)$/;
116 $namespace = $1 || '/';
119 else { $namespace = _class2prefix($caller) || '/' }
120 my $results = $c->get_action( $command, $namespace );
121 unless ( @{$results} ) {
122 my $class = $command;
123 if ( $class =~ /[^\w\:]/ ) {
124 my $error = qq/Couldn't forward to "$class"/;
126 $c->log->debug($error) if $c->debug;
129 my $method = shift || 'process';
130 if ( my $code = $class->can($method) ) {
131 $c->actions->{reverse}->{"$code"} = "$class->$method";
132 $results = [ [ [ $class, $code ] ] ];
135 my $error = qq/Couldn't forward to "$class->$method"/;
137 $c->log->debug($error)
142 for my $result ( @{$results} ) {
143 $c->execute( @{ $result->[0] } );
144 return if scalar @{ $c->error };
145 last unless $c->state;
150 =item $c->get_action( $action, $namespace )
152 Get an action in a given namespace.
157 my ( $c, $action, $namespace ) = @_;
158 return [] unless $action;
161 $namespace = '' if $namespace eq '/';
162 my $parent = $c->tree;
164 my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
165 if ( $allowed{$action} ) {
166 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
167 push @results, [$result] if $result;
168 my $visitor = Tree::Simple::Visitor::FindByPath->new;
169 for my $part ( split '/', $namespace ) {
170 $visitor->setSearchPath($part);
171 $parent->accept($visitor);
172 my $child = $visitor->getResult;
173 my $uid = $child->getUID if $child;
174 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
175 push @results, [$match] if $match;
176 $parent = $child if $child;
181 my $visitor = Tree::Simple::Visitor::FindByPath->new;
182 $visitor->setSearchPath( split '/', $namespace );
183 $parent->accept($visitor);
184 my $child = $visitor->getResult;
185 my $uid = $child->getUID if $child;
186 my $match = $c->actions->{private}->{$uid}->{$action}
188 push @results, [$match] if $match;
192 $c->actions->{private}->{ $parent->getUID }->{$action};
193 push @results, [$result] if $result;
198 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
199 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
201 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
202 my $name = $c->actions->{compiled}->[$i]->[0];
203 my $regex = $c->actions->{compiled}->[$i]->[1];
204 if ( $action =~ $regex ) {
206 for my $i ( 1 .. 9 ) {
209 push @snippets, ${$i};
211 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
218 =item $c->set_action( $action, $code, $namespace, $attrs )
220 Set an action in a given namespace.
225 my ( $c, $method, $code, $namespace, $attrs ) = @_;
227 my $prefix = _class2prefix($namespace) || '';
230 for my $attr ( @{$attrs} ) {
231 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
232 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
233 elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 }
234 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
235 elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 }
238 if ( $flags{private} && ( keys %flags > 1 ) ) {
239 $c->log->error( 'Bad action definition "'
240 . join( ' ', @{$attrs} )
241 . qq/" for "$namespace->$method"/ );
244 return unless keys %flags;
246 my $parent = $c->tree;
247 my $visitor = Tree::Simple::Visitor::FindByPath->new;
248 for my $part ( split '/', $prefix ) {
249 $visitor->setSearchPath($part);
250 $parent->accept($visitor);
251 my $child = $visitor->getResult;
253 $child = $parent->addChild( Tree::Simple->new($part) );
254 $visitor->setSearchPath($part);
255 $parent->accept($visitor);
256 $child = $visitor->getResult;
260 my $uid = $parent->getUID;
261 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
262 my $forward = $prefix ? "$prefix/$method" : $method;
264 if ( $flags{path} ) {
265 $flags{path} =~ s/^\w+//;
266 $flags{path} =~ s/\w+$//;
267 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
268 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
270 if ( $flags{regex} ) {
271 $flags{regex} =~ s/^\w+//;
272 $flags{regex} =~ s/\w+$//;
273 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
274 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
277 my $reverse = $prefix ? "$prefix/$method" : $method;
279 if ( $flags{local} || $flags{global} || $flags{path} ) {
280 my $path = $flags{path} || $method;
282 if ( $path =~ /^\/(.+)/ ) {
286 $absolute = 1 if $flags{global};
287 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
288 $c->actions->{plain}->{$name} = [ $namespace, $code ];
290 if ( my $regex = $flags{regex} ) {
291 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
292 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
295 $c->actions->{reverse}->{"$code"} = $reverse;
298 =item $class->setup_actions($component)
300 Setup actions for a component.
305 my ( $self, $comps ) = @_;
306 for my $comp (@$comps) {
307 $comp = ref $comp || $comp;
308 for my $action ( @{ $comp->_cache } ) {
309 my ( $code, $attrs ) = @{$action};
312 my @cache = ( $comp, @{"$comp\::ISA"} );
314 while ( my $namespace = shift @cache ) {
315 $namespaces{$namespace}++;
316 for my $isa ( @{"$comp\::ISA"} ) {
317 next if $namespaces{$isa};
322 for my $namespace ( keys %namespaces ) {
323 for my $sym ( values %{ $namespace . '::' } ) {
324 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
325 $name = *{$sym}{NAME};
326 $self->set_action( $name, $code, $comp, $attrs );
333 my $actions = $self->actions;
334 my $privates = Text::ASCIITable->new;
335 $privates->setCols( 'Private', 'Class' );
336 $privates->setColWidth( 'Private', 36, 1 );
337 $privates->setColWidth( 'Class', 37, 1 );
339 my ( $walker, $parent, $prefix ) = @_;
340 $prefix .= $parent->getNodeValue || '';
341 $prefix .= '/' unless $prefix =~ /\/$/;
342 my $uid = $parent->getUID;
343 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
344 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
345 $privates->addRow( wrap( "$prefix$action", 36 ),
346 wrap( $class, 37 ) );
348 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
350 $walker->( $walker, $self->tree, '' );
351 $self->log->debug( 'Loaded private actions', $privates->draw )
352 if ( @{ $privates->{tbl_rows} } && $self->debug );
353 my $publics = Text::ASCIITable->new;
354 $publics->setCols( 'Public', 'Private' );
355 $publics->setColWidth( 'Public', 36, 1 );
356 $publics->setColWidth( 'Private', 37, 1 );
357 for my $plain ( sort keys %{ $actions->{plain} } ) {
358 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
359 my $reverse = $self->actions->{reverse}->{$code};
360 $reverse = $reverse ? "/$reverse" : $code;
361 $publics->addRow( wrap( "/$plain", 36 ), wrap( $reverse, 37 ) );
363 $self->log->debug( 'Loaded public actions', $publics->draw )
364 if ( @{ $publics->{tbl_rows} } && $self->debug );
365 my $regexes = Text::ASCIITable->new;
366 $regexes->setCols( 'Regex', 'Private' );
367 $regexes->setColWidth( 'Regex', 36, 1 );
368 $regexes->setColWidth( 'Private', 37, 1 );
369 for my $regex ( sort keys %{ $actions->{regex} } ) {
370 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
371 my $reverse = $self->actions->{reverse}->{$code};
372 $reverse = $reverse ? "/$reverse" : $code;
373 $regexes->addRow( wrap( $regex, 36 ), wrap( $reverse, 37 ) );
375 $self->log->debug( 'Loaded regex actions', $regexes->draw )
376 if ( @{ $regexes->{tbl_rows} } && $self->debug );
380 my ( $class, $name ) = @_;
381 my $prefix = _class2prefix($class);
382 $name = "$prefix/$name" if $prefix;
387 my $class = shift || '';
389 if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
391 $prefix =~ s/\:\:/\//g;
400 Sebastian Riedel, C<sri@cpan.org>
404 This program is free software, you can redistribute it and/or modify it under
405 the same terms as Perl itself.