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