65f3120e4864da536822d3745875d771c9202287
[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 sub new {
64   my $self = shift;
65   my $app = $_[0];
66   my $new = $self->NEXT::new(@_);
67   $new->{application} = $app;
68   return $new;
69 }
70
71 =head1 NAME
72
73 Catalyst::Base - Catalyst Base Class
74
75 =head1 SYNOPSIS
76
77 See L<Catalyst>
78
79 =head1 DESCRIPTION
80
81 Catalyst Base Class
82
83 This is the base class for all Catalyst components. It also handles 
84 dispatch of actions for controllers.
85
86 =head1 METHODS
87
88 =head2 $self->action_for('name')
89
90 Returns the Catalyst::Action object (if any) for a given method name in
91 this component.
92
93 =cut
94
95 sub 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
101 =head2 $self->action_namespace($c)
102
103 Returns the private namespace for actions in this component. Defaults to a value
104 from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes
105 "foo/bar") or can be overriden from the "namespace" config key.
106
107 =cut
108
109 sub action_namespace {
110     my ( $self, $c ) = @_;
111     unless ( $c ) {
112         $c = ($self->isa('Catalyst') ? $self : $self->{application});
113     }
114     my $hash = (ref $self ? $self : $self->config); # hate app-is-class
115     return $hash->{namespace} if exists $hash->{namespace};
116     return Catalyst::Utils::class2prefix( ref($self) || $self,
117         $c->config->{case_sensitive} )
118       || '';
119 }
120
121 =head2 $self->path_prefix($c)
122
123 Returns the default path prefix for :Local, :LocalRegex and relative :Path
124 actions in this component. Defaults to the action_namespace or can be
125 overriden from the "path" config key.
126
127 =cut
128
129 sub path_prefix {
130     my ( $self, $c ) = @_;
131     unless ( $c ) {
132         $c = ($self->isa('Catalyst') ? $self : $self->{application});
133     }
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 }
138
139 =head2 $self->register_actions($c)
140
141 register all actions for this component based on a given context.
142
143 =cut
144
145 sub register_actions {
146     my ( $self, $c ) = @_;
147     my $class = ref $self || $self;
148     my $namespace = $self->action_namespace($c);
149     my %methods;
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];
165         my $method = delete $methods{$code}; # avoid dupe registers
166         next unless $method;
167         my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
168         if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
169             $c->log->debug( 'Bad action definition "'
170                   . join( ' ', @{ $cache->[1] } )
171                   . qq/" for "$class->$method"/ )
172               if $c->debug;
173             next;
174         }
175         my $reverse = $namespace ? "$namespace/$method" : $method;
176         my $action = $self->create_action(
177             name       => $method,
178             code       => $code,
179             reverse    => $reverse,
180             namespace  => $namespace,
181             class      => $class,
182             attributes => $attrs,
183         );
184
185         $c->dispatcher->register( $c, $action );
186     }
187 }
188
189 sub create_action {
190     my $self = shift;
191     my %args = @_;
192
193     my $class = (exists $args{attributes}{ActionClass}
194                     ? $args{attributes}{ActionClass}[0]
195                     : $self->_action_class);
196
197     unless ( Class::Inspector->loaded($class) ) {
198         require Class::Inspector->filename($class);
199     }
200     
201     return $class->new( \%args );
202 }
203
204 sub _parse_attrs {
205     my ( $self, $c, $name, @attrs ) = @_;
206
207     my %raw_attributes;
208
209     foreach my $attr (@attrs) {
210
211         # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
212
213         if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
214         {
215
216             if ( defined $value ) {
217                 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
218             }
219             push( @{ $raw_attributes{$key} }, $value );
220         }
221     }
222
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
232     my %final_attributes;
233
234     foreach my $key (keys %raw_attributes) {
235
236         my $raw = $raw_attributes{$key};
237
238         foreach my $value (ref($raw) ? @$raw : $raw) {
239
240             my $meth = "_parse_${key}_attr";
241             if ( $self->can($meth) ) {
242                 ( $key, $value ) = $self->$meth( $c, $name, $value );
243             }
244             push( @{ $final_attributes{$key} }, $value );
245         }
246     }
247
248     return \%final_attributes;
249 }
250
251 sub _parse_Global_attr {
252     my ( $self, $c, $name, $value ) = @_;
253     return $self->_parse_Path_attr( $c, $name, "/$name" );
254 }
255
256 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
257
258 sub _parse_Local_attr {
259     my ( $self, $c, $name, $value ) = @_;
260     return $self->_parse_Path_attr( $c, $name, $name );
261 }
262
263 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
264
265 sub _parse_Path_attr {
266     my ( $self, $c, $name, $value ) = @_;
267     $value ||= '';
268     if ( $value =~ m!^/! ) {
269         return ( 'Path', $value );
270     }
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
279 sub _parse_Regex_attr {
280     my ( $self, $c, $name, $value ) = @_;
281     return ( 'Regex', $value );
282 }
283
284 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
285
286 sub _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
292 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
293
294 sub _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
302 =head1 SEE ALSO
303
304 L<Catalyst>, L<Catalyst::Controller>.
305
306 =head1 AUTHOR
307
308 Sebastian Riedel, C<sri@cpan.org>
309 Marcus Ramberg, C<mramberg@cpan.org>
310 Matt S Trout, C<mst@shadowcatsystems.co.uk>
311
312 =head1 COPYRIGHT
313
314 This program is free software, you can redistribute it and/or modify it under
315 the same terms as Perl itself.
316
317 =cut
318
319 1;