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