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