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