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