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