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