added actions-paths-from-config
[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
aad72cc9 63=head1 NAME
64
65Catalyst::Base - Catalyst Base Class
66
67=head1 SYNOPSIS
68
69See L<Catalyst>
70
71=head1 DESCRIPTION
72
73Catalyst Base Class
74
649fd1fa 75This is the base class for all Catalyst components. It also handles
76dispatch of actions for controllers.
77
aad72cc9 78=head1 METHODS
79
b5ecfcf0 80=head2 $self->action_namespace($c)
aad72cc9 81
649fd1fa 82Determine the namespace for actions in this component.
83
aad72cc9 84=cut
85
91d4abc5 86sub action_namespace {
87 my ( $self, $c ) = @_;
c02f7490 88 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
9488795c 89 return $hash->{namespace} if exists $hash->{namespace};
1cf67b31 90 return Catalyst::Utils::class2prefix( ref($self) || $self,
57e45928 91 $c->config->{case_sensitive} )
92 || '';
91d4abc5 93}
94
27708fc5 95=head2 $self->path_prefix($c)
96
649fd1fa 97alias for action_namespace
98
27708fc5 99=cut
100
101sub path_prefix { shift->action_namespace(@_); }
102
b5ecfcf0 103=head2 $self->register_actions($c)
aad72cc9 104
649fd1fa 105register all actions for this component based on a given context.
106
aad72cc9 107=cut
108
91d4abc5 109sub register_actions {
110 my ( $self, $c ) = @_;
111 my $class = ref $self || $self;
57e45928 112 my $namespace = $self->action_namespace($c);
91d4abc5 113 my %methods;
57e45928 114 $methods{ $self->can($_) } = $_
115 for @{ Class::Inspector->methods($class) || [] };
116
117 # Advanced inheritance support for plugins and the like
118 my @action_cache;
119 {
120 no strict 'refs';
121 for my $isa ( @{"$class\::ISA"}, $class ) {
122 push @action_cache, @{ $isa->_action_cache }
123 if $isa->can('_action_cache');
124 }
125 }
126
127 foreach my $cache (@action_cache) {
128 my $code = $cache->[0];
91d4abc5 129 my $method = $methods{$code};
130 next unless $method;
5b707014 131 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
57e45928 132 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
91d4abc5 133 $c->log->debug( 'Bad action definition "'
57e45928 134 . join( ' ', @{ $cache->[1] } )
91d4abc5 135 . qq/" for "$class->$method"/ )
136 if $c->debug;
137 next;
138 }
139 my $reverse = $namespace ? "$namespace/$method" : $method;
f40acefd 140 my $action = $self->create_action(
141 name => $method,
142 code => $code,
143 reverse => $reverse,
144 namespace => $namespace,
145 class => $class,
146 attributes => $attrs,
91d4abc5 147 );
f40acefd 148
57e45928 149 $c->dispatcher->register( $c, $action );
91d4abc5 150 }
151}
152
f40acefd 153sub create_action {
154 my $self = shift;
734a1e11 155 my %args = @_;
156
157 my $class = (exists $args{attributes}{ActionClass}
158 ? $args{attributes}{ActionClass}[0]
159 : $self->_action_class);
160
16d306fa 161 unless ( Class::Inspector->loaded($class) ) {
1e514a51 162 require Class::Inspector->filename($class);
734a1e11 163 }
164
165 return $class->new( \%args );
f40acefd 166}
167
91d4abc5 168sub _parse_attrs {
5b707014 169 my ( $self, $c, $name, @attrs ) = @_;
d030cb97 170
171 my %raw_attributes;
172
91d4abc5 173 foreach my $attr (@attrs) {
174
175 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
176
aad72cc9 177 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
178 {
91d4abc5 179
180 if ( defined $value ) {
181 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
182 }
d030cb97 183 push( @{ $raw_attributes{$key} }, $value );
184 }
185 }
186
c02f7490 187 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
188
189 if (exists $hash->{actions} || exists $hash->{action}) {
190 my $a = $hash->{actions} || $hash->{action};
191 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
192 %raw_attributes,
193 (exists $a->{$name} ? %{$a->{$name}} : ()));
194 }
195
d030cb97 196 my %final_attributes;
197
198 foreach my $key (keys %raw_attributes) {
199
c02f7490 200 my $raw = $raw_attributes{$key};
201
202 foreach my $value (ref($raw) ? @$raw : $raw) {
d030cb97 203
5b707014 204 my $meth = "_parse_${key}_attr";
205 if ( $self->can($meth) ) {
206 ( $key, $value ) = $self->$meth( $c, $name, $value );
207 }
d030cb97 208 push( @{ $final_attributes{$key} }, $value );
91d4abc5 209 }
210 }
d030cb97 211
212 return \%final_attributes;
91d4abc5 213}
214
5b707014 215sub _parse_Global_attr {
216 my ( $self, $c, $name, $value ) = @_;
217 return $self->_parse_Path_attr( $c, $name, "/$name" );
218}
219
27708fc5 220sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
5b707014 221
222sub _parse_Local_attr {
223 my ( $self, $c, $name, $value ) = @_;
224 return $self->_parse_Path_attr( $c, $name, $name );
225}
226
27708fc5 227sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
5b707014 228
229sub _parse_Path_attr {
230 my ( $self, $c, $name, $value ) = @_;
231 $value ||= '';
232 if ( $value =~ m!^/! ) {
233 return ( 'Path', $value );
5b707014 234 }
27708fc5 235 elsif ( length $value ) {
236 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
237 }
238 else {
239 return ( 'Path', $self->path_prefix($c) );
240 }
241}
242
243sub _parse_Regex_attr {
244 my ( $self, $c, $name, $value ) = @_;
245 return ( 'Regex', $value );
5b707014 246}
247
27708fc5 248sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
249
250sub _parse_LocalRegex_attr {
251 my ( $self, $c, $name, $value ) = @_;
252 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
253 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
254}
255
256sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
257
734a1e11 258sub _parse_ActionClass_attr {
259 my ( $self, $c, $name, $value ) = @_;
260 unless ( $value =~ s/^\+// ) {
261 $value = join('::', $self->_action_class, $value );
262 }
263 return ( 'ActionClass', $value );
264}
265
fc7ec1d9 266=head1 SEE ALSO
267
e7f1cf73 268L<Catalyst>, L<Catalyst::Controller>.
fc7ec1d9 269
270=head1 AUTHOR
271
272Sebastian Riedel, C<sri@cpan.org>
61b1e958 273Marcus Ramberg, C<mramberg@cpan.org>
ba599d1c 274Matt S Trout, C<mst@shadowcatsystems.co.uk>
fc7ec1d9 275
276=head1 COPYRIGHT
277
278This program is free software, you can redistribute it and/or modify it under
279the same terms as Perl itself.
280
281=cut
282
2831;