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