Added config->{namespace}
[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 ) = @_;
8b76bfcf 46 if ( ref $c->action
31375184 47 && $c->action->can('execute')
8b76bfcf 48 && $c->req->action )
49 {
245ae014 50 $c->action->execute($c);
51 }
0bbb5a1f 52 return !@{ $c->error };
1143712c 53}
ba599d1c 54
0bbb5a1f 55sub _END : Private {
1143712c 56 my ( $self, $c ) = @_;
57e45928 57 my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
1143712c 58 return 1 unless $end;
a9dc674c 59 $end->execute($c);
0bbb5a1f 60 return !@{ $c->error };
ba599d1c 61}
d70195d8 62
aad72cc9 63=head1 NAME
64
65Catalyst::Base - Catalyst Base Class
66
67=head1 SYNOPSIS
68
69See L<Catalyst>
70
71=head1 DESCRIPTION
72
73Catalyst Base Class
74
75=head1 METHODS
76
b5ecfcf0 77=head2 $self->action_namespace($c)
aad72cc9 78
79=cut
80
91d4abc5 81sub action_namespace {
82 my ( $self, $c ) = @_;
1cf67b31 83 return $self->config->{namespace} if exists $self->config->{namespace};
84 return Catalyst::Utils::class2prefix( ref($self) || $self,
57e45928 85 $c->config->{case_sensitive} )
86 || '';
91d4abc5 87}
88
b5ecfcf0 89=head2 $self->register_actions($c)
aad72cc9 90
91=cut
92
91d4abc5 93sub register_actions {
94 my ( $self, $c ) = @_;
95 my $class = ref $self || $self;
57e45928 96 my $namespace = $self->action_namespace($c);
91d4abc5 97 my %methods;
57e45928 98 $methods{ $self->can($_) } = $_
99 for @{ Class::Inspector->methods($class) || [] };
100
101 # Advanced inheritance support for plugins and the like
102 my @action_cache;
103 {
104 no strict 'refs';
105 for my $isa ( @{"$class\::ISA"}, $class ) {
106 push @action_cache, @{ $isa->_action_cache }
107 if $isa->can('_action_cache');
108 }
109 }
110
111 foreach my $cache (@action_cache) {
112 my $code = $cache->[0];
91d4abc5 113 my $method = $methods{$code};
114 next unless $method;
57e45928 115 my $attrs = $self->_parse_attrs( @{ $cache->[1] } );
116 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
91d4abc5 117 $c->log->debug( 'Bad action definition "'
57e45928 118 . join( ' ', @{ $cache->[1] } )
91d4abc5 119 . qq/" for "$class->$method"/ )
120 if $c->debug;
121 next;
122 }
123 my $reverse = $namespace ? "$namespace/$method" : $method;
97d6d2bd 124 my $action = $self->_action_class->new(
91d4abc5 125 {
126 name => $method,
127 code => $code,
128 reverse => $reverse,
129 namespace => $namespace,
130 class => $class,
131 attributes => $attrs,
132 }
133 );
57e45928 134 $c->dispatcher->register( $c, $action );
91d4abc5 135 }
136}
137
138sub _parse_attrs {
139 my ( $self, @attrs ) = @_;
140 my %attributes;
141 foreach my $attr (@attrs) {
142
143 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
144
aad72cc9 145 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
146 {
91d4abc5 147
148 if ( defined $value ) {
149 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
150 }
151 push( @{ $attributes{$key} }, $value );
152 }
153 }
154 return \%attributes;
155}
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;