1 package Catalyst::Dispatch;
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 $c->log->debug(qq/Couldn't forward to "$class"/) if $c->debug;
127 my $method = shift || 'process';
128 if ( my $code = $class->can($method) ) {
129 $c->actions->{reverse}->{"$code"} = "$class->$method";
130 $results = [ [ [ $class, $code ] ] ];
133 $c->log->debug(qq/Couldn't forward to "$class->$method"/)
138 for my $result ( @{$results} ) {
139 $c->execute( @{ $result->[0] } );
140 return if scalar @{ $c->error };
141 last unless $c->state;
146 =item $c->get_action( $action, $namespace )
148 Get an action in a given namespace.
153 my ( $c, $action, $namespace ) = @_;
154 return [] unless $action;
157 $namespace = '' if $namespace eq '/';
158 my $parent = $c->tree;
160 my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
161 if ( $allowed{$action} ) {
162 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
163 push @results, [$result] if $result;
164 my $visitor = Tree::Simple::Visitor::FindByPath->new;
165 for my $part ( split '/', $namespace ) {
166 $visitor->setSearchPath($part);
167 $parent->accept($visitor);
168 my $child = $visitor->getResult;
169 my $uid = $child->getUID if $child;
170 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
171 push @results, [$match] if $match;
172 $parent = $child if $child;
177 my $visitor = Tree::Simple::Visitor::FindByPath->new;
178 $visitor->setSearchPath( split '/', $namespace );
179 $parent->accept($visitor);
180 my $child = $visitor->getResult;
181 my $uid = $child->getUID if $child;
182 my $match = $c->actions->{private}->{$uid}->{$action}
184 push @results, [$match] if $match;
188 $c->actions->{private}->{ $parent->getUID }->{$action};
189 push @results, [$result] if $result;
194 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
195 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
197 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
198 my $name = $c->actions->{compiled}->[$i]->[0];
199 my $regex = $c->actions->{compiled}->[$i]->[1];
200 if ( $action =~ $regex ) {
202 for my $i ( 1 .. 9 ) {
205 push @snippets, ${$i};
207 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
214 =item $c->set_action( $action, $code, $namespace, $attrs )
216 Set an action in a given namespace.
221 my ( $c, $method, $code, $namespace, $attrs ) = @_;
223 my $prefix = _class2prefix($namespace) || '';
226 for my $attr ( @{$attrs} ) {
227 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
228 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
229 elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 }
230 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
231 elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 }
234 return unless keys %flags;
236 my $parent = $c->tree;
237 my $visitor = Tree::Simple::Visitor::FindByPath->new;
238 for my $part ( split '/', $prefix ) {
239 $visitor->setSearchPath($part);
240 $parent->accept($visitor);
241 my $child = $visitor->getResult;
243 $child = $parent->addChild( Tree::Simple->new($part) );
244 $visitor->setSearchPath($part);
245 $parent->accept($visitor);
246 $child = $visitor->getResult;
250 my $uid = $parent->getUID;
251 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
252 my $forward = $prefix ? "$prefix/$method" : $method;
254 if ( $flags{path} ) {
255 $flags{path} =~ s/^\w+//;
256 $flags{path} =~ s/\w+$//;
257 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
258 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
260 if ( $flags{regex} ) {
261 $flags{regex} =~ s/^\w+//;
262 $flags{regex} =~ s/\w+$//;
263 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
264 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
267 my $reverse = $prefix ? "$prefix/$method" : $method;
269 if ( $flags{local} || $flags{global} || $flags{path} ) {
270 my $path = $flags{path} || $method;
272 if ( $path =~ /^\/(.+)/ ) {
276 $absolute = 1 if $flags{global};
277 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
278 $c->actions->{plain}->{$name} = [ $namespace, $code ];
280 if ( my $regex = $flags{regex} ) {
281 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
282 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
285 $c->actions->{reverse}->{"$code"} = $reverse;
288 =item $class->setup_actions($component)
290 Setup actions for a component.
295 my ( $self, $comps ) = @_;
296 for my $comp (@$comps) {
297 $comp = ref $comp || $comp;
298 for my $action ( @{ $comp->_cache } ) {
299 my ( $code, $attrs ) = @{$action};
302 my @cache = ( $comp, @{"$comp\::ISA"} );
304 while ( my $namespace = shift @cache ) {
305 $namespaces{$namespace}++;
306 for my $isa ( @{"$comp\::ISA"} ) {
307 next if $namespaces{$isa};
312 for my $namespace ( keys %namespaces ) {
313 for my $sym ( values %{ $namespace . '::' } ) {
314 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
315 $name = *{$sym}{NAME};
316 $self->set_action( $name, $code, $comp, $attrs );
323 my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } );
324 $t->setCols('Class');
325 $t->setColWidth( 'Class', 75, 1 );
326 $t->addRow( wrap( $_, 75 ) ) for keys %{ $self->components };
327 $self->log->debug( 'Loaded components', $t->draw )
328 if ( @{ $t->{tbl_rows} } && $self->debug );
329 my $actions = $self->actions;
330 my $privates = Text::ASCIITable->new;
331 $privates->setCols( 'Private', 'Class', 'Code' );
332 $privates->setColWidth( 'Private', 28, 1 );
333 $privates->setColWidth( 'Class', 28, 1 );
334 $privates->setColWidth( 'Code', 14, 1 );
336 my ( $walker, $parent, $prefix ) = @_;
337 $prefix .= $parent->getNodeValue || '';
338 $prefix .= '/' unless $prefix =~ /\/$/;
339 my $uid = $parent->getUID;
340 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
341 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
343 wrap( "$prefix$action", 28 ),
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', 37, 1 );
356 $publics->setColWidth( 'Private', 36, 1 );
357 for my $plain ( sort keys %{ $actions->{plain} } ) {
358 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
359 $publics->addRow( wrap( "/$plain", 37 ),
360 wrap( $self->actions->{reverse}->{$code} || $code, 36 ) );
362 $self->log->debug( 'Loaded public actions', $publics->draw )
363 if ( @{ $publics->{tbl_rows} } && $self->debug );
364 my $regexes = Text::ASCIITable->new;
365 $regexes->setCols( 'Regex', 'Private' );
366 $regexes->setColWidth( 'Regex', 37, 1 );
367 $regexes->setColWidth( 'Private', 36, 1 );
368 for my $regex ( sort keys %{ $actions->{regex} } ) {
369 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
370 $regexes->addRow( wrap( $regex, 37 ),
371 wrap( $self->actions->{reverse}->{$class} || $class, 36 ) );
373 $self->log->debug( 'Loaded regex actions', $regexes->draw )
374 if ( @{ $regexes->{tbl_rows} } && $self->debug );
378 my ( $class, $name ) = @_;
379 my $prefix = _class2prefix($class);
380 $name = "$prefix/$name" if $prefix;
385 my $class = shift || '';
387 if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
389 $prefix =~ s/\:\:/\//g;
398 Sebastian Riedel, C<sri@cpan.org>
402 This program is free software, you can redistribute it and/or modify it under
403 the same terms as Perl itself.