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