1 package Catalyst::Base;
4 use base qw/Catalyst::Component Catalyst::AttrContainer Class::Accessor::Fast/;
6 use Catalyst::Exception;
11 __PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class/;
13 __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
14 __PACKAGE__->_action_class('Catalyst::Action');
16 sub _DISPATCH : Private {
17 my ( $self, $c ) = @_;
19 foreach my $disp ( @{ $self->_dispatch_steps } ) {
20 last unless $c->forward($disp);
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 };
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;
44 sub _ACTION : Private {
45 my ( $self, $c ) = @_;
47 && $c->action->can('execute')
50 $c->action->dispatch( $c );
52 return !@{ $c->error };
56 my ( $self, $c ) = @_;
57 my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
60 return !@{ $c->error };
65 Catalyst::Base - Catalyst Base Class
75 This is the base class for all Catalyst components. It also handles
76 dispatch of actions for controllers.
80 =head2 $self->action_namespace($c)
82 Determine the namespace for actions in this component.
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} )
94 =head2 $self->path_prefix($c)
96 alias for action_namespace
100 sub path_prefix { shift->action_namespace(@_); }
102 =head2 $self->register_actions($c)
104 register all actions for this component based on a given context.
108 sub register_actions {
109 my ( $self, $c ) = @_;
110 my $class = ref $self || $self;
111 my $namespace = $self->action_namespace($c);
113 $methods{ $self->can($_) } = $_
114 for @{ Class::Inspector->methods($class) || [] };
116 # Advanced inheritance support for plugins and the like
120 for my $isa ( @{"$class\::ISA"}, $class ) {
121 push @action_cache, @{ $isa->_action_cache }
122 if $isa->can('_action_cache');
126 foreach my $cache (@action_cache) {
127 my $code = $cache->[0];
128 my $method = $methods{$code};
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"/ )
138 my $reverse = $namespace ? "$namespace/$method" : $method;
139 my $action = $self->create_action(
143 namespace => $namespace,
145 attributes => $attrs,
148 $c->dispatcher->register( $c, $action );
156 my $class = (exists $args{attributes}{ActionClass}
157 ? $args{attributes}{ActionClass}[0]
158 : $self->_action_class);
160 unless ( Class::Inspector->loaded($class) ) {
161 require Class::Inspector->filename($class);
164 return $class->new( \%args );
168 my ( $self, $c, $name, @attrs ) = @_;
170 foreach my $attr (@attrs) {
172 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
174 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
177 if ( defined $value ) {
178 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
180 my $meth = "_parse_${key}_attr";
181 if ( $self->can($meth) ) {
182 ( $key, $value ) = $self->$meth( $c, $name, $value );
184 push( @{ $attributes{$key} }, $value );
190 sub _parse_Global_attr {
191 my ( $self, $c, $name, $value ) = @_;
192 return $self->_parse_Path_attr( $c, $name, "/$name" );
195 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
197 sub _parse_Local_attr {
198 my ( $self, $c, $name, $value ) = @_;
199 return $self->_parse_Path_attr( $c, $name, $name );
202 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
204 sub _parse_Path_attr {
205 my ( $self, $c, $name, $value ) = @_;
207 if ( $value =~ m!^/! ) {
208 return ( 'Path', $value );
210 elsif ( length $value ) {
211 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
214 return ( 'Path', $self->path_prefix($c) );
218 sub _parse_Regex_attr {
219 my ( $self, $c, $name, $value ) = @_;
220 return ( 'Regex', $value );
223 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
225 sub _parse_LocalRegex_attr {
226 my ( $self, $c, $name, $value ) = @_;
227 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
228 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
231 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
233 sub _parse_ActionClass_attr {
234 my ( $self, $c, $name, $value ) = @_;
235 unless ( $value =~ s/^\+// ) {
236 $value = join('::', $self->_action_class, $value );
238 return ( 'ActionClass', $value );
243 L<Catalyst>, L<Catalyst::Controller>.
247 Sebastian Riedel, C<sri@cpan.org>
248 Marcus Ramberg, C<mramberg@cpan.org>
249 Matt S Trout, C<mst@shadowcatsystems.co.uk>
253 This program is free software, you can redistribute it and/or modify it under
254 the same terms as Perl itself.