Update docs with new methods in ::Controller, update TODO
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Controller.pm
CommitLineData
5ee249f2 1package Catalyst::Controller;
2
ae29b412 3use Moose;
a58af53d 4use Moose::Util qw/find_meta/;
ae29b412 5
6use namespace::clean -except => 'meta';
7
ba545c13 8BEGIN { extends qw/Catalyst::Component MooseX::MethodAttributes::Inheritable/; }
234763d4 9
10use Catalyst::Exception;
11use Catalyst::Utils;
ae29b412 12
13with 'Catalyst::Component::ApplicationAttribute';
14
15has path_prefix =>
16 (
17 is => 'rw',
18 isa => 'Str',
19 init_arg => 'path',
20 predicate => 'has_path_prefix',
21 );
22
23has action_namespace =>
24 (
25 is => 'rw',
26 isa => 'Str',
27 init_arg => 'namespace',
28 predicate => 'has_action_namespace',
29 );
30
31has actions =>
32 (
33 is => 'rw',
34 isa => 'HashRef',
35 init_arg => undef,
36 );
37
38sub 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
48Catalyst::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
27ae4114 55 sub foo : Local {
85d9fce6 56 my ($self,$c,@args) = @_;
27ae4114 57 ...
234763d4 58 } # Dispatches to /search/foo
5ee249f2 59
60=head1 DESCRIPTION
61
a269e0c2 62Controllers are where the actions in the Catalyst framework
63reside. Each action is represented by a function with an attribute to
64identify what kind of action it is. See the L<Catalyst::Dispatcher>
65for 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
77sub _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
87sub _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
95sub _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
105sub _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
116sub _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 124sub 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
27ae4114 130#my opinion is that this whole sub really should be a builder method, not
ae29b412 131#something that happens on every call. Anyone else disagree?? -- groditi
132## -- apparently this is all just waiting for app/ctx split
133around 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
164around 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 177sub get_action_methods {
178 my $self = shift;
179 my $meta = find_meta($self);
27ae4114 180 confess("Metaclass for " . ref($meta) ." for " . $meta->name
9ab7d83d 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
187sub register_actions {
188 my ( $self, $c ) = @_;
9ab7d83d 189 $self->register_action_methods( $c, $self->get_action_methods );
190}
191
192sub 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);
234763d4 197
ba545c13 198 foreach my $method (@methods) {
199 my $name = $method->name;
200 my $attributes = $method->attributes;
201 next unless $attributes;
202 my $attrs = $self->_parse_attrs( $c, $name, @{ $attributes } );
234763d4 203 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
204 $c->log->debug( 'Bad action definition "'
ba545c13 205 . join( ' ', @{ $attributes } )
206 . qq/" for "$class->$name"/ )
234763d4 207 if $c->debug;
208 next;
209 }
bc677969 210 my $reverse = $namespace ? "${namespace}/${name}" : $name;
234763d4 211 my $action = $self->create_action(
ba545c13 212 name => $name,
213 code => $method->body,
234763d4 214 reverse => $reverse,
215 namespace => $namespace,
216 class => $class,
217 attributes => $attrs,
218 );
219
220 $c->dispatcher->register( $c, $action );
221 }
222}
223
224sub create_action {
225 my $self = shift;
226 my %args = @_;
227
228 my $class = (exists $args{attributes}{ActionClass}
229 ? $args{attributes}{ActionClass}[0]
230 : $self->_action_class);
231
ae29b412 232 Class::MOP::load_class($class);
234763d4 233 return $class->new( \%args );
234}
235
236sub _parse_attrs {
237 my ( $self, $c, $name, @attrs ) = @_;
238
239 my %raw_attributes;
240
241 foreach my $attr (@attrs) {
242
243 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
244
245 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
246 {
247
248 if ( defined $value ) {
249 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
250 }
251 push( @{ $raw_attributes{$key} }, $value );
252 }
253 }
254
ae29b412 255 #I know that the original behavior was to ignore action if actions was set
256 # but i actually think this may be a little more sane? we can always remove
257 # the merge behavior quite easily and go back to having actions have
258 # presedence over action by modifying the keys. i honestly think this is
259 # superior while mantaining really high degree of compat
260 my $actions;
261 if( ref($self) ) {
262 $actions = $self->actions;
263 } else {
264 my $cfg = $self->config;
265 $actions = $self->merge_config_hashes($cfg->{actions}, $cfg->{action});
234763d4 266 }
267
ae29b412 268 %raw_attributes = ((exists $actions->{'*'} ? %{$actions->{'*'}} : ()),
269 %raw_attributes,
270 (exists $actions->{$name} ? %{$actions->{$name}} : ()));
271
272
234763d4 273 my %final_attributes;
274
275 foreach my $key (keys %raw_attributes) {
276
277 my $raw = $raw_attributes{$key};
278
279 foreach my $value (ref($raw) eq 'ARRAY' ? @$raw : $raw) {
280
281 my $meth = "_parse_${key}_attr";
ae29b412 282 if ( my $code = $self->can($meth) ) {
283 ( $key, $value ) = $self->$code( $c, $name, $value );
234763d4 284 }
285 push( @{ $final_attributes{$key} }, $value );
286 }
287 }
288
289 return \%final_attributes;
290}
291
292sub _parse_Global_attr {
293 my ( $self, $c, $name, $value ) = @_;
294 return $self->_parse_Path_attr( $c, $name, "/$name" );
295}
296
297sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
298
299sub _parse_Local_attr {
300 my ( $self, $c, $name, $value ) = @_;
301 return $self->_parse_Path_attr( $c, $name, $name );
302}
303
304sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
305
306sub _parse_Path_attr {
307 my ( $self, $c, $name, $value ) = @_;
53119b78 308 $value = '' if !defined $value;
234763d4 309 if ( $value =~ m!^/! ) {
310 return ( 'Path', $value );
311 }
312 elsif ( length $value ) {
313 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
314 }
315 else {
316 return ( 'Path', $self->path_prefix($c) );
317 }
318}
319
320sub _parse_Regex_attr {
321 my ( $self, $c, $name, $value ) = @_;
322 return ( 'Regex', $value );
323}
324
325sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
326
327sub _parse_LocalRegex_attr {
328 my ( $self, $c, $name, $value ) = @_;
329 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
19c01ee1 330
331 my $prefix = $self->path_prefix( $c );
332 $prefix .= '/' if length( $prefix );
27ae4114 333
19c01ee1 334 return ( 'Regex', "^${prefix}${value}" );
234763d4 335}
336
337sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
338
f3107403 339sub _parse_Chained_attr {
340 my ($self, $c, $name, $value) = @_;
341
342 if (defined($value) && length($value)) {
343 if ($value eq '.') {
344 $value = '/'.$self->action_namespace($c);
fb56008f 345 } elsif (my ($rel, $rest) = $value =~ /^((?:\.{2}\/)+)(.*)$/) {
eb270c30 346 my @parts = split '/', $self->action_namespace($c);
fb56008f 347 my @levels = split '/', $rel;
348
349 $value = '/'.join('/', @parts[0 .. $#parts - @levels], $rest);
f3107403 350 } elsif ($value !~ m/^\//) {
351 my $action_ns = $self->action_namespace($c);
352
353 if ($action_ns) {
354 $value = '/'.join('/', $action_ns, $value);
355 } else {
356 $value = '/'.$value; # special case namespace '' (root)
357 }
358 }
359 } else {
360 $value = '/'
361 }
362
363 return Chained => $value;
364}
365
9356b981 366sub _parse_ChainedParent_attr {
367 my ($self, $c, $name, $value) = @_;
368 return $self->_parse_Chained_attr($c, $name, '../'.$name);
369}
370
e5d2cfdb 371sub _parse_PathPrefix_attr {
372 my $self = shift;
373 return PathPart => $self->path_prefix;
374}
375
234763d4 376sub _parse_ActionClass_attr {
377 my ( $self, $c, $name, $value ) = @_;
378 unless ( $value =~ s/^\+// ) {
379 $value = join('::', $self->_action_class, $value );
380 }
381 return ( 'ActionClass', $value );
382}
383
9287719b 384sub _parse_MyAction_attr {
385 my ( $self, $c, $name, $value ) = @_;
386
387 my $appclass = Catalyst::Utils::class2appclass($self);
388 $value = "${appclass}::Action::${value}";
234763d4 389
9287719b 390 return ( 'ActionClass', $value );
391}
234763d4 392
ae29b412 393__PACKAGE__->meta->make_immutable;
394
234763d4 3951;
396
397__END__
398
399=head1 CONFIGURATION
400
a269e0c2 401Like any other L<Catalyst::Component>, controllers have a config hash,
402accessible through $self->config from the controller actions. Some
403settings are in use by the Catalyst framework:
234763d4 404
405=head2 namespace
406
a269e0c2 407This specifies the internal namespace the controller should be bound
408to. By default the controller is bound to the URI version of the
409controller name. For instance controller 'MyApp::Controller::Foo::Bar'
410will be bound to 'foo/bar'. The default Root controller is an example
411of setting namespace to '' (the null string).
234763d4 412
27ae4114 413=head2 path
234763d4 414
415Sets 'path_prefix', as described below.
416
417=head1 METHODS
418
c4d02967 419=head2 BUILDARGS ($app, @args)
234763d4 420
c4d02967 421From L<Catalyst::Component::ApplicationAttribute>, stashes the application
422instance as $self->_application.
234763d4 423
424=head2 $self->action_for('name')
425
a269e0c2 426Returns the Catalyst::Action object (if any) for a given method name
427in this component.
234763d4 428
234763d4 429=head2 $self->action_namespace($c)
430
a269e0c2 431Returns the private namespace for actions in this component. Defaults
432to a value from the controller name (for
433e.g. MyApp::Controller::Foo::Bar becomes "foo/bar") or can be
434overridden from the "namespace" config key.
234763d4 435
436
437=head2 $self->path_prefix($c)
438
e5d2cfdb 439Returns the default path prefix for :PathPrefix, :Local, :LocalRegex and
440relative :Path actions in this component. Defaults to the action_namespace or
a269e0c2 441can be overridden from the "path" config key.
234763d4 442
c4d02967 443=head2 $self->register_actions($c)
444
445Finds all applicable actions for this component, creates
446Catalyst::Action objects (using $self->create_action) for them and
447registers them with $c->dispatcher.
448
449=head2 $self->get_action_methods()
450
451Returns a list of L<Moose::Meta::Method> objects, doing the
452L<MooseX::MethodAttributes::Role::Meta::Method> role, which are the set of
453action methods for this package.
454
455=head2 $self->register_action_methods($c, @methods)
456
457Creates action objects for a set of action methods using C< create_action >,
458and registers them with the dispatcher.
459
234763d4 460=head2 $self->create_action(%args)
461
a269e0c2 462Called with a hash of data to be use for construction of a new
463Catalyst::Action (or appropriate sub/alternative class) object.
234763d4 464
a269e0c2 465=head2 $self->_application
234763d4 466
467=head2 $self->_app
468
469Returns the application instance stored by C<new()>
5ee249f2 470
0bf7ab71 471=head1 AUTHORS
5ee249f2 472
0bf7ab71 473Catalyst Contributors, see Catalyst.pm
5ee249f2 474
475=head1 COPYRIGHT
476
a269e0c2 477This program is free software, you can redistribute it and/or modify
478it under the same terms as Perl itself.
5ee249f2 479
480=cut