removed chansen from makefile, since he has no sense of humor
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
CommitLineData
68a748b9 1package Catalyst::Dispatcher;
1abd6db7 2
3use strict;
4use base 'Class::Data::Inheritable';
5use Memoize;
6use Text::ASCIITable;
7use Text::ASCIITable::Wrap 'wrap';
8use Tree::Simple;
9use Tree::Simple::Visitor::FindByPath;
10
11__PACKAGE__->mk_classdata($_) for qw/actions tree/;
12
13__PACKAGE__->actions(
14 { plain => {}, private => {}, regex => {}, compiled => [], reverse => {} }
15);
16__PACKAGE__->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
17
18memoize('_class2prefix');
19
20=head1 NAME
21
22Catalyst::Dispatch - The Catalyst Dispatcher
23
24=head1 SYNOPSIS
25
26See L<Catalyst>.
27
28=head1 DESCRIPTION
29
30=head1 METHODS
31
32=over 4
33
34=item $c->dispatch
35
36Dispatch request to actions.
37
38=cut
39
40sub dispatch {
41 my $c = shift;
42 my $action = $c->req->action;
43 my $namespace = '';
44 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
45 if $action eq 'default';
46 unless ($namespace) {
47 if ( my $result = $c->get_action($action) ) {
48 $namespace = _class2prefix( $result->[0]->[0]->[0] );
49 }
50 }
51 my $default = $action eq 'default' ? $namespace : undef;
52 my $results = $c->get_action( $action, $default );
53 $namespace ||= '/';
54 if ( @{$results} ) {
55
56 # Execute last begin
57 $c->state(1);
58 if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) {
59 $c->execute( @{ $begin->[0] } );
60 return if scalar @{ $c->error };
61 }
62
63 # Execute the auto chain
61cfdd57 64 my $auto = 0;
1abd6db7 65 for my $auto ( @{ $c->get_action( 'auto', $namespace ) } ) {
66 $c->execute( @{ $auto->[0] } );
67 return if scalar @{ $c->error };
68 last unless $c->state;
61cfdd57 69 $auto++;
1abd6db7 70 }
71
72 # Execute the action or last default
61cfdd57 73 my $mkay = $auto ? $c->state ? 1 : 0 : 1;
74 if ( ( my $action = $c->req->action ) && $mkay ) {
1abd6db7 75 if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) {
76 $c->execute( @{ $result->[0] } );
77 }
78 }
79
80 # Execute last end
81 if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) {
82 $c->execute( @{ $end->[0] } );
83 return if scalar @{ $c->error };
84 }
85 }
86 else {
87 my $path = $c->req->path;
88 my $error = $path
89 ? qq/Unknown resource "$path"/
90 : "No default action defined";
91 $c->log->error($error) if $c->debug;
92 $c->error($error);
93 }
94}
95
96=item $c->forward($command)
97
98Forward processing to a private action or a method from a class.
99If you define a class without method it will default to process().
100
101 $c->forward('/foo');
102 $c->forward('index');
103 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
104 $c->forward('MyApp::View::TT');
105
106=cut
107
108sub forward {
109 my $c = shift;
110 my $command = shift;
111 unless ($command) {
112 $c->log->debug('Nothing to forward to') if $c->debug;
113 return 0;
114 }
115 my $caller = caller(0);
116 my $namespace = '/';
117 if ( $command =~ /^\// ) {
bad16602 118 $command =~ /^\/(.*)\/(\w+)$/;
1abd6db7 119 $namespace = $1 || '/';
120 $command = $2;
121 }
122 else { $namespace = _class2prefix($caller) || '/' }
123 my $results = $c->get_action( $command, $namespace );
124 unless ( @{$results} ) {
125 my $class = $command;
126 if ( $class =~ /[^\w\:]/ ) {
3b2ed580 127 my $error = qq/Couldn't forward to "$class"/;
128 $c->error($error);
129 $c->log->debug($error) if $c->debug;
1abd6db7 130 return 0;
131 }
132 my $method = shift || 'process';
133 if ( my $code = $class->can($method) ) {
134 $c->actions->{reverse}->{"$code"} = "$class->$method";
135 $results = [ [ [ $class, $code ] ] ];
136 }
137 else {
3b2ed580 138 my $error = qq/Couldn't forward to "$class->$method"/;
139 $c->error($error);
140 $c->log->debug($error)
1abd6db7 141 if $c->debug;
142 return 0;
143 }
144 }
145 for my $result ( @{$results} ) {
146 $c->execute( @{ $result->[0] } );
147 return if scalar @{ $c->error };
148 last unless $c->state;
149 }
150 return $c->state;
151}
152
153=item $c->get_action( $action, $namespace )
154
155Get an action in a given namespace.
156
157=cut
158
159sub get_action {
160 my ( $c, $action, $namespace ) = @_;
161 return [] unless $action;
162 $namespace ||= '';
163 if ($namespace) {
164 $namespace = '' if $namespace eq '/';
165 my $parent = $c->tree;
166 my @results;
167 my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
168 if ( $allowed{$action} ) {
169 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
170 push @results, [$result] if $result;
171 my $visitor = Tree::Simple::Visitor::FindByPath->new;
172 for my $part ( split '/', $namespace ) {
173 $visitor->setSearchPath($part);
174 $parent->accept($visitor);
175 my $child = $visitor->getResult;
176 my $uid = $child->getUID if $child;
177 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
178 push @results, [$match] if $match;
179 $parent = $child if $child;
180 }
181 }
182 else {
183 if ($namespace) {
184 my $visitor = Tree::Simple::Visitor::FindByPath->new;
185 $visitor->setSearchPath( split '/', $namespace );
186 $parent->accept($visitor);
187 my $child = $visitor->getResult;
188 my $uid = $child->getUID if $child;
189 my $match = $c->actions->{private}->{$uid}->{$action}
190 if $uid;
191 push @results, [$match] if $match;
192 }
193 else {
194 my $result =
195 $c->actions->{private}->{ $parent->getUID }->{$action};
196 push @results, [$result] if $result;
197 }
198 }
199 return \@results;
200 }
201 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
202 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
203 else {
204 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
205 my $name = $c->actions->{compiled}->[$i]->[0];
206 my $regex = $c->actions->{compiled}->[$i]->[1];
207 if ( $action =~ $regex ) {
208 my @snippets;
209 for my $i ( 1 .. 9 ) {
210 no strict 'refs';
211 last unless ${$i};
212 push @snippets, ${$i};
213 }
214 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
215 }
216 }
217 }
218 return [];
219}
220
221=item $c->set_action( $action, $code, $namespace, $attrs )
222
223Set an action in a given namespace.
224
225=cut
226
227sub set_action {
228 my ( $c, $method, $code, $namespace, $attrs ) = @_;
229
230 my $prefix = _class2prefix($namespace) || '';
231 my %flags;
232
233 for my $attr ( @{$attrs} ) {
234 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
235 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
236 elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 }
237 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
238 elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 }
239 }
240
d0e524d2 241 if ( $flags{private} && ( keys %flags > 1 ) ) {
8d4e224b 242 $c->log->debug( 'Bad action definition "'
d0e524d2 243 . join( ' ', @{$attrs} )
8d4e224b 244 . qq/" for "$namespace->$method"/ )
245 if $c->debug;
d0e524d2 246 return;
247 }
1abd6db7 248 return unless keys %flags;
249
250 my $parent = $c->tree;
251 my $visitor = Tree::Simple::Visitor::FindByPath->new;
252 for my $part ( split '/', $prefix ) {
253 $visitor->setSearchPath($part);
254 $parent->accept($visitor);
255 my $child = $visitor->getResult;
256 unless ($child) {
257 $child = $parent->addChild( Tree::Simple->new($part) );
258 $visitor->setSearchPath($part);
259 $parent->accept($visitor);
260 $child = $visitor->getResult;
261 }
262 $parent = $child;
263 }
264 my $uid = $parent->getUID;
265 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
266 my $forward = $prefix ? "$prefix/$method" : $method;
267
268 if ( $flags{path} ) {
269 $flags{path} =~ s/^\w+//;
270 $flags{path} =~ s/\w+$//;
271 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
272 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
273 }
274 if ( $flags{regex} ) {
275 $flags{regex} =~ s/^\w+//;
276 $flags{regex} =~ s/\w+$//;
277 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
278 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
279 }
280
281 my $reverse = $prefix ? "$prefix/$method" : $method;
282
283 if ( $flags{local} || $flags{global} || $flags{path} ) {
284 my $path = $flags{path} || $method;
285 my $absolute = 0;
286 if ( $path =~ /^\/(.+)/ ) {
287 $path = $1;
288 $absolute = 1;
289 }
290 $absolute = 1 if $flags{global};
291 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
292 $c->actions->{plain}->{$name} = [ $namespace, $code ];
293 }
294 if ( my $regex = $flags{regex} ) {
295 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
296 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
297 }
298
299 $c->actions->{reverse}->{"$code"} = $reverse;
300}
301
302=item $class->setup_actions($component)
303
304Setup actions for a component.
305
306=cut
307
308sub setup_actions {
309 my ( $self, $comps ) = @_;
310 for my $comp (@$comps) {
311 $comp = ref $comp || $comp;
312 for my $action ( @{ $comp->_cache } ) {
313 my ( $code, $attrs ) = @{$action};
314 my $name = '';
315 no strict 'refs';
316 my @cache = ( $comp, @{"$comp\::ISA"} );
317 my %namespaces;
318 while ( my $namespace = shift @cache ) {
319 $namespaces{$namespace}++;
320 for my $isa ( @{"$comp\::ISA"} ) {
321 next if $namespaces{$isa};
322 push @cache, $isa;
323 $namespaces{$isa}++;
324 }
325 }
326 for my $namespace ( keys %namespaces ) {
327 for my $sym ( values %{ $namespace . '::' } ) {
328 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
329 $name = *{$sym}{NAME};
330 $self->set_action( $name, $code, $comp, $attrs );
331 last;
332 }
333 }
334 }
335 }
336 }
1abd6db7 337 my $actions = $self->actions;
338 my $privates = Text::ASCIITable->new;
5fbed090 339 $privates->setCols( 'Private', 'Class' );
340 $privates->setColWidth( 'Private', 36, 1 );
341 $privates->setColWidth( 'Class', 37, 1 );
1abd6db7 342 my $walker = sub {
343 my ( $walker, $parent, $prefix ) = @_;
344 $prefix .= $parent->getNodeValue || '';
345 $prefix .= '/' unless $prefix =~ /\/$/;
346 my $uid = $parent->getUID;
347 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
348 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
5fbed090 349 $privates->addRow( wrap( "$prefix$action", 36 ),
350 wrap( $class, 37 ) );
1abd6db7 351 }
352 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
353 };
354 $walker->( $walker, $self->tree, '' );
355 $self->log->debug( 'Loaded private actions', $privates->draw )
356 if ( @{ $privates->{tbl_rows} } && $self->debug );
357 my $publics = Text::ASCIITable->new;
358 $publics->setCols( 'Public', 'Private' );
699e1247 359 $publics->setColWidth( 'Public', 36, 1 );
360 $publics->setColWidth( 'Private', 37, 1 );
1abd6db7 361 for my $plain ( sort keys %{ $actions->{plain} } ) {
362 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
7ace516e 363 my $reverse = $self->actions->{reverse}->{$code};
364 $reverse = $reverse ? "/$reverse" : $code;
699e1247 365 $publics->addRow( wrap( "/$plain", 36 ), wrap( $reverse, 37 ) );
1abd6db7 366 }
367 $self->log->debug( 'Loaded public actions', $publics->draw )
368 if ( @{ $publics->{tbl_rows} } && $self->debug );
369 my $regexes = Text::ASCIITable->new;
370 $regexes->setCols( 'Regex', 'Private' );
699e1247 371 $regexes->setColWidth( 'Regex', 36, 1 );
372 $regexes->setColWidth( 'Private', 37, 1 );
1abd6db7 373 for my $regex ( sort keys %{ $actions->{regex} } ) {
374 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
7ace516e 375 my $reverse = $self->actions->{reverse}->{$code};
376 $reverse = $reverse ? "/$reverse" : $code;
699e1247 377 $regexes->addRow( wrap( $regex, 36 ), wrap( $reverse, 37 ) );
1abd6db7 378 }
379 $self->log->debug( 'Loaded regex actions', $regexes->draw )
380 if ( @{ $regexes->{tbl_rows} } && $self->debug );
381}
382
383sub _prefix {
384 my ( $class, $name ) = @_;
385 my $prefix = _class2prefix($class);
386 $name = "$prefix/$name" if $prefix;
387 return $name;
388}
389
390sub _class2prefix {
391 my $class = shift || '';
392 my $prefix;
393 if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
394 $prefix = lc $2;
395 $prefix =~ s/\:\:/\//g;
396 }
397 return $prefix;
398}
399
400=back
401
402=head1 AUTHOR
403
404Sebastian Riedel, C<sri@cpan.org>
405
406=head1 COPYRIGHT
407
408This program is free software, you can redistribute it and/or modify it under
409the same terms as Perl itself.
410
411=cut
412
4131;