Fixed dispatcher, so $c->req->action(undef) works again
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
1 package Catalyst::Base;
2
3 use strict;
4 use base qw/Catalyst::Component Catalyst::AttrContainer Class::Accessor::Fast/;
5
6 use Catalyst::Exception;
7 use Catalyst::Utils;
8 use Class::Inspector;
9 use NEXT;
10
11 __PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class/;
12
13 __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
14 __PACKAGE__->_action_class('Catalyst::Action');
15
16 sub _DISPATCH : Private {
17     my ( $self, $c ) = @_;
18
19     foreach my $disp ( @{ $self->_dispatch_steps } ) {
20         last unless $c->forward($disp);
21     }
22
23     $c->forward('_END');
24 }
25
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->execute($c);
31     return !@{ $c->error };
32 }
33
34 sub _AUTO : Private {
35     my ( $self, $c ) = @_;
36     my @auto = $c->get_actions( 'auto', $c->namespace );
37     foreach my $auto (@auto) {
38         $auto->execute($c);
39         return 0 unless $c->state;
40     }
41     return 1;
42 }
43
44 sub _ACTION : Private {
45     my ( $self, $c ) = @_;
46     if ( ref $c->action && $c->action->isa('Catalyst::Action') ) {
47         $c->action->execute($c);
48     }
49     return !@{ $c->error };
50 }
51
52 sub _END : Private {
53     my ( $self, $c ) = @_;
54     my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
55     return 1 unless $end;
56     $end->execute($c);
57     return !@{ $c->error };
58 }
59
60 =head1 NAME
61
62 Catalyst::Base - Catalyst Base Class
63
64 =head1 SYNOPSIS
65
66 See L<Catalyst>
67
68 =head1 DESCRIPTION
69
70 Catalyst Base Class
71
72 =head1 METHODS
73
74 =over 4
75
76 =item $self->action_namespace($c)
77
78 =cut
79
80 sub action_namespace {
81     my ( $self, $c ) = @_;
82     return Catalyst::Utils::class2prefix( ref $self,
83         $c->config->{case_sensitive} )
84       || '';
85 }
86
87 =item $self->register_actions($c)
88
89 =cut
90
91 sub register_actions {
92     my ( $self, $c ) = @_;
93     my $class = ref $self || $self;
94     my $namespace = $self->action_namespace($c);
95     my %methods;
96     $methods{ $self->can($_) } = $_
97       for @{ Class::Inspector->methods($class) || [] };
98
99     # Advanced inheritance support for plugins and the like
100     my @action_cache;
101     {
102         no strict 'refs';
103         for my $isa ( @{"$class\::ISA"}, $class ) {
104             push @action_cache, @{ $isa->_action_cache }
105               if $isa->can('_action_cache');
106         }
107     }
108
109     foreach my $cache (@action_cache) {
110         my $code   = $cache->[0];
111         my $method = $methods{$code};
112         next unless $method;
113         my $attrs = $self->_parse_attrs( @{ $cache->[1] } );
114         if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
115             $c->log->debug( 'Bad action definition "'
116                   . join( ' ', @{ $cache->[1] } )
117                   . qq/" for "$class->$method"/ )
118               if $c->debug;
119             next;
120         }
121         my $reverse = $namespace ? "$namespace/$method" : $method;
122         my $action = $self->_action_class->new(
123             {
124                 name       => $method,
125                 code       => $code,
126                 reverse    => $reverse,
127                 namespace  => $namespace,
128                 class      => $class,
129                 attributes => $attrs,
130             }
131         );
132         $c->dispatcher->register( $c, $action );
133     }
134 }
135
136 sub _parse_attrs {
137     my ( $self, @attrs ) = @_;
138     my %attributes;
139     foreach my $attr (@attrs) {
140
141         # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
142
143         if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
144         {
145
146             if ( defined $value ) {
147                 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
148             }
149             push( @{ $attributes{$key} }, $value );
150         }
151     }
152     return \%attributes;
153 }
154
155 =back
156
157 =head1 SEE ALSO
158
159 L<Catalyst>, L<Catalyst::Controller>.
160
161 =head1 AUTHOR
162
163 Sebastian Riedel, C<sri@cpan.org>
164 Marcus Ramberg, C<mramberg@cpan.org>
165 Matt S Trout, C<mst@shadowcatsystems.co.uk>
166
167 =head1 COPYRIGHT
168
169 This program is free software, you can redistribute it and/or modify it under
170 the same terms as Perl itself.
171
172 =cut
173
174 1;