removed UNIVERSAL::require from core
[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;
b8f669f3 30 $begin->dispatch( $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) {
b8f669f3 38 $auto->dispatch( $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 {
b8f669f3 50 $c->action->dispatch( $c );
245ae014 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;
b8f669f3 59 $end->dispatch( $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
649fd1fa 75This is the base class for all Catalyst components. It also handles
76dispatch of actions for controllers.
77
aad72cc9 78=head1 METHODS
79
b5ecfcf0 80=head2 $self->action_namespace($c)
aad72cc9 81
649fd1fa 82Determine the namespace for actions in this component.
83
aad72cc9 84=cut
85
91d4abc5 86sub action_namespace {
87 my ( $self, $c ) = @_;
1cf67b31 88 return $self->config->{namespace} if exists $self->config->{namespace};
89 return Catalyst::Utils::class2prefix( ref($self) || $self,
57e45928 90 $c->config->{case_sensitive} )
91 || '';
91d4abc5 92}
93
27708fc5 94=head2 $self->path_prefix($c)
95
649fd1fa 96alias for action_namespace
97
27708fc5 98=cut
99
100sub path_prefix { shift->action_namespace(@_); }
101
b5ecfcf0 102=head2 $self->register_actions($c)
aad72cc9 103
649fd1fa 104register all actions for this component based on a given context.
105
aad72cc9 106=cut
107
91d4abc5 108sub register_actions {
109 my ( $self, $c ) = @_;
110 my $class = ref $self || $self;
57e45928 111 my $namespace = $self->action_namespace($c);
91d4abc5 112 my %methods;
57e45928 113 $methods{ $self->can($_) } = $_
114 for @{ Class::Inspector->methods($class) || [] };
115
116 # Advanced inheritance support for plugins and the like
117 my @action_cache;
118 {
119 no strict 'refs';
120 for my $isa ( @{"$class\::ISA"}, $class ) {
121 push @action_cache, @{ $isa->_action_cache }
122 if $isa->can('_action_cache');
123 }
124 }
125
126 foreach my $cache (@action_cache) {
127 my $code = $cache->[0];
91d4abc5 128 my $method = $methods{$code};
129 next unless $method;
5b707014 130 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
57e45928 131 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
91d4abc5 132 $c->log->debug( 'Bad action definition "'
57e45928 133 . join( ' ', @{ $cache->[1] } )
91d4abc5 134 . qq/" for "$class->$method"/ )
135 if $c->debug;
136 next;
137 }
138 my $reverse = $namespace ? "$namespace/$method" : $method;
f40acefd 139 my $action = $self->create_action(
140 name => $method,
141 code => $code,
142 reverse => $reverse,
143 namespace => $namespace,
144 class => $class,
145 attributes => $attrs,
91d4abc5 146 );
f40acefd 147
57e45928 148 $c->dispatcher->register( $c, $action );
91d4abc5 149 }
150}
151
f40acefd 152sub create_action {
153 my $self = shift;
734a1e11 154 my %args = @_;
155
156 my $class = (exists $args{attributes}{ActionClass}
157 ? $args{attributes}{ActionClass}[0]
158 : $self->_action_class);
159
16d306fa 160 unless ( Class::Inspector->loaded($class) ) {
1e514a51 161 require Class::Inspector->filename($class);
734a1e11 162 }
163
164 return $class->new( \%args );
f40acefd 165}
166
91d4abc5 167sub _parse_attrs {
5b707014 168 my ( $self, $c, $name, @attrs ) = @_;
91d4abc5 169 my %attributes;
170 foreach my $attr (@attrs) {
171
172 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
173
aad72cc9 174 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
175 {
91d4abc5 176
177 if ( defined $value ) {
178 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
179 }
5b707014 180 my $meth = "_parse_${key}_attr";
181 if ( $self->can($meth) ) {
182 ( $key, $value ) = $self->$meth( $c, $name, $value );
183 }
91d4abc5 184 push( @{ $attributes{$key} }, $value );
185 }
186 }
187 return \%attributes;
188}
189
5b707014 190sub _parse_Global_attr {
191 my ( $self, $c, $name, $value ) = @_;
192 return $self->_parse_Path_attr( $c, $name, "/$name" );
193}
194
27708fc5 195sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
5b707014 196
197sub _parse_Local_attr {
198 my ( $self, $c, $name, $value ) = @_;
199 return $self->_parse_Path_attr( $c, $name, $name );
200}
201
27708fc5 202sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
5b707014 203
204sub _parse_Path_attr {
205 my ( $self, $c, $name, $value ) = @_;
206 $value ||= '';
207 if ( $value =~ m!^/! ) {
208 return ( 'Path', $value );
5b707014 209 }
27708fc5 210 elsif ( length $value ) {
211 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
212 }
213 else {
214 return ( 'Path', $self->path_prefix($c) );
215 }
216}
217
218sub _parse_Regex_attr {
219 my ( $self, $c, $name, $value ) = @_;
220 return ( 'Regex', $value );
5b707014 221}
222
27708fc5 223sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
224
225sub _parse_LocalRegex_attr {
226 my ( $self, $c, $name, $value ) = @_;
227 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
228 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
229}
230
231sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
232
734a1e11 233sub _parse_ActionClass_attr {
234 my ( $self, $c, $name, $value ) = @_;
235 unless ( $value =~ s/^\+// ) {
236 $value = join('::', $self->_action_class, $value );
237 }
238 return ( 'ActionClass', $value );
239}
240
fc7ec1d9 241=head1 SEE ALSO
242
e7f1cf73 243L<Catalyst>, L<Catalyst::Controller>.
fc7ec1d9 244
245=head1 AUTHOR
246
247Sebastian Riedel, C<sri@cpan.org>
61b1e958 248Marcus Ramberg, C<mramberg@cpan.org>
ba599d1c 249Matt S Trout, C<mst@shadowcatsystems.co.uk>
fc7ec1d9 250
251=head1 COPYRIGHT
252
253This program is free software, you can redistribute it and/or modify it under
254the same terms as Perl itself.
255
256=cut
257
2581;