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