current_view patch.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
CommitLineData
fc7ec1d9 1package Catalyst::Base;
2
3use strict;
158c88c0 4use base qw/Catalyst::Component Catalyst::AttrContainer Class::Accessor::Fast/;
b7783788 5
a2f2cde9 6use Catalyst::Exception;
91d4abc5 7use Catalyst::Utils;
8use Class::Inspector;
fc7ec1d9 9use NEXT;
10
97d6d2bd 11__PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class/;
1143712c 12
0bbb5a1f 13__PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
97d6d2bd 14__PACKAGE__->_action_class('Catalyst::Action');
ac733264 15
0bbb5a1f 16sub _DISPATCH : Private {
ba599d1c 17 my ( $self, $c ) = @_;
1143712c 18
0bbb5a1f 19 foreach my $disp ( @{ $self->_dispatch_steps } ) {
1143712c 20 last unless $c->forward($disp);
ba599d1c 21 }
22
1143712c 23 $c->forward('_END');
24}
fc7ec1d9 25
0bbb5a1f 26sub _BEGIN : Private {
1143712c 27 my ( $self, $c ) = @_;
57e45928 28 my $begin = ( $c->get_actions( 'begin', $c->namespace ) )[-1];
1143712c 29 return 1 unless $begin;
b8f669f3 30 $begin->dispatch( $c );
0bbb5a1f 31 return !@{ $c->error };
1143712c 32}
ba599d1c 33
0bbb5a1f 34sub _AUTO : Private {
1143712c 35 my ( $self, $c ) = @_;
57e45928 36 my @auto = $c->get_actions( 'auto', $c->namespace );
1143712c 37 foreach my $auto (@auto) {
b8f669f3 38 $auto->dispatch( $c );
1143712c 39 return 0 unless $c->state;
ba599d1c 40 }
1143712c 41 return 1;
42}
ba599d1c 43
0bbb5a1f 44sub _ACTION : Private {
1143712c 45 my ( $self, $c ) = @_;
8b76bfcf 46 if ( ref $c->action
31375184 47 && $c->action->can('execute')
8b76bfcf 48 && $c->req->action )
49 {
b8f669f3 50 $c->action->dispatch( $c );
245ae014 51 }
0bbb5a1f 52 return !@{ $c->error };
1143712c 53}
ba599d1c 54
0bbb5a1f 55sub _END : Private {
1143712c 56 my ( $self, $c ) = @_;
57e45928 57 my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
1143712c 58 return 1 unless $end;
b8f669f3 59 $end->dispatch( $c );
0bbb5a1f 60 return !@{ $c->error };
ba599d1c 61}
d70195d8 62
2a563513 63sub new {
64 my $self = shift;
65 my $app = $_[0];
66 my $new = $self->NEXT::new(@_);
67 $new->{application} = $app;
68 return $new;
69}
70
aad72cc9 71=head1 NAME
72
73Catalyst::Base - Catalyst Base Class
74
75=head1 SYNOPSIS
76
77See L<Catalyst>
78
79=head1 DESCRIPTION
80
81Catalyst Base Class
82
ac2b01fb 83This is the base class for all Catalyst components. It is a subclass
84of L<Catalyst::Component> which is the universal base class for
85catalyst components. This module includes handling of dispatching for
86controller actions.
649fd1fa 87
aad72cc9 88=head1 METHODS
89
d4c74714 90=head2 $class->new($app, @args)
91
92Proxies through to NEXT::new and stashes the application instance as
93$self->{application}.
94
2a563513 95=head2 $self->action_for('name')
96
97Returns the Catalyst::Action object (if any) for a given method name in
98this component.
99
100=cut
101
102sub action_for {
103 my ( $self, $name ) = @_;
104 my $app = ($self->isa('Catalyst') ? $self : $self->{application});
105 return $app->dispatcher->get_action($name, $self->action_namespace);
106}
107
b5ecfcf0 108=head2 $self->action_namespace($c)
aad72cc9 109
1b1636b5 110Returns the private namespace for actions in this component. Defaults to a value
111from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes
112"foo/bar") or can be overriden from the "namespace" config key.
649fd1fa 113
aad72cc9 114=cut
115
91d4abc5 116sub action_namespace {
117 my ( $self, $c ) = @_;
2a563513 118 unless ( $c ) {
119 $c = ($self->isa('Catalyst') ? $self : $self->{application});
120 }
c02f7490 121 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
9488795c 122 return $hash->{namespace} if exists $hash->{namespace};
1cf67b31 123 return Catalyst::Utils::class2prefix( ref($self) || $self,
57e45928 124 $c->config->{case_sensitive} )
125 || '';
91d4abc5 126}
127
27708fc5 128=head2 $self->path_prefix($c)
129
1b1636b5 130Returns the default path prefix for :Local, :LocalRegex and relative :Path
131actions in this component. Defaults to the action_namespace or can be
132overriden from the "path" config key.
649fd1fa 133
27708fc5 134=cut
135
1b1636b5 136sub path_prefix {
137 my ( $self, $c ) = @_;
2a563513 138 unless ( $c ) {
139 $c = ($self->isa('Catalyst') ? $self : $self->{application});
140 }
1b1636b5 141 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
142 return $hash->{path} if exists $hash->{path};
143 return shift->action_namespace(@_);
144}
27708fc5 145
b5ecfcf0 146=head2 $self->register_actions($c)
aad72cc9 147
d4c74714 148Finds all applicable actions for this component, creates Catalyst::Action
149objects (using $self->create_action) for them and registers them with
150$c->dispatcher.
649fd1fa 151
aad72cc9 152=cut
153
91d4abc5 154sub register_actions {
155 my ( $self, $c ) = @_;
156 my $class = ref $self || $self;
57e45928 157 my $namespace = $self->action_namespace($c);
91d4abc5 158 my %methods;
57e45928 159 $methods{ $self->can($_) } = $_
160 for @{ Class::Inspector->methods($class) || [] };
161
162 # Advanced inheritance support for plugins and the like
163 my @action_cache;
164 {
165 no strict 'refs';
166 for my $isa ( @{"$class\::ISA"}, $class ) {
167 push @action_cache, @{ $isa->_action_cache }
168 if $isa->can('_action_cache');
169 }
170 }
171
172 foreach my $cache (@action_cache) {
173 my $code = $cache->[0];
c5b74a51 174 my $method = delete $methods{$code}; # avoid dupe registers
91d4abc5 175 next unless $method;
5b707014 176 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
57e45928 177 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
91d4abc5 178 $c->log->debug( 'Bad action definition "'
57e45928 179 . join( ' ', @{ $cache->[1] } )
91d4abc5 180 . qq/" for "$class->$method"/ )
181 if $c->debug;
182 next;
183 }
184 my $reverse = $namespace ? "$namespace/$method" : $method;
f40acefd 185 my $action = $self->create_action(
186 name => $method,
187 code => $code,
188 reverse => $reverse,
189 namespace => $namespace,
190 class => $class,
191 attributes => $attrs,
91d4abc5 192 );
f40acefd 193
57e45928 194 $c->dispatcher->register( $c, $action );
91d4abc5 195 }
196}
197
d4c74714 198=head2 $self->create_action(%args)
199
200Called with a hash of data to be use for construction of a new Catalyst::Action
201(or appropriate sub/alternative class) object.
202
203Primarily designed for the use of register_actions.
204
205=cut
206
f40acefd 207sub create_action {
208 my $self = shift;
734a1e11 209 my %args = @_;
210
211 my $class = (exists $args{attributes}{ActionClass}
212 ? $args{attributes}{ActionClass}[0]
213 : $self->_action_class);
214
16d306fa 215 unless ( Class::Inspector->loaded($class) ) {
1e514a51 216 require Class::Inspector->filename($class);
734a1e11 217 }
218
219 return $class->new( \%args );
f40acefd 220}
221
91d4abc5 222sub _parse_attrs {
5b707014 223 my ( $self, $c, $name, @attrs ) = @_;
d030cb97 224
225 my %raw_attributes;
226
91d4abc5 227 foreach my $attr (@attrs) {
228
229 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
230
aad72cc9 231 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
232 {
91d4abc5 233
234 if ( defined $value ) {
235 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
236 }
d030cb97 237 push( @{ $raw_attributes{$key} }, $value );
238 }
239 }
240
c02f7490 241 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
242
243 if (exists $hash->{actions} || exists $hash->{action}) {
244 my $a = $hash->{actions} || $hash->{action};
245 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
246 %raw_attributes,
247 (exists $a->{$name} ? %{$a->{$name}} : ()));
248 }
249
d030cb97 250 my %final_attributes;
251
252 foreach my $key (keys %raw_attributes) {
253
c02f7490 254 my $raw = $raw_attributes{$key};
255
6d316c39 256 foreach my $value (ref($raw) eq 'ARRAY' ? @$raw : $raw) {
d030cb97 257
5b707014 258 my $meth = "_parse_${key}_attr";
259 if ( $self->can($meth) ) {
260 ( $key, $value ) = $self->$meth( $c, $name, $value );
261 }
d030cb97 262 push( @{ $final_attributes{$key} }, $value );
91d4abc5 263 }
264 }
d030cb97 265
266 return \%final_attributes;
91d4abc5 267}
268
5b707014 269sub _parse_Global_attr {
270 my ( $self, $c, $name, $value ) = @_;
271 return $self->_parse_Path_attr( $c, $name, "/$name" );
272}
273
27708fc5 274sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
5b707014 275
276sub _parse_Local_attr {
277 my ( $self, $c, $name, $value ) = @_;
278 return $self->_parse_Path_attr( $c, $name, $name );
279}
280
27708fc5 281sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
5b707014 282
283sub _parse_Path_attr {
284 my ( $self, $c, $name, $value ) = @_;
285 $value ||= '';
286 if ( $value =~ m!^/! ) {
287 return ( 'Path', $value );
5b707014 288 }
27708fc5 289 elsif ( length $value ) {
290 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
291 }
292 else {
293 return ( 'Path', $self->path_prefix($c) );
294 }
295}
296
297sub _parse_Regex_attr {
298 my ( $self, $c, $name, $value ) = @_;
299 return ( 'Regex', $value );
5b707014 300}
301
27708fc5 302sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
303
304sub _parse_LocalRegex_attr {
305 my ( $self, $c, $name, $value ) = @_;
306 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
307 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
308}
309
310sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
311
734a1e11 312sub _parse_ActionClass_attr {
313 my ( $self, $c, $name, $value ) = @_;
314 unless ( $value =~ s/^\+// ) {
315 $value = join('::', $self->_action_class, $value );
316 }
317 return ( 'ActionClass', $value );
318}
319
fc7ec1d9 320=head1 SEE ALSO
321
e7f1cf73 322L<Catalyst>, L<Catalyst::Controller>.
fc7ec1d9 323
324=head1 AUTHOR
325
326Sebastian Riedel, C<sri@cpan.org>
61b1e958 327Marcus Ramberg, C<mramberg@cpan.org>
ba599d1c 328Matt S Trout, C<mst@shadowcatsystems.co.uk>
fc7ec1d9 329
330=head1 COPYRIGHT
331
332This program is free software, you can redistribute it and/or modify it under
333the same terms as Perl itself.
334
335=cut
336
3371;