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