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 Returns the private namespace for actions in this component. Defaults to a value
83 from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes
84 "foo/bar") or can be overriden from the "namespace" config key.
88 sub action_namespace {
89 my ( $self, $c ) = @_;
90 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
91 return $hash->{namespace} if exists $hash->{namespace};
92 return Catalyst::Utils::class2prefix( ref($self) || $self,
93 $c->config->{case_sensitive} )
97 =head2 $self->path_prefix($c)
99 Returns the default path prefix for :Local, :LocalRegex and relative :Path
100 actions in this component. Defaults to the action_namespace or can be
101 overriden from the "path" config key.
106 my ( $self, $c ) = @_;
107 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
108 return $hash->{path} if exists $hash->{path};
109 return shift->action_namespace(@_);
112 =head2 $self->register_actions($c)
114 register all actions for this component based on a given context.
118 sub register_actions {
119 my ( $self, $c ) = @_;
120 my $class = ref $self || $self;
121 my $namespace = $self->action_namespace($c);
123 $methods{ $self->can($_) } = $_
124 for @{ Class::Inspector->methods($class) || [] };
126 # Advanced inheritance support for plugins and the like
130 for my $isa ( @{"$class\::ISA"}, $class ) {
131 push @action_cache, @{ $isa->_action_cache }
132 if $isa->can('_action_cache');
136 foreach my $cache (@action_cache) {
137 my $code = $cache->[0];
138 my $method = $methods{$code};
140 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
141 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
142 $c->log->debug( 'Bad action definition "'
143 . join( ' ', @{ $cache->[1] } )
144 . qq/" for "$class->$method"/ )
148 my $reverse = $namespace ? "$namespace/$method" : $method;
149 my $action = $self->create_action(
153 namespace => $namespace,
155 attributes => $attrs,
158 $c->dispatcher->register( $c, $action );
166 my $class = (exists $args{attributes}{ActionClass}
167 ? $args{attributes}{ActionClass}[0]
168 : $self->_action_class);
170 unless ( Class::Inspector->loaded($class) ) {
171 require Class::Inspector->filename($class);
174 return $class->new( \%args );
178 my ( $self, $c, $name, @attrs ) = @_;
182 foreach my $attr (@attrs) {
184 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
186 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
189 if ( defined $value ) {
190 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
192 push( @{ $raw_attributes{$key} }, $value );
196 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
198 if (exists $hash->{actions} || exists $hash->{action}) {
199 my $a = $hash->{actions} || $hash->{action};
200 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
202 (exists $a->{$name} ? %{$a->{$name}} : ()));
205 my %final_attributes;
207 foreach my $key (keys %raw_attributes) {
209 my $raw = $raw_attributes{$key};
211 foreach my $value (ref($raw) ? @$raw : $raw) {
213 my $meth = "_parse_${key}_attr";
214 if ( $self->can($meth) ) {
215 ( $key, $value ) = $self->$meth( $c, $name, $value );
217 push( @{ $final_attributes{$key} }, $value );
221 return \%final_attributes;
224 sub _parse_Global_attr {
225 my ( $self, $c, $name, $value ) = @_;
226 return $self->_parse_Path_attr( $c, $name, "/$name" );
229 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
231 sub _parse_Local_attr {
232 my ( $self, $c, $name, $value ) = @_;
233 return $self->_parse_Path_attr( $c, $name, $name );
236 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
238 sub _parse_Path_attr {
239 my ( $self, $c, $name, $value ) = @_;
241 if ( $value =~ m!^/! ) {
242 return ( 'Path', $value );
244 elsif ( length $value ) {
245 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
248 return ( 'Path', $self->path_prefix($c) );
252 sub _parse_Regex_attr {
253 my ( $self, $c, $name, $value ) = @_;
254 return ( 'Regex', $value );
257 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
259 sub _parse_LocalRegex_attr {
260 my ( $self, $c, $name, $value ) = @_;
261 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
262 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
265 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
267 sub _parse_ActionClass_attr {
268 my ( $self, $c, $name, $value ) = @_;
269 unless ( $value =~ s/^\+// ) {
270 $value = join('::', $self->_action_class, $value );
272 return ( 'ActionClass', $value );
277 L<Catalyst>, L<Catalyst::Controller>.
281 Sebastian Riedel, C<sri@cpan.org>
282 Marcus Ramberg, C<mramberg@cpan.org>
283 Matt S Trout, C<mst@shadowcatsystems.co.uk>
287 This program is free software, you can redistribute it and/or modify it under
288 the same terms as Perl itself.