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 $self->action_for('name')
90 Returns the Catalyst::Action object (if any) for a given method name in
96 my ( $self, $name ) = @_;
97 my $app = ($self->isa('Catalyst') ? $self : $self->{application});
98 return $app->dispatcher->get_action($name, $self->action_namespace);
101 =head2 $self->action_namespace($c)
103 Returns the private namespace for actions in this component. Defaults to a value
104 from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes
105 "foo/bar") or can be overriden from the "namespace" config key.
109 sub action_namespace {
110 my ( $self, $c ) = @_;
112 $c = ($self->isa('Catalyst') ? $self : $self->{application});
114 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
115 return $hash->{namespace} if exists $hash->{namespace};
116 return Catalyst::Utils::class2prefix( ref($self) || $self,
117 $c->config->{case_sensitive} )
121 =head2 $self->path_prefix($c)
123 Returns the default path prefix for :Local, :LocalRegex and relative :Path
124 actions in this component. Defaults to the action_namespace or can be
125 overriden from the "path" config key.
130 my ( $self, $c ) = @_;
132 $c = ($self->isa('Catalyst') ? $self : $self->{application});
134 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
135 return $hash->{path} if exists $hash->{path};
136 return shift->action_namespace(@_);
139 =head2 $self->register_actions($c)
141 register all actions for this component based on a given context.
145 sub register_actions {
146 my ( $self, $c ) = @_;
147 my $class = ref $self || $self;
148 my $namespace = $self->action_namespace($c);
150 $methods{ $self->can($_) } = $_
151 for @{ Class::Inspector->methods($class) || [] };
153 # Advanced inheritance support for plugins and the like
157 for my $isa ( @{"$class\::ISA"}, $class ) {
158 push @action_cache, @{ $isa->_action_cache }
159 if $isa->can('_action_cache');
163 foreach my $cache (@action_cache) {
164 my $code = $cache->[0];
165 my $method = delete $methods{$code}; # avoid dupe registers
167 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
168 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
169 $c->log->debug( 'Bad action definition "'
170 . join( ' ', @{ $cache->[1] } )
171 . qq/" for "$class->$method"/ )
175 my $reverse = $namespace ? "$namespace/$method" : $method;
176 my $action = $self->create_action(
180 namespace => $namespace,
182 attributes => $attrs,
185 $c->dispatcher->register( $c, $action );
193 my $class = (exists $args{attributes}{ActionClass}
194 ? $args{attributes}{ActionClass}[0]
195 : $self->_action_class);
197 unless ( Class::Inspector->loaded($class) ) {
198 require Class::Inspector->filename($class);
201 return $class->new( \%args );
205 my ( $self, $c, $name, @attrs ) = @_;
209 foreach my $attr (@attrs) {
211 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
213 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
216 if ( defined $value ) {
217 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
219 push( @{ $raw_attributes{$key} }, $value );
223 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
225 if (exists $hash->{actions} || exists $hash->{action}) {
226 my $a = $hash->{actions} || $hash->{action};
227 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
229 (exists $a->{$name} ? %{$a->{$name}} : ()));
232 my %final_attributes;
234 foreach my $key (keys %raw_attributes) {
236 my $raw = $raw_attributes{$key};
238 foreach my $value (ref($raw) ? @$raw : $raw) {
240 my $meth = "_parse_${key}_attr";
241 if ( $self->can($meth) ) {
242 ( $key, $value ) = $self->$meth( $c, $name, $value );
244 push( @{ $final_attributes{$key} }, $value );
248 return \%final_attributes;
251 sub _parse_Global_attr {
252 my ( $self, $c, $name, $value ) = @_;
253 return $self->_parse_Path_attr( $c, $name, "/$name" );
256 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
258 sub _parse_Local_attr {
259 my ( $self, $c, $name, $value ) = @_;
260 return $self->_parse_Path_attr( $c, $name, $name );
263 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
265 sub _parse_Path_attr {
266 my ( $self, $c, $name, $value ) = @_;
268 if ( $value =~ m!^/! ) {
269 return ( 'Path', $value );
271 elsif ( length $value ) {
272 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
275 return ( 'Path', $self->path_prefix($c) );
279 sub _parse_Regex_attr {
280 my ( $self, $c, $name, $value ) = @_;
281 return ( 'Regex', $value );
284 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
286 sub _parse_LocalRegex_attr {
287 my ( $self, $c, $name, $value ) = @_;
288 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
289 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
292 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
294 sub _parse_ActionClass_attr {
295 my ( $self, $c, $name, $value ) = @_;
296 unless ( $value =~ s/^\+// ) {
297 $value = join('::', $self->_action_class, $value );
299 return ( 'ActionClass', $value );
304 L<Catalyst>, L<Catalyst::Controller>.
308 Sebastian Riedel, C<sri@cpan.org>
309 Marcus Ramberg, C<mramberg@cpan.org>
310 Matt S Trout, C<mst@shadowcatsystems.co.uk>
314 This program is free software, you can redistribute it and/or modify it under
315 the same terms as Perl itself.