7536dc66aee3eecd8e5ed99c41579aad3b22b1c3
[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->execute($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->execute($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->execute($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->execute($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 =head1 METHODS
76
77 =head2 $self->action_namespace($c)
78
79 =cut
80
81 sub action_namespace {
82     my ( $self, $c ) = @_;
83     return $self->config->{namespace} if exists $self->config->{namespace};
84     return Catalyst::Utils::class2prefix( ref($self) || $self,
85         $c->config->{case_sensitive} )
86       || '';
87 }
88
89 =head2 $self->path_prefix($c)
90
91 =cut
92
93 sub path_prefix { shift->action_namespace(@_); }
94
95 =head2 $self->register_actions($c)
96
97 =cut
98
99 sub register_actions {
100     my ( $self, $c ) = @_;
101     my $class = ref $self || $self;
102     my $namespace = $self->action_namespace($c);
103     my %methods;
104     $methods{ $self->can($_) } = $_
105       for @{ Class::Inspector->methods($class) || [] };
106
107     # Advanced inheritance support for plugins and the like
108     my @action_cache;
109     {
110         no strict 'refs';
111         for my $isa ( @{"$class\::ISA"}, $class ) {
112             push @action_cache, @{ $isa->_action_cache }
113               if $isa->can('_action_cache');
114         }
115     }
116
117     foreach my $cache (@action_cache) {
118         my $code   = $cache->[0];
119         my $method = $methods{$code};
120         next unless $method;
121         my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
122         if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
123             $c->log->debug( 'Bad action definition "'
124                   . join( ' ', @{ $cache->[1] } )
125                   . qq/" for "$class->$method"/ )
126               if $c->debug;
127             next;
128         }
129         my $reverse = $namespace ? "$namespace/$method" : $method;
130         my $action = $self->_action_class->new(
131             {
132                 name       => $method,
133                 code       => $code,
134                 reverse    => $reverse,
135                 namespace  => $namespace,
136                 class      => $class,
137                 attributes => $attrs,
138             }
139         );
140         $c->dispatcher->register( $c, $action );
141     }
142 }
143
144 sub _parse_attrs {
145     my ( $self, $c, $name, @attrs ) = @_;
146     my %attributes;
147     foreach my $attr (@attrs) {
148
149         # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
150
151         if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
152         {
153
154             if ( defined $value ) {
155                 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
156             }
157             my $meth = "_parse_${key}_attr";
158             if ( $self->can($meth) ) {
159                 ( $key, $value ) = $self->$meth( $c, $name, $value );
160             }
161             push( @{ $attributes{$key} }, $value );
162         }
163     }
164     return \%attributes;
165 }
166
167 sub _parse_Global_attr {
168     my ( $self, $c, $name, $value ) = @_;
169     return $self->_parse_Path_attr( $c, $name, "/$name" );
170 }
171
172 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
173
174 sub _parse_Local_attr {
175     my ( $self, $c, $name, $value ) = @_;
176     return $self->_parse_Path_attr( $c, $name, $name );
177 }
178
179 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
180
181 sub _parse_Path_attr {
182     my ( $self, $c, $name, $value ) = @_;
183     $value ||= '';
184     if ( $value =~ m!^/! ) {
185         return ( 'Path', $value );
186     }
187     elsif ( length $value ) {
188         return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
189     }
190     else {
191         return ( 'Path', $self->path_prefix($c) );
192     }
193 }
194
195 sub _parse_Regex_attr {
196     my ( $self, $c, $name, $value ) = @_;
197     return ( 'Regex', $value );
198 }
199
200 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
201
202 sub _parse_LocalRegex_attr {
203     my ( $self, $c, $name, $value ) = @_;
204     unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
205     return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
206 }
207
208 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
209
210 =head1 SEE ALSO
211
212 L<Catalyst>, L<Catalyst::Controller>.
213
214 =head1 AUTHOR
215
216 Sebastian Riedel, C<sri@cpan.org>
217 Marcus Ramberg, C<mramberg@cpan.org>
218 Matt S Trout, C<mst@shadowcatsystems.co.uk>
219
220 =head1 COPYRIGHT
221
222 This program is free software, you can redistribute it and/or modify it under
223 the same terms as Perl itself.
224
225 =cut
226
227 1;