Added COMPONENT() and ACCEPT_CONTEXT() support
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
1 package Catalyst::Base;
2
3 use strict;
4 use base qw/Catalyst::Component Catalyst::AttrContainer Class::Accessor::Fast/;
5
6 use Catalyst::Exception;
7 use Catalyst::Utils;
8 use Class::Inspector;
9 use NEXT;
10
11 __PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class/;
12
13 __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
14 __PACKAGE__->_action_class('Catalyst::Action');
15
16 sub _DISPATCH : Private {
17     my ( $self, $c ) = @_;
18
19     foreach my $disp ( @{ $self->_dispatch_steps } ) {
20         last unless $c->forward($disp);
21     }
22
23     $c->forward('_END');
24 }
25
26 sub _BEGIN : Private {
27     my ( $self, $c ) = @_;
28     my $begin = ( $c->get_actions( 'begin', $c->namespace ) )[-1];
29     return 1 unless $begin;
30     $begin->execute($c);
31     return !@{ $c->error };
32 }
33
34 sub _AUTO : Private {
35     my ( $self, $c ) = @_;
36     my @auto = $c->get_actions( 'auto', $c->namespace );
37     foreach my $auto (@auto) {
38         $auto->execute($c);
39         return 0 unless $c->state;
40     }
41     return 1;
42 }
43
44 sub _ACTION : Private {
45     my ( $self, $c ) = @_;
46     if (   ref $c->action
47         && $c->action->isa('Catalyst::Action')
48         && $c->req->action )
49     {
50         $c->action->execute($c);
51     }
52     return !@{ $c->error };
53 }
54
55 sub _END : Private {
56     my ( $self, $c ) = @_;
57     my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
58     return 1 unless $end;
59     $end->execute($c);
60     return !@{ $c->error };
61 }
62
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 =head2 $self->action_namespace($c)
78
79 =cut
80
81 sub action_namespace {
82     my ( $self, $c ) = @_;
83     return Catalyst::Utils::class2prefix( ref $self,
84         $c->config->{case_sensitive} )
85       || '';
86 }
87
88 =head2 $self->register_actions($c)
89
90 =cut
91
92 sub register_actions {
93     my ( $self, $c ) = @_;
94     my $class = ref $self || $self;
95     my $namespace = $self->action_namespace($c);
96     my %methods;
97     $methods{ $self->can($_) } = $_
98       for @{ Class::Inspector->methods($class) || [] };
99
100     # Advanced inheritance support for plugins and the like
101     my @action_cache;
102     {
103         no strict 'refs';
104         for my $isa ( @{"$class\::ISA"}, $class ) {
105             push @action_cache, @{ $isa->_action_cache }
106               if $isa->can('_action_cache');
107         }
108     }
109
110     foreach my $cache (@action_cache) {
111         my $code   = $cache->[0];
112         my $method = $methods{$code};
113         next unless $method;
114         my $attrs = $self->_parse_attrs( @{ $cache->[1] } );
115         if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
116             $c->log->debug( 'Bad action definition "'
117                   . join( ' ', @{ $cache->[1] } )
118                   . qq/" for "$class->$method"/ )
119               if $c->debug;
120             next;
121         }
122         my $reverse = $namespace ? "$namespace/$method" : $method;
123         my $action = $self->_action_class->new(
124             {
125                 name       => $method,
126                 code       => $code,
127                 reverse    => $reverse,
128                 namespace  => $namespace,
129                 class      => $class,
130                 attributes => $attrs,
131             }
132         );
133         $c->dispatcher->register( $c, $action );
134     }
135 }
136
137 sub _parse_attrs {
138     my ( $self, @attrs ) = @_;
139     my %attributes;
140     foreach my $attr (@attrs) {
141
142         # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
143
144         if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
145         {
146
147             if ( defined $value ) {
148                 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
149             }
150             push( @{ $attributes{$key} }, $value );
151         }
152     }
153     return \%attributes;
154 }
155
156 =head1 SEE ALSO
157
158 L<Catalyst>, L<Catalyst::Controller>.
159
160 =head1 AUTHOR
161
162 Sebastian Riedel, C<sri@cpan.org>
163 Marcus Ramberg, C<mramberg@cpan.org>
164 Matt S Trout, C<mst@shadowcatsystems.co.uk>
165
166 =head1 COPYRIGHT
167
168 This program is free software, you can redistribute it and/or modify it under
169 the same terms as Perl itself.
170
171 =cut
172
173 1;