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