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 ) = @_; |
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 |
52 | sub _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 | |
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 | |
91d4abc5 |
80 | sub 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 |
91 | sub 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 | |
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 | |
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 |
159 | L<Catalyst>, L<Catalyst::Controller>. |
fc7ec1d9 |
160 | |
161 | =head1 AUTHOR |
162 | |
163 | Sebastian Riedel, C<sri@cpan.org> |
61b1e958 |
164 | Marcus Ramberg, C<mramberg@cpan.org> |
ba599d1c |
165 | Matt S Trout, C<mst@shadowcatsystems.co.uk> |
fc7ec1d9 |
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; |