improved docs for action_namespace and path_prefix and made path configurable
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
1 package Catalyst::Base;
2
3 use strict;
4 use base qw/Catalyst::Component Catalyst::AttrContainer Class::Accessor::Fast/;
5
6 use Catalyst::Exception;
7 use Catalyst::Utils;
8 use Class::Inspector;
9 use NEXT;
10
11 __PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class/;
12
13 __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
14 __PACKAGE__->_action_class('Catalyst::Action');
15
16 sub _DISPATCH : Private {
17     my ( $self, $c ) = @_;
18
19     foreach my $disp ( @{ $self->_dispatch_steps } ) {
20         last unless $c->forward($disp);
21     }
22
23     $c->forward('_END');
24 }
25
26 sub _BEGIN : Private {
27     my ( $self, $c ) = @_;
28     my $begin = ( $c->get_actions( 'begin', $c->namespace ) )[-1];
29     return 1 unless $begin;
30     $begin->dispatch( $c );
31     return !@{ $c->error };
32 }
33
34 sub _AUTO : Private {
35     my ( $self, $c ) = @_;
36     my @auto = $c->get_actions( 'auto', $c->namespace );
37     foreach my $auto (@auto) {
38         $auto->dispatch( $c );
39         return 0 unless $c->state;
40     }
41     return 1;
42 }
43
44 sub _ACTION : Private {
45     my ( $self, $c ) = @_;
46     if (   ref $c->action
47         && $c->action->can('execute')
48         && $c->req->action )
49     {
50         $c->action->dispatch( $c );
51     }
52     return !@{ $c->error };
53 }
54
55 sub _END : Private {
56     my ( $self, $c ) = @_;
57     my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
58     return 1 unless $end;
59     $end->dispatch( $c );
60     return !@{ $c->error };
61 }
62
63 =head1 NAME
64
65 Catalyst::Base - Catalyst Base Class
66
67 =head1 SYNOPSIS
68
69 See L<Catalyst>
70
71 =head1 DESCRIPTION
72
73 Catalyst Base Class
74
75 This is the base class for all Catalyst components. It also handles 
76 dispatch of actions for controllers.
77
78 =head1 METHODS
79
80 =head2 $self->action_namespace($c)
81
82 Returns the private namespace for actions in this component. Defaults to a value
83 from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes
84 "foo/bar") or can be overriden from the "namespace" config key.
85
86 =cut
87
88 sub action_namespace {
89     my ( $self, $c ) = @_;
90     my $hash = (ref $self ? $self : $self->config); # hate app-is-class
91     return $hash->{namespace} if exists $hash->{namespace};
92     return Catalyst::Utils::class2prefix( ref($self) || $self,
93         $c->config->{case_sensitive} )
94       || '';
95 }
96
97 =head2 $self->path_prefix($c)
98
99 Returns the default path prefix for :Local, :LocalRegex and relative :Path
100 actions in this component. Defaults to the action_namespace or can be
101 overriden from the "path" config key.
102
103 =cut
104
105 sub 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 }
111
112 =head2 $self->register_actions($c)
113
114 register all actions for this component based on a given context.
115
116 =cut
117
118 sub register_actions {
119     my ( $self, $c ) = @_;
120     my $class = ref $self || $self;
121     my $namespace = $self->action_namespace($c);
122     my %methods;
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];
138         my $method = $methods{$code};
139         next unless $method;
140         my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
141         if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
142             $c->log->debug( 'Bad action definition "'
143                   . join( ' ', @{ $cache->[1] } )
144                   . qq/" for "$class->$method"/ )
145               if $c->debug;
146             next;
147         }
148         my $reverse = $namespace ? "$namespace/$method" : $method;
149         my $action = $self->create_action(
150             name       => $method,
151             code       => $code,
152             reverse    => $reverse,
153             namespace  => $namespace,
154             class      => $class,
155             attributes => $attrs,
156         );
157
158         $c->dispatcher->register( $c, $action );
159     }
160 }
161
162 sub create_action {
163     my $self = shift;
164     my %args = @_;
165
166     my $class = (exists $args{attributes}{ActionClass}
167                     ? $args{attributes}{ActionClass}[0]
168                     : $self->_action_class);
169
170     unless ( Class::Inspector->loaded($class) ) {
171         require Class::Inspector->filename($class);
172     }
173     
174     return $class->new( \%args );
175 }
176
177 sub _parse_attrs {
178     my ( $self, $c, $name, @attrs ) = @_;
179
180     my %raw_attributes;
181
182     foreach my $attr (@attrs) {
183
184         # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
185
186         if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
187         {
188
189             if ( defined $value ) {
190                 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
191             }
192             push( @{ $raw_attributes{$key} }, $value );
193         }
194     }
195
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
205     my %final_attributes;
206
207     foreach my $key (keys %raw_attributes) {
208
209         my $raw = $raw_attributes{$key};
210
211         foreach my $value (ref($raw) ? @$raw : $raw) {
212
213             my $meth = "_parse_${key}_attr";
214             if ( $self->can($meth) ) {
215                 ( $key, $value ) = $self->$meth( $c, $name, $value );
216             }
217             push( @{ $final_attributes{$key} }, $value );
218         }
219     }
220
221     return \%final_attributes;
222 }
223
224 sub _parse_Global_attr {
225     my ( $self, $c, $name, $value ) = @_;
226     return $self->_parse_Path_attr( $c, $name, "/$name" );
227 }
228
229 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
230
231 sub _parse_Local_attr {
232     my ( $self, $c, $name, $value ) = @_;
233     return $self->_parse_Path_attr( $c, $name, $name );
234 }
235
236 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
237
238 sub _parse_Path_attr {
239     my ( $self, $c, $name, $value ) = @_;
240     $value ||= '';
241     if ( $value =~ m!^/! ) {
242         return ( 'Path', $value );
243     }
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
252 sub _parse_Regex_attr {
253     my ( $self, $c, $name, $value ) = @_;
254     return ( 'Regex', $value );
255 }
256
257 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
258
259 sub _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
265 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
266
267 sub _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
275 =head1 SEE ALSO
276
277 L<Catalyst>, L<Catalyst::Controller>.
278
279 =head1 AUTHOR
280
281 Sebastian Riedel, C<sri@cpan.org>
282 Marcus Ramberg, C<mramberg@cpan.org>
283 Matt S Trout, C<mst@shadowcatsystems.co.uk>
284
285 =head1 COPYRIGHT
286
287 This program is free software, you can redistribute it and/or modify it under
288 the same terms as Perl itself.
289
290 =cut
291
292 1;