Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Base; |
2 | |
3 | use strict; |
158c88c0 |
4 | use base qw/Catalyst::Component Catalyst::AttrContainer Class::Accessor::Fast/; |
b7783788 |
5 | |
a2f2cde9 |
6 | use Catalyst::Exception; |
91d4abc5 |
7 | use Catalyst::Utils; |
8 | use Class::Inspector; |
fc7ec1d9 |
9 | use 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 |
16 | sub _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 |
26 | sub _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 |
34 | sub _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 |
44 | sub _ACTION : Private { |
1143712c |
45 | my ( $self, $c ) = @_; |
8b76bfcf |
46 | if ( ref $c->action |
47 | && $c->action->isa('Catalyst::Action') |
48 | && $c->req->action ) |
49 | { |
245ae014 |
50 | $c->action->execute($c); |
51 | } |
0bbb5a1f |
52 | return !@{ $c->error }; |
1143712c |
53 | } |
ba599d1c |
54 | |
0bbb5a1f |
55 | sub _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 | |
65 | Catalyst::Base - Catalyst Base Class |
66 | |
67 | =head1 SYNOPSIS |
68 | |
69 | See L<Catalyst> |
70 | |
71 | =head1 DESCRIPTION |
72 | |
73 | Catalyst Base Class |
74 | |
75 | =head1 METHODS |
76 | |
77 | =over 4 |
78 | |
79 | =item $self->action_namespace($c) |
80 | |
81 | =cut |
82 | |
91d4abc5 |
83 | sub action_namespace { |
84 | my ( $self, $c ) = @_; |
57e45928 |
85 | return Catalyst::Utils::class2prefix( ref $self, |
86 | $c->config->{case_sensitive} ) |
87 | || ''; |
91d4abc5 |
88 | } |
89 | |
aad72cc9 |
90 | =item $self->register_actions($c) |
91 | |
92 | =cut |
93 | |
91d4abc5 |
94 | sub register_actions { |
95 | my ( $self, $c ) = @_; |
96 | my $class = ref $self || $self; |
57e45928 |
97 | my $namespace = $self->action_namespace($c); |
91d4abc5 |
98 | my %methods; |
57e45928 |
99 | $methods{ $self->can($_) } = $_ |
100 | for @{ Class::Inspector->methods($class) || [] }; |
101 | |
102 | # Advanced inheritance support for plugins and the like |
103 | my @action_cache; |
104 | { |
105 | no strict 'refs'; |
106 | for my $isa ( @{"$class\::ISA"}, $class ) { |
107 | push @action_cache, @{ $isa->_action_cache } |
108 | if $isa->can('_action_cache'); |
109 | } |
110 | } |
111 | |
112 | foreach my $cache (@action_cache) { |
113 | my $code = $cache->[0]; |
91d4abc5 |
114 | my $method = $methods{$code}; |
115 | next unless $method; |
57e45928 |
116 | my $attrs = $self->_parse_attrs( @{ $cache->[1] } ); |
117 | if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) { |
91d4abc5 |
118 | $c->log->debug( 'Bad action definition "' |
57e45928 |
119 | . join( ' ', @{ $cache->[1] } ) |
91d4abc5 |
120 | . qq/" for "$class->$method"/ ) |
121 | if $c->debug; |
122 | next; |
123 | } |
124 | my $reverse = $namespace ? "$namespace/$method" : $method; |
97d6d2bd |
125 | my $action = $self->_action_class->new( |
91d4abc5 |
126 | { |
127 | name => $method, |
128 | code => $code, |
129 | reverse => $reverse, |
130 | namespace => $namespace, |
131 | class => $class, |
132 | attributes => $attrs, |
133 | } |
134 | ); |
57e45928 |
135 | $c->dispatcher->register( $c, $action ); |
91d4abc5 |
136 | } |
137 | } |
138 | |
139 | sub _parse_attrs { |
140 | my ( $self, @attrs ) = @_; |
141 | my %attributes; |
142 | foreach my $attr (@attrs) { |
143 | |
144 | # Parse out :Foo(bar) into Foo => bar etc (and arrayify) |
145 | |
aad72cc9 |
146 | if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) ) |
147 | { |
91d4abc5 |
148 | |
149 | if ( defined $value ) { |
150 | ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ ); |
151 | } |
152 | push( @{ $attributes{$key} }, $value ); |
153 | } |
154 | } |
155 | return \%attributes; |
156 | } |
157 | |
bea4160a |
158 | =back |
159 | |
fc7ec1d9 |
160 | =head1 SEE ALSO |
161 | |
e7f1cf73 |
162 | L<Catalyst>, L<Catalyst::Controller>. |
fc7ec1d9 |
163 | |
164 | =head1 AUTHOR |
165 | |
166 | Sebastian Riedel, C<sri@cpan.org> |
61b1e958 |
167 | Marcus Ramberg, C<mramberg@cpan.org> |
ba599d1c |
168 | Matt S Trout, C<mst@shadowcatsystems.co.uk> |
fc7ec1d9 |
169 | |
170 | =head1 COPYRIGHT |
171 | |
172 | This program is free software, you can redistribute it and/or modify it under |
173 | the same terms as Perl itself. |
174 | |
175 | =cut |
176 | |
177 | 1; |