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 };
66 my $new = $self->NEXT::new(@_);
67 $new->{application} = $app;
73 Catalyst::Base - Catalyst Base Class
83 This is the base class for all Catalyst components. It also handles
84 dispatch of actions for controllers.
88 =head2 $class->new($app, @args)
90 Proxies through to NEXT::new and stashes the application instance as
93 =head2 $self->action_for('name')
95 Returns the Catalyst::Action object (if any) for a given method name in
101 my ( $self, $name ) = @_;
102 my $app = ($self->isa('Catalyst') ? $self : $self->{application});
103 return $app->dispatcher->get_action($name, $self->action_namespace);
106 =head2 $self->action_namespace($c)
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.
114 sub action_namespace {
115 my ( $self, $c ) = @_;
117 $c = ($self->isa('Catalyst') ? $self : $self->{application});
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} )
126 =head2 $self->path_prefix($c)
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.
135 my ( $self, $c ) = @_;
137 $c = ($self->isa('Catalyst') ? $self : $self->{application});
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(@_);
144 =head2 $self->register_actions($c)
146 Finds all applicable actions for this component, creates Catalyst::Action
147 objects (using $self->create_action) for them and registers them with
152 sub register_actions {
153 my ( $self, $c ) = @_;
154 my $class = ref $self || $self;
155 my $namespace = $self->action_namespace($c);
157 $methods{ $self->can($_) } = $_
158 for @{ Class::Inspector->methods($class) || [] };
160 # Advanced inheritance support for plugins and the like
164 for my $isa ( @{"$class\::ISA"}, $class ) {
165 push @action_cache, @{ $isa->_action_cache }
166 if $isa->can('_action_cache');
170 foreach my $cache (@action_cache) {
171 my $code = $cache->[0];
172 my $method = delete $methods{$code}; # avoid dupe registers
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"/ )
182 my $reverse = $namespace ? "$namespace/$method" : $method;
183 my $action = $self->create_action(
187 namespace => $namespace,
189 attributes => $attrs,
192 $c->dispatcher->register( $c, $action );
196 =head2 $self->create_action(%args)
198 Called with a hash of data to be use for construction of a new Catalyst::Action
199 (or appropriate sub/alternative class) object.
201 Primarily designed for the use of register_actions.
209 my $class = (exists $args{attributes}{ActionClass}
210 ? $args{attributes}{ActionClass}[0]
211 : $self->_action_class);
213 unless ( Class::Inspector->loaded($class) ) {
214 require Class::Inspector->filename($class);
217 return $class->new( \%args );
221 my ( $self, $c, $name, @attrs ) = @_;
225 foreach my $attr (@attrs) {
227 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
229 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
232 if ( defined $value ) {
233 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
235 push( @{ $raw_attributes{$key} }, $value );
239 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
241 if (exists $hash->{actions} || exists $hash->{action}) {
242 my $a = $hash->{actions} || $hash->{action};
243 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
245 (exists $a->{$name} ? %{$a->{$name}} : ()));
248 my %final_attributes;
250 foreach my $key (keys %raw_attributes) {
252 my $raw = $raw_attributes{$key};
254 foreach my $value (ref($raw) ? @$raw : $raw) {
256 my $meth = "_parse_${key}_attr";
257 if ( $self->can($meth) ) {
258 ( $key, $value ) = $self->$meth( $c, $name, $value );
260 push( @{ $final_attributes{$key} }, $value );
264 return \%final_attributes;
267 sub _parse_Global_attr {
268 my ( $self, $c, $name, $value ) = @_;
269 return $self->_parse_Path_attr( $c, $name, "/$name" );
272 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
274 sub _parse_Local_attr {
275 my ( $self, $c, $name, $value ) = @_;
276 return $self->_parse_Path_attr( $c, $name, $name );
279 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
281 sub _parse_Path_attr {
282 my ( $self, $c, $name, $value ) = @_;
284 if ( $value =~ m!^/! ) {
285 return ( 'Path', $value );
287 elsif ( length $value ) {
288 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
291 return ( 'Path', $self->path_prefix($c) );
295 sub _parse_Regex_attr {
296 my ( $self, $c, $name, $value ) = @_;
297 return ( 'Regex', $value );
300 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
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}" );
308 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
310 sub _parse_ActionClass_attr {
311 my ( $self, $c, $name, $value ) = @_;
312 unless ( $value =~ s/^\+// ) {
313 $value = join('::', $self->_action_class, $value );
315 return ( 'ActionClass', $value );
320 L<Catalyst>, L<Catalyst::Controller>.
324 Sebastian Riedel, C<sri@cpan.org>
325 Marcus Ramberg, C<mramberg@cpan.org>
326 Matt S Trout, C<mst@shadowcatsystems.co.uk>
330 This program is free software, you can redistribute it and/or modify it under
331 the same terms as Perl itself.