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 is a subclass
84 of L<Catalyst::Component> which is the universal base class for
85 catalyst components. This module includes handling of dispatching for
90 =head2 $class->new($app, @args)
92 Proxies through to NEXT::new and stashes the application instance as
95 =head2 $self->action_for('name')
97 Returns the Catalyst::Action object (if any) for a given method name in
103 my ( $self, $name ) = @_;
104 my $app = ($self->isa('Catalyst') ? $self : $self->{application});
105 return $app->dispatcher->get_action($name, $self->action_namespace);
108 =head2 $self->action_namespace($c)
110 Returns the private namespace for actions in this component. Defaults to a value
111 from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes
112 "foo/bar") or can be overriden from the "namespace" config key.
116 sub action_namespace {
117 my ( $self, $c ) = @_;
119 $c = ($self->isa('Catalyst') ? $self : $self->{application});
121 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
122 return $hash->{namespace} if exists $hash->{namespace};
123 return Catalyst::Utils::class2prefix( ref($self) || $self,
124 $c->config->{case_sensitive} )
128 =head2 $self->path_prefix($c)
130 Returns the default path prefix for :Local, :LocalRegex and relative :Path
131 actions in this component. Defaults to the action_namespace or can be
132 overriden from the "path" config key.
137 my ( $self, $c ) = @_;
139 $c = ($self->isa('Catalyst') ? $self : $self->{application});
141 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
142 return $hash->{path} if exists $hash->{path};
143 return shift->action_namespace(@_);
146 =head2 $self->register_actions($c)
148 Finds all applicable actions for this component, creates Catalyst::Action
149 objects (using $self->create_action) for them and registers them with
154 sub register_actions {
155 my ( $self, $c ) = @_;
156 my $class = ref $self || $self;
157 my $namespace = $self->action_namespace($c);
159 $methods{ $self->can($_) } = $_
160 for @{ Class::Inspector->methods($class) || [] };
162 # Advanced inheritance support for plugins and the like
166 for my $isa ( @{"$class\::ISA"}, $class ) {
167 push @action_cache, @{ $isa->_action_cache }
168 if $isa->can('_action_cache');
172 foreach my $cache (@action_cache) {
173 my $code = $cache->[0];
174 my $method = delete $methods{$code}; # avoid dupe registers
176 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
177 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
178 $c->log->debug( 'Bad action definition "'
179 . join( ' ', @{ $cache->[1] } )
180 . qq/" for "$class->$method"/ )
184 my $reverse = $namespace ? "$namespace/$method" : $method;
185 my $action = $self->create_action(
189 namespace => $namespace,
191 attributes => $attrs,
194 $c->dispatcher->register( $c, $action );
198 =head2 $self->create_action(%args)
200 Called with a hash of data to be use for construction of a new Catalyst::Action
201 (or appropriate sub/alternative class) object.
203 Primarily designed for the use of register_actions.
211 my $class = (exists $args{attributes}{ActionClass}
212 ? $args{attributes}{ActionClass}[0]
213 : $self->_action_class);
215 unless ( Class::Inspector->loaded($class) ) {
216 require Class::Inspector->filename($class);
219 return $class->new( \%args );
223 my ( $self, $c, $name, @attrs ) = @_;
227 foreach my $attr (@attrs) {
229 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
231 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
234 if ( defined $value ) {
235 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
237 push( @{ $raw_attributes{$key} }, $value );
241 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
243 if (exists $hash->{actions} || exists $hash->{action}) {
244 my $a = $hash->{actions} || $hash->{action};
245 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
247 (exists $a->{$name} ? %{$a->{$name}} : ()));
250 my %final_attributes;
252 foreach my $key (keys %raw_attributes) {
254 my $raw = $raw_attributes{$key};
256 foreach my $value (ref($raw) eq 'ARRAY' ? @$raw : $raw) {
258 my $meth = "_parse_${key}_attr";
259 if ( $self->can($meth) ) {
260 ( $key, $value ) = $self->$meth( $c, $name, $value );
262 push( @{ $final_attributes{$key} }, $value );
266 return \%final_attributes;
269 sub _parse_Global_attr {
270 my ( $self, $c, $name, $value ) = @_;
271 return $self->_parse_Path_attr( $c, $name, "/$name" );
274 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
276 sub _parse_Local_attr {
277 my ( $self, $c, $name, $value ) = @_;
278 return $self->_parse_Path_attr( $c, $name, $name );
281 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
283 sub _parse_Path_attr {
284 my ( $self, $c, $name, $value ) = @_;
286 if ( $value =~ m!^/! ) {
287 return ( 'Path', $value );
289 elsif ( length $value ) {
290 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
293 return ( 'Path', $self->path_prefix($c) );
297 sub _parse_Regex_attr {
298 my ( $self, $c, $name, $value ) = @_;
299 return ( 'Regex', $value );
302 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
304 sub _parse_LocalRegex_attr {
305 my ( $self, $c, $name, $value ) = @_;
306 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
307 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
310 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
312 sub _parse_ActionClass_attr {
313 my ( $self, $c, $name, $value ) = @_;
314 unless ( $value =~ s/^\+// ) {
315 $value = join('::', $self->_action_class, $value );
317 return ( 'ActionClass', $value );
322 L<Catalyst>, L<Catalyst::Controller>.
326 Sebastian Riedel, C<sri@cpan.org>
327 Marcus Ramberg, C<mramberg@cpan.org>
328 Matt S Trout, C<mst@shadowcatsystems.co.uk>
332 This program is free software, you can redistribute it and/or modify it under
333 the same terms as Perl itself.