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 __PACKAGE__->mk_accessors( qw/_application/ );
19 *_app = *_application;
21 sub _DISPATCH : Private {
22 my ( $self, $c ) = @_;
24 foreach my $disp ( @{ $self->_dispatch_steps } ) {
25 last unless $c->forward($disp);
31 sub _BEGIN : Private {
32 my ( $self, $c ) = @_;
33 my $begin = ( $c->get_actions( 'begin', $c->namespace ) )[-1];
34 return 1 unless $begin;
35 $begin->dispatch( $c );
36 return !@{ $c->error };
40 my ( $self, $c ) = @_;
41 my @auto = $c->get_actions( 'auto', $c->namespace );
42 foreach my $auto (@auto) {
43 $auto->dispatch( $c );
44 return 0 unless $c->state;
49 sub _ACTION : Private {
50 my ( $self, $c ) = @_;
52 && $c->action->can('execute')
55 $c->action->dispatch( $c );
57 return !@{ $c->error };
61 my ( $self, $c ) = @_;
62 my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
65 return !@{ $c->error };
71 my $new = $self->NEXT::new(@_);
72 $new->_application( $app );
78 Catalyst::Base - Catalyst Base Class
88 This is the base class for all Catalyst components. It is a subclass
89 of L<Catalyst::Component> which is the universal base class for
90 catalyst components. This module includes handling of dispatching for
95 =head2 $class->new($app, @args)
97 Proxies through to NEXT::new and stashes the application instance as
100 =head2 $self->action_for('name')
102 Returns the Catalyst::Action object (if any) for a given method name in
108 my ( $self, $name ) = @_;
109 my $app = ($self->isa('Catalyst') ? $self : $self->_application);
110 return $app->dispatcher->get_action($name, $self->action_namespace);
113 =head2 $self->action_namespace($c)
115 Returns the private namespace for actions in this component. Defaults to a value
116 from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes
117 "foo/bar") or can be overriden from the "namespace" config key.
121 sub action_namespace {
122 my ( $self, $c ) = @_;
124 $c = ($self->isa('Catalyst') ? $self : $self->_application);
126 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
127 return $hash->{namespace} if exists $hash->{namespace};
128 return Catalyst::Utils::class2prefix( ref($self) || $self,
129 $c->config->{case_sensitive} )
133 =head2 $self->path_prefix($c)
135 Returns the default path prefix for :Local, :LocalRegex and relative :Path
136 actions in this component. Defaults to the action_namespace or can be
137 overriden from the "path" config key.
142 my ( $self, $c ) = @_;
144 $c = ($self->isa('Catalyst') ? $self : $self->_application);
146 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
147 return $hash->{path} if exists $hash->{path};
148 return shift->action_namespace(@_);
151 =head2 $self->register_actions($c)
153 Finds all applicable actions for this component, creates Catalyst::Action
154 objects (using $self->create_action) for them and registers them with
159 sub register_actions {
160 my ( $self, $c ) = @_;
161 my $class = ref $self || $self;
162 my $namespace = $self->action_namespace($c);
164 $methods{ $self->can($_) } = $_
165 for @{ Class::Inspector->methods($class) || [] };
167 # Advanced inheritance support for plugins and the like
171 for my $isa ( @{"$class\::ISA"}, $class ) {
172 push @action_cache, @{ $isa->_action_cache }
173 if $isa->can('_action_cache');
177 foreach my $cache (@action_cache) {
178 my $code = $cache->[0];
179 my $method = delete $methods{$code}; # avoid dupe registers
181 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
182 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
183 $c->log->debug( 'Bad action definition "'
184 . join( ' ', @{ $cache->[1] } )
185 . qq/" for "$class->$method"/ )
189 my $reverse = $namespace ? "$namespace/$method" : $method;
190 my $action = $self->create_action(
194 namespace => $namespace,
196 attributes => $attrs,
199 $c->dispatcher->register( $c, $action );
203 =head2 $self->create_action(%args)
205 Called with a hash of data to be use for construction of a new Catalyst::Action
206 (or appropriate sub/alternative class) object.
208 Primarily designed for the use of register_actions.
216 my $class = (exists $args{attributes}{ActionClass}
217 ? $args{attributes}{ActionClass}[0]
218 : $self->_action_class);
220 unless ( Class::Inspector->loaded($class) ) {
221 require Class::Inspector->filename($class);
224 return $class->new( \%args );
228 my ( $self, $c, $name, @attrs ) = @_;
232 foreach my $attr (@attrs) {
234 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
236 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
239 if ( defined $value ) {
240 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
242 push( @{ $raw_attributes{$key} }, $value );
246 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
248 if (exists $hash->{actions} || exists $hash->{action}) {
249 my $a = $hash->{actions} || $hash->{action};
250 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
252 (exists $a->{$name} ? %{$a->{$name}} : ()));
255 my %final_attributes;
257 foreach my $key (keys %raw_attributes) {
259 my $raw = $raw_attributes{$key};
261 foreach my $value (ref($raw) eq 'ARRAY' ? @$raw : $raw) {
263 my $meth = "_parse_${key}_attr";
264 if ( $self->can($meth) ) {
265 ( $key, $value ) = $self->$meth( $c, $name, $value );
267 push( @{ $final_attributes{$key} }, $value );
271 return \%final_attributes;
274 sub _parse_Global_attr {
275 my ( $self, $c, $name, $value ) = @_;
276 return $self->_parse_Path_attr( $c, $name, "/$name" );
279 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
281 sub _parse_Local_attr {
282 my ( $self, $c, $name, $value ) = @_;
283 return $self->_parse_Path_attr( $c, $name, $name );
286 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
288 sub _parse_Path_attr {
289 my ( $self, $c, $name, $value ) = @_;
291 if ( $value =~ m!^/! ) {
292 return ( 'Path', $value );
294 elsif ( length $value ) {
295 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
298 return ( 'Path', $self->path_prefix($c) );
302 sub _parse_Regex_attr {
303 my ( $self, $c, $name, $value ) = @_;
304 return ( 'Regex', $value );
307 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
309 sub _parse_LocalRegex_attr {
310 my ( $self, $c, $name, $value ) = @_;
311 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
312 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
315 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
317 sub _parse_ActionClass_attr {
318 my ( $self, $c, $name, $value ) = @_;
319 unless ( $value =~ s/^\+// ) {
320 $value = join('::', $self->_action_class, $value );
322 return ( 'ActionClass', $value );
325 =head2 $self->_application
329 Returns the application instance stored by C<new()>
333 L<Catalyst>, L<Catalyst::Controller>.
337 Sebastian Riedel, C<sri@cpan.org>
338 Marcus Ramberg, C<mramberg@cpan.org>
339 Matt S Trout, C<mst@shadowcatsystems.co.uk>
343 This program is free software, you can redistribute it and/or modify it under
344 the same terms as Perl itself.