document $c->detach with no args
[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
649fd1fa 83This is the base class for all Catalyst components. It also handles
84dispatch of actions for controllers.
85
aad72cc9 86=head1 METHODS
87
d4c74714 88=head2 $class->new($app, @args)
89
90Proxies through to NEXT::new and stashes the application instance as
91$self->{application}.
92
2a563513 93=head2 $self->action_for('name')
94
95Returns the Catalyst::Action object (if any) for a given method name in
96this component.
97
98=cut
99
100sub action_for {
101 my ( $self, $name ) = @_;
102 my $app = ($self->isa('Catalyst') ? $self : $self->{application});
103 return $app->dispatcher->get_action($name, $self->action_namespace);
104}
105
b5ecfcf0 106=head2 $self->action_namespace($c)
aad72cc9 107
1b1636b5 108Returns the private namespace for actions in this component. Defaults to a value
109from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes
110"foo/bar") or can be overriden from the "namespace" config key.
649fd1fa 111
aad72cc9 112=cut
113
91d4abc5 114sub action_namespace {
115 my ( $self, $c ) = @_;
2a563513 116 unless ( $c ) {
117 $c = ($self->isa('Catalyst') ? $self : $self->{application});
118 }
c02f7490 119 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
9488795c 120 return $hash->{namespace} if exists $hash->{namespace};
1cf67b31 121 return Catalyst::Utils::class2prefix( ref($self) || $self,
57e45928 122 $c->config->{case_sensitive} )
123 || '';
91d4abc5 124}
125
27708fc5 126=head2 $self->path_prefix($c)
127
1b1636b5 128Returns the default path prefix for :Local, :LocalRegex and relative :Path
129actions in this component. Defaults to the action_namespace or can be
130overriden from the "path" config key.
649fd1fa 131
27708fc5 132=cut
133
1b1636b5 134sub path_prefix {
135 my ( $self, $c ) = @_;
2a563513 136 unless ( $c ) {
137 $c = ($self->isa('Catalyst') ? $self : $self->{application});
138 }
1b1636b5 139 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
140 return $hash->{path} if exists $hash->{path};
141 return shift->action_namespace(@_);
142}
27708fc5 143
b5ecfcf0 144=head2 $self->register_actions($c)
aad72cc9 145
d4c74714 146Finds all applicable actions for this component, creates Catalyst::Action
147objects (using $self->create_action) for them and registers them with
148$c->dispatcher.
649fd1fa 149
aad72cc9 150=cut
151
91d4abc5 152sub register_actions {
153 my ( $self, $c ) = @_;
154 my $class = ref $self || $self;
57e45928 155 my $namespace = $self->action_namespace($c);
91d4abc5 156 my %methods;
57e45928 157 $methods{ $self->can($_) } = $_
158 for @{ Class::Inspector->methods($class) || [] };
159
160 # Advanced inheritance support for plugins and the like
161 my @action_cache;
162 {
163 no strict 'refs';
164 for my $isa ( @{"$class\::ISA"}, $class ) {
165 push @action_cache, @{ $isa->_action_cache }
166 if $isa->can('_action_cache');
167 }
168 }
169
170 foreach my $cache (@action_cache) {
171 my $code = $cache->[0];
c5b74a51 172 my $method = delete $methods{$code}; # avoid dupe registers
91d4abc5 173 next unless $method;
5b707014 174 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
57e45928 175 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
91d4abc5 176 $c->log->debug( 'Bad action definition "'
57e45928 177 . join( ' ', @{ $cache->[1] } )
91d4abc5 178 . qq/" for "$class->$method"/ )
179 if $c->debug;
180 next;
181 }
182 my $reverse = $namespace ? "$namespace/$method" : $method;
f40acefd 183 my $action = $self->create_action(
184 name => $method,
185 code => $code,
186 reverse => $reverse,
187 namespace => $namespace,
188 class => $class,
189 attributes => $attrs,
91d4abc5 190 );
f40acefd 191
57e45928 192 $c->dispatcher->register( $c, $action );
91d4abc5 193 }
194}
195
d4c74714 196=head2 $self->create_action(%args)
197
198Called with a hash of data to be use for construction of a new Catalyst::Action
199(or appropriate sub/alternative class) object.
200
201Primarily designed for the use of register_actions.
202
203=cut
204
f40acefd 205sub create_action {
206 my $self = shift;
734a1e11 207 my %args = @_;
208
209 my $class = (exists $args{attributes}{ActionClass}
210 ? $args{attributes}{ActionClass}[0]
211 : $self->_action_class);
212
16d306fa 213 unless ( Class::Inspector->loaded($class) ) {
1e514a51 214 require Class::Inspector->filename($class);
734a1e11 215 }
216
217 return $class->new( \%args );
f40acefd 218}
219
91d4abc5 220sub _parse_attrs {
5b707014 221 my ( $self, $c, $name, @attrs ) = @_;
d030cb97 222
223 my %raw_attributes;
224
91d4abc5 225 foreach my $attr (@attrs) {
226
227 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
228
aad72cc9 229 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
230 {
91d4abc5 231
232 if ( defined $value ) {
233 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
234 }
d030cb97 235 push( @{ $raw_attributes{$key} }, $value );
236 }
237 }
238
c02f7490 239 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
240
241 if (exists $hash->{actions} || exists $hash->{action}) {
242 my $a = $hash->{actions} || $hash->{action};
243 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
244 %raw_attributes,
245 (exists $a->{$name} ? %{$a->{$name}} : ()));
246 }
247
d030cb97 248 my %final_attributes;
249
250 foreach my $key (keys %raw_attributes) {
251
c02f7490 252 my $raw = $raw_attributes{$key};
253
6d316c39 254 foreach my $value (ref($raw) eq 'ARRAY' ? @$raw : $raw) {
d030cb97 255
5b707014 256 my $meth = "_parse_${key}_attr";
257 if ( $self->can($meth) ) {
258 ( $key, $value ) = $self->$meth( $c, $name, $value );
259 }
d030cb97 260 push( @{ $final_attributes{$key} }, $value );
91d4abc5 261 }
262 }
d030cb97 263
264 return \%final_attributes;
91d4abc5 265}
266
5b707014 267sub _parse_Global_attr {
268 my ( $self, $c, $name, $value ) = @_;
269 return $self->_parse_Path_attr( $c, $name, "/$name" );
270}
271
27708fc5 272sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
5b707014 273
274sub _parse_Local_attr {
275 my ( $self, $c, $name, $value ) = @_;
276 return $self->_parse_Path_attr( $c, $name, $name );
277}
278
27708fc5 279sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
5b707014 280
281sub _parse_Path_attr {
282 my ( $self, $c, $name, $value ) = @_;
283 $value ||= '';
284 if ( $value =~ m!^/! ) {
285 return ( 'Path', $value );
5b707014 286 }
27708fc5 287 elsif ( length $value ) {
288 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
289 }
290 else {
291 return ( 'Path', $self->path_prefix($c) );
292 }
293}
294
295sub _parse_Regex_attr {
296 my ( $self, $c, $name, $value ) = @_;
297 return ( 'Regex', $value );
5b707014 298}
299
27708fc5 300sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
301
302sub _parse_LocalRegex_attr {
303 my ( $self, $c, $name, $value ) = @_;
304 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
305 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
306}
307
308sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
309
734a1e11 310sub _parse_ActionClass_attr {
311 my ( $self, $c, $name, $value ) = @_;
312 unless ( $value =~ s/^\+// ) {
313 $value = join('::', $self->_action_class, $value );
314 }
315 return ( 'ActionClass', $value );
316}
317
fc7ec1d9 318=head1 SEE ALSO
319
e7f1cf73 320L<Catalyst>, L<Catalyst::Controller>.
fc7ec1d9 321
322=head1 AUTHOR
323
324Sebastian Riedel, C<sri@cpan.org>
61b1e958 325Marcus Ramberg, C<mramberg@cpan.org>
ba599d1c 326Matt S Trout, C<mst@shadowcatsystems.co.uk>
fc7ec1d9 327
328=head1 COPYRIGHT
329
330This program is free software, you can redistribute it and/or modify it under
331the same terms as Perl itself.
332
333=cut
334
3351;