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