Commit | Line | Data |
5ee249f2 |
1 | package Catalyst::Controller; |
2 | |
0bc4c3ab |
3 | #switch to BEGIN { extends qw/ ... /; } ? |
6f1f968a |
4 | use MRO::Compat; |
5 | use mro 'c3'; |
a7caa492 |
6 | use base qw/Catalyst::Component Catalyst::AttrContainer/; |
6e58f383 |
7 | use Moose; |
e8b9f2a9 |
8 | |
5fb67d52 |
9 | use Scalar::Util qw/blessed/; |
234763d4 |
10 | use Catalyst::Exception; |
11 | use Catalyst::Utils; |
12 | use Class::Inspector; |
5ee249f2 |
13 | |
0bc4c3ab |
14 | has path_prefix => |
15 | ( |
84ff88cf |
16 | is => 'rw', |
0bc4c3ab |
17 | isa => 'Str', |
18 | init_arg => 'path', |
19 | predicate => 'has_path_prefix', |
20 | ); |
21 | |
22 | has action_namespace => |
23 | ( |
84ff88cf |
24 | is => 'rw', |
0bc4c3ab |
25 | isa => 'Str', |
26 | init_arg => 'namespace', |
27 | predicate => 'has_action_namespace', |
28 | ); |
29 | |
30 | has actions => |
31 | ( |
32 | is => 'rw', |
33 | isa => 'HashRef', |
34 | init_arg => undef, |
35 | ); |
36 | |
37 | # isa => 'ClassName|Catalyst' ? |
38 | has _application => (is => 'rw'); |
0fc2d522 |
39 | sub _app{ shift->_application(@_) } |
0bc4c3ab |
40 | |
41 | sub BUILD { |
42 | my ($self, $args) = @_; |
43 | my $action = delete $args->{action} || {}; |
44 | my $actions = delete $args->{actions} || {}; |
45 | my $attr_value = $self->merge_config_hashes($actions, $action); |
46 | $self->actions($attr_value); |
47 | } |
48 | |
5ee249f2 |
49 | =head1 NAME |
50 | |
51 | Catalyst::Controller - Catalyst Controller base class |
52 | |
53 | =head1 SYNOPSIS |
54 | |
234763d4 |
55 | package MyApp::Controller::Search |
a269e0c2 |
56 | use base qw/Catalyst::Controller/; |
234763d4 |
57 | |
ac5c933b |
58 | sub foo : Local { |
85d9fce6 |
59 | my ($self,$c,@args) = @_; |
ac5c933b |
60 | ... |
234763d4 |
61 | } # Dispatches to /search/foo |
5ee249f2 |
62 | |
63 | =head1 DESCRIPTION |
64 | |
a269e0c2 |
65 | Controllers are where the actions in the Catalyst framework |
66 | reside. Each action is represented by a function with an attribute to |
67 | identify what kind of action it is. See the L<Catalyst::Dispatcher> |
68 | for more info about how Catalyst dispatches to actions. |
234763d4 |
69 | |
70 | =cut |
71 | |
2361ed67 |
72 | #I think both of these could be attributes. doesn't really seem like they need |
73 | #to ble class data. i think that attributes +default would work just fine |
e8b9f2a9 |
74 | __PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class/; |
234763d4 |
75 | |
e8b9f2a9 |
76 | __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] ); |
77 | __PACKAGE__->_action_class('Catalyst::Action'); |
78 | |
234763d4 |
79 | |
80 | sub _DISPATCH : Private { |
81 | my ( $self, $c ) = @_; |
82 | |
83 | foreach my $disp ( @{ $self->_dispatch_steps } ) { |
84 | last unless $c->forward($disp); |
85 | } |
86 | |
87 | $c->forward('_END'); |
88 | } |
89 | |
90 | sub _BEGIN : Private { |
91 | my ( $self, $c ) = @_; |
92 | my $begin = ( $c->get_actions( 'begin', $c->namespace ) )[-1]; |
93 | return 1 unless $begin; |
94 | $begin->dispatch( $c ); |
95 | return !@{ $c->error }; |
96 | } |
97 | |
98 | sub _AUTO : Private { |
99 | my ( $self, $c ) = @_; |
100 | my @auto = $c->get_actions( 'auto', $c->namespace ); |
101 | foreach my $auto (@auto) { |
102 | $auto->dispatch( $c ); |
103 | return 0 unless $c->state; |
104 | } |
105 | return 1; |
106 | } |
107 | |
108 | sub _ACTION : Private { |
109 | my ( $self, $c ) = @_; |
110 | if ( ref $c->action |
111 | && $c->action->can('execute') |
112 | && $c->req->action ) |
113 | { |
114 | $c->action->dispatch( $c ); |
115 | } |
116 | return !@{ $c->error }; |
117 | } |
118 | |
119 | sub _END : Private { |
120 | my ( $self, $c ) = @_; |
121 | my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1]; |
122 | return 1 unless $end; |
123 | $end->dispatch( $c ); |
124 | return !@{ $c->error }; |
125 | } |
126 | |
0fc2d522 |
127 | sub new { |
e8b9f2a9 |
128 | my $self = shift; |
129 | my $app = $_[0]; |
0fc2d522 |
130 | my $new = $self->next::method(@_); |
e8b9f2a9 |
131 | $new->_application( $app ); |
132 | return $new; |
0fc2d522 |
133 | } |
e8b9f2a9 |
134 | |
234763d4 |
135 | sub action_for { |
136 | my ( $self, $name ) = @_; |
137 | my $app = ($self->isa('Catalyst') ? $self : $self->_application); |
138 | return $app->dispatcher->get_action($name, $self->action_namespace); |
139 | } |
140 | |
0fc2d522 |
141 | #my opinion is that this whole sub really should be a builder method, not |
142 | #something that happens on every call. Anyone else disagree?? -- groditi |
143 | |
144 | #we are wrapping the accessor, so just uyse a modifier since a normal sub would |
145 | #just be overridden by the generated moose method |
0bc4c3ab |
146 | around action_namespace => sub { |
0fc2d522 |
147 | my $orig = shift; |
148 | my ( $self, $c ) = @_; |
0bc4c3ab |
149 | |
5fb67d52 |
150 | if( ref($self) ){ |
0bc4c3ab |
151 | return $self->$orig if $self->has_action_namespace; |
0fc2d522 |
152 | } else { |
84ff88cf |
153 | warn "action_namespace called as class method"; |
154 | # if the following won't change at runtime it should be lazy_building thing |
0bc4c3ab |
155 | return $self->config->{namespace} if exists $self->config->{namespace}; |
156 | } |
234763d4 |
157 | |
0bc4c3ab |
158 | #the following looks like a possible target for a default setting. i am not |
159 | #making the below the builder because i don't know if $c will vary from |
0fc2d522 |
160 | #call to call, which would affect case sensitivity settings -- groditi |
0bc4c3ab |
161 | my $case_s; |
162 | if( $c ){ |
163 | $case_s = $c->config->{case_sensitive}; |
164 | } else { |
165 | if ($self->isa('Catalyst')) { |
166 | $case_s = $self->config->{case_sensitive}; |
167 | } else { |
168 | if (ref $self) { |
169 | $case_s = $self->_application->config->{case_sensitive}; |
170 | } else { |
171 | confess("Can't figure out case_sensitive setting"); |
172 | } |
173 | } |
234763d4 |
174 | } |
0bc4c3ab |
175 | |
84ff88cf |
176 | my $namespace = Catalyst::Utils::class2prefix(ref($self) || $self, $case_s) || ''; |
177 | $self->$orig($namespace) if ref($self); |
178 | return $namespace; |
0bc4c3ab |
179 | }; |
180 | |
0fc2d522 |
181 | #Once again, this is probably better written as a builder method |
0bc4c3ab |
182 | around path_prefix => sub { |
183 | my $orig = shift; |
184 | my $self = shift; |
5fb67d52 |
185 | if( ref($self) ){ |
0bc4c3ab |
186 | return $self->$orig if $self->has_path_prefix; |
187 | } else { |
5fb67d52 |
188 | return $self->config->{path} if exists $self->config->{path}; |
0bc4c3ab |
189 | } |
84ff88cf |
190 | my $namespace = $self->action_namespace(@_); |
191 | $self->$orig($namespace) if ref($self); |
192 | return $namespace; |
0bc4c3ab |
193 | }; |
234763d4 |
194 | |
195 | |
196 | sub register_actions { |
197 | my ( $self, $c ) = @_; |
198 | my $class = ref $self || $self; |
5fb67d52 |
199 | #this is still not correct for some reason. |
234763d4 |
200 | my $namespace = $self->action_namespace($c); |
84ff88cf |
201 | my $meta = $self->meta; |
202 | my %methods = map{ $_->{code}->body => $_->{name} } |
2361ed67 |
203 | grep {$_->{class} ne 'Moose::Object'} #ignore Moose::Object methods |
84ff88cf |
204 | $meta->compute_all_applicable_methods; |
205 | |
234763d4 |
206 | |
207 | # Advanced inheritance support for plugins and the like |
0bc4c3ab |
208 | #moose todo: migrate to eliminate CDI compat |
234763d4 |
209 | my @action_cache; |
84ff88cf |
210 | for my $isa ( $meta->superclasses, $class ) { |
211 | if(my $coderef = $isa->can('_action_cache')){ |
212 | push(@action_cache, @{ $isa->$coderef }); |
234763d4 |
213 | } |
214 | } |
215 | |
216 | foreach my $cache (@action_cache) { |
217 | my $code = $cache->[0]; |
218 | my $method = delete $methods{$code}; # avoid dupe registers |
219 | next unless $method; |
220 | my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } ); |
221 | if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) { |
222 | $c->log->debug( 'Bad action definition "' |
223 | . join( ' ', @{ $cache->[1] } ) |
224 | . qq/" for "$class->$method"/ ) |
225 | if $c->debug; |
226 | next; |
227 | } |
5fb67d52 |
228 | my $reverse = $namespace ? "${namespace}/${method}" : $method; |
234763d4 |
229 | my $action = $self->create_action( |
230 | name => $method, |
231 | code => $code, |
232 | reverse => $reverse, |
233 | namespace => $namespace, |
234 | class => $class, |
235 | attributes => $attrs, |
236 | ); |
237 | |
238 | $c->dispatcher->register( $c, $action ); |
239 | } |
240 | } |
241 | |
242 | sub create_action { |
243 | my $self = shift; |
244 | my %args = @_; |
245 | |
246 | my $class = (exists $args{attributes}{ActionClass} |
247 | ? $args{attributes}{ActionClass}[0] |
248 | : $self->_action_class); |
249 | |
2361ed67 |
250 | Class::MOP::load_class($class); |
234763d4 |
251 | return $class->new( \%args ); |
252 | } |
253 | |
254 | sub _parse_attrs { |
255 | my ( $self, $c, $name, @attrs ) = @_; |
256 | |
257 | my %raw_attributes; |
258 | |
259 | foreach my $attr (@attrs) { |
260 | |
261 | # Parse out :Foo(bar) into Foo => bar etc (and arrayify) |
262 | |
263 | if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) ) |
264 | { |
265 | |
266 | if ( defined $value ) { |
267 | ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ ); |
268 | } |
269 | push( @{ $raw_attributes{$key} }, $value ); |
270 | } |
271 | } |
272 | |
0bc4c3ab |
273 | #I know that the original behavior was to ignore action if actions was set |
274 | # but i actually think this may be a little more sane? we can always remove |
275 | # the merge behavior quite easily and go back to having actions have |
276 | # presedence over action by modifying the keys. i honestly think this is |
277 | # superior while mantaining really high degree of compat |
278 | my $actions; |
279 | if( ref($self) ) { |
280 | $actions = $self->actions; |
281 | } else { |
282 | my $cfg = $self->config; |
283 | $actions = $self->merge_config_hashes($cfg->{actions}, $cfg->{action}); |
234763d4 |
284 | } |
285 | |
0bc4c3ab |
286 | %raw_attributes = ((exists $actions->{'*'} ? %{$actions->{'*'}} : ()), |
287 | %raw_attributes, |
288 | (exists $actions->{$name} ? %{$actions->{$name}} : ())); |
289 | |
290 | |
234763d4 |
291 | my %final_attributes; |
292 | |
293 | foreach my $key (keys %raw_attributes) { |
294 | |
295 | my $raw = $raw_attributes{$key}; |
296 | |
297 | foreach my $value (ref($raw) eq 'ARRAY' ? @$raw : $raw) { |
298 | |
299 | my $meth = "_parse_${key}_attr"; |
0bc4c3ab |
300 | if ( my $code = $self->can($meth) ) { |
301 | ( $key, $value ) = $self->$code( $c, $name, $value ); |
234763d4 |
302 | } |
303 | push( @{ $final_attributes{$key} }, $value ); |
304 | } |
305 | } |
306 | |
307 | return \%final_attributes; |
308 | } |
309 | |
310 | sub _parse_Global_attr { |
311 | my ( $self, $c, $name, $value ) = @_; |
312 | return $self->_parse_Path_attr( $c, $name, "/$name" ); |
313 | } |
314 | |
315 | sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); } |
316 | |
317 | sub _parse_Local_attr { |
318 | my ( $self, $c, $name, $value ) = @_; |
319 | return $self->_parse_Path_attr( $c, $name, $name ); |
320 | } |
321 | |
322 | sub _parse_Relative_attr { shift->_parse_Local_attr(@_); } |
323 | |
324 | sub _parse_Path_attr { |
325 | my ( $self, $c, $name, $value ) = @_; |
326 | $value ||= ''; |
327 | if ( $value =~ m!^/! ) { |
328 | return ( 'Path', $value ); |
329 | } |
330 | elsif ( length $value ) { |
331 | return ( 'Path', join( '/', $self->path_prefix($c), $value ) ); |
332 | } |
333 | else { |
334 | return ( 'Path', $self->path_prefix($c) ); |
335 | } |
336 | } |
337 | |
338 | sub _parse_Regex_attr { |
339 | my ( $self, $c, $name, $value ) = @_; |
340 | return ( 'Regex', $value ); |
341 | } |
342 | |
343 | sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); } |
344 | |
345 | sub _parse_LocalRegex_attr { |
346 | my ( $self, $c, $name, $value ) = @_; |
347 | unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; } |
348 | return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" ); |
349 | } |
350 | |
351 | sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); } |
352 | |
353 | sub _parse_ActionClass_attr { |
354 | my ( $self, $c, $name, $value ) = @_; |
355 | unless ( $value =~ s/^\+// ) { |
356 | $value = join('::', $self->_action_class, $value ); |
357 | } |
358 | return ( 'ActionClass', $value ); |
359 | } |
360 | |
9287719b |
361 | sub _parse_MyAction_attr { |
362 | my ( $self, $c, $name, $value ) = @_; |
363 | |
364 | my $appclass = Catalyst::Utils::class2appclass($self); |
365 | $value = "${appclass}::Action::${value}"; |
234763d4 |
366 | |
9287719b |
367 | return ( 'ActionClass', $value ); |
368 | } |
234763d4 |
369 | |
0fc2d522 |
370 | no Moose; |
371 | |
234763d4 |
372 | 1; |
373 | |
374 | __END__ |
375 | |
376 | =head1 CONFIGURATION |
377 | |
a269e0c2 |
378 | Like any other L<Catalyst::Component>, controllers have a config hash, |
379 | accessible through $self->config from the controller actions. Some |
380 | settings are in use by the Catalyst framework: |
234763d4 |
381 | |
382 | =head2 namespace |
383 | |
a269e0c2 |
384 | This specifies the internal namespace the controller should be bound |
385 | to. By default the controller is bound to the URI version of the |
386 | controller name. For instance controller 'MyApp::Controller::Foo::Bar' |
387 | will be bound to 'foo/bar'. The default Root controller is an example |
388 | of setting namespace to '' (the null string). |
234763d4 |
389 | |
ac5c933b |
390 | =head2 path |
234763d4 |
391 | |
392 | Sets 'path_prefix', as described below. |
393 | |
394 | =head1 METHODS |
395 | |
396 | =head2 $class->new($app, @args) |
397 | |
398 | Proxies through to NEXT::new and stashes the application instance as |
399 | $self->_application. |
400 | |
401 | =head2 $self->action_for('name') |
402 | |
a269e0c2 |
403 | Returns the Catalyst::Action object (if any) for a given method name |
404 | in this component. |
234763d4 |
405 | |
406 | =head2 $self->register_actions($c) |
407 | |
a269e0c2 |
408 | Finds all applicable actions for this component, creates |
409 | Catalyst::Action objects (using $self->create_action) for them and |
410 | registers them with $c->dispatcher. |
234763d4 |
411 | |
412 | =head2 $self->action_namespace($c) |
413 | |
a269e0c2 |
414 | Returns the private namespace for actions in this component. Defaults |
415 | to a value from the controller name (for |
416 | e.g. MyApp::Controller::Foo::Bar becomes "foo/bar") or can be |
417 | overridden from the "namespace" config key. |
234763d4 |
418 | |
419 | |
420 | =head2 $self->path_prefix($c) |
421 | |
a269e0c2 |
422 | Returns the default path prefix for :Local, :LocalRegex and relative |
423 | :Path actions in this component. Defaults to the action_namespace or |
424 | can be overridden from the "path" config key. |
234763d4 |
425 | |
426 | =head2 $self->create_action(%args) |
427 | |
a269e0c2 |
428 | Called with a hash of data to be use for construction of a new |
429 | Catalyst::Action (or appropriate sub/alternative class) object. |
234763d4 |
430 | |
431 | Primarily designed for the use of register_actions. |
432 | |
a269e0c2 |
433 | =head2 $self->_application |
234763d4 |
434 | |
435 | =head2 $self->_app |
436 | |
437 | Returns the application instance stored by C<new()> |
5ee249f2 |
438 | |
439 | =head1 AUTHOR |
440 | |
441 | Sebastian Riedel, C<sri@oook.de> |
234763d4 |
442 | Marcus Ramberg C<mramberg@cpan.org> |
5ee249f2 |
443 | |
444 | =head1 COPYRIGHT |
445 | |
a269e0c2 |
446 | This program is free software, you can redistribute it and/or modify |
447 | it under the same terms as Perl itself. |
5ee249f2 |
448 | |
449 | =cut |