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 Determine the namespace for actions in this component.
86 sub action_namespace {
87 my ( $self, $c ) = @_;
88 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
89 return $hash->{namespace} if exists $hash->{namespace};
90 return Catalyst::Utils::class2prefix( ref($self) || $self,
91 $c->config->{case_sensitive} )
95 =head2 $self->path_prefix($c)
97 alias for action_namespace
101 sub path_prefix { shift->action_namespace(@_); }
103 =head2 $self->register_actions($c)
105 register all actions for this component based on a given context.
109 sub register_actions {
110 my ( $self, $c ) = @_;
111 my $class = ref $self || $self;
112 my $namespace = $self->action_namespace($c);
114 $methods{ $self->can($_) } = $_
115 for @{ Class::Inspector->methods($class) || [] };
117 # Advanced inheritance support for plugins and the like
121 for my $isa ( @{"$class\::ISA"}, $class ) {
122 push @action_cache, @{ $isa->_action_cache }
123 if $isa->can('_action_cache');
127 foreach my $cache (@action_cache) {
128 my $code = $cache->[0];
129 my $method = $methods{$code};
131 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
132 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
133 $c->log->debug( 'Bad action definition "'
134 . join( ' ', @{ $cache->[1] } )
135 . qq/" for "$class->$method"/ )
139 my $reverse = $namespace ? "$namespace/$method" : $method;
140 my $action = $self->create_action(
144 namespace => $namespace,
146 attributes => $attrs,
149 $c->dispatcher->register( $c, $action );
157 my $class = (exists $args{attributes}{ActionClass}
158 ? $args{attributes}{ActionClass}[0]
159 : $self->_action_class);
161 unless ( Class::Inspector->loaded($class) ) {
162 require Class::Inspector->filename($class);
165 return $class->new( \%args );
169 my ( $self, $c, $name, @attrs ) = @_;
173 foreach my $attr (@attrs) {
175 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
177 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
180 if ( defined $value ) {
181 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
183 push( @{ $raw_attributes{$key} }, $value );
187 my $hash = (ref $self ? $self : $self->config); # hate app-is-class
189 if (exists $hash->{actions} || exists $hash->{action}) {
190 my $a = $hash->{actions} || $hash->{action};
191 %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
193 (exists $a->{$name} ? %{$a->{$name}} : ()));
196 my %final_attributes;
198 foreach my $key (keys %raw_attributes) {
200 my $raw = $raw_attributes{$key};
202 foreach my $value (ref($raw) ? @$raw : $raw) {
204 my $meth = "_parse_${key}_attr";
205 if ( $self->can($meth) ) {
206 ( $key, $value ) = $self->$meth( $c, $name, $value );
208 push( @{ $final_attributes{$key} }, $value );
212 return \%final_attributes;
215 sub _parse_Global_attr {
216 my ( $self, $c, $name, $value ) = @_;
217 return $self->_parse_Path_attr( $c, $name, "/$name" );
220 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
222 sub _parse_Local_attr {
223 my ( $self, $c, $name, $value ) = @_;
224 return $self->_parse_Path_attr( $c, $name, $name );
227 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
229 sub _parse_Path_attr {
230 my ( $self, $c, $name, $value ) = @_;
232 if ( $value =~ m!^/! ) {
233 return ( 'Path', $value );
235 elsif ( length $value ) {
236 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
239 return ( 'Path', $self->path_prefix($c) );
243 sub _parse_Regex_attr {
244 my ( $self, $c, $name, $value ) = @_;
245 return ( 'Regex', $value );
248 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
250 sub _parse_LocalRegex_attr {
251 my ( $self, $c, $name, $value ) = @_;
252 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
253 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
256 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
258 sub _parse_ActionClass_attr {
259 my ( $self, $c, $name, $value ) = @_;
260 unless ( $value =~ s/^\+// ) {
261 $value = join('::', $self->_action_class, $value );
263 return ( 'ActionClass', $value );
268 L<Catalyst>, L<Catalyst::Controller>.
272 Sebastian Riedel, C<sri@cpan.org>
273 Marcus Ramberg, C<mramberg@cpan.org>
274 Matt S Trout, C<mst@shadowcatsystems.co.uk>
278 This program is free software, you can redistribute it and/or modify it under
279 the same terms as Perl itself.