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