- Refactored get_action into get_action and get_actions
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
CommitLineData
fc7ec1d9 1package Catalyst::Base;
2
3use strict;
ba599d1c 4use base qw/Catalyst::AttrContainer Class::Accessor::Fast/;
b7783788 5
a2f2cde9 6use Catalyst::Exception;
fc7ec1d9 7use NEXT;
8
1143712c 9__PACKAGE__->mk_classdata($_) for qw/_config _dispatch_steps/;
10
0bbb5a1f 11__PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
ac733264 12
0bbb5a1f 13sub _DISPATCH : Private {
ba599d1c 14 my ( $self, $c ) = @_;
1143712c 15
0bbb5a1f 16 foreach my $disp ( @{ $self->_dispatch_steps } ) {
1143712c 17 last unless $c->forward($disp);
ba599d1c 18 }
19
1143712c 20 $c->forward('_END');
21}
fc7ec1d9 22
0bbb5a1f 23sub _BEGIN : Private {
1143712c 24 my ( $self, $c ) = @_;
a9dc674c 25 my $begin = ($c->get_actions( 'begin', $c->namespace))[-1];
1143712c 26 return 1 unless $begin;
a9dc674c 27 $begin->execute($c);
0bbb5a1f 28 return !@{ $c->error };
1143712c 29}
ba599d1c 30
0bbb5a1f 31sub _AUTO : Private {
1143712c 32 my ( $self, $c ) = @_;
a9dc674c 33 my @auto = $c->get_actions('auto', $c->namespace);
1143712c 34 foreach my $auto (@auto) {
a9dc674c 35 $auto->execute($c);
1143712c 36 return 0 unless $c->state;
ba599d1c 37 }
1143712c 38 return 1;
39}
ba599d1c 40
0bbb5a1f 41sub _ACTION : Private {
1143712c 42 my ( $self, $c ) = @_;
43 $c->action->execute($c);
0bbb5a1f 44 return !@{ $c->error };
1143712c 45}
ba599d1c 46
0bbb5a1f 47sub _END : Private {
1143712c 48 my ( $self, $c ) = @_;
a9dc674c 49 my $end = ($c->get_actions( 'end', $c->namespace))[-1];
1143712c 50 return 1 unless $end;
a9dc674c 51 $end->execute($c);
0bbb5a1f 52 return !@{ $c->error };
ba599d1c 53}
d70195d8 54
fc7ec1d9 55=head1 NAME
56
57Catalyst::Base - Catalyst Universal Base Class
58
59=head1 SYNOPSIS
60
61 # lib/MyApp/Model/Something.pm
62 package MyApp::Model::Something;
63
64 use base 'Catalyst::Base';
65
66 __PACKAGE__->config( foo => 'bar' );
67
68 sub test {
69 my $self = shift;
70 return $self->{foo};
71 }
72
73 sub forward_to_me {
74 my ( $self, $c ) = @_;
75 $c->response->output( $self->{foo} );
76 }
77
78 1;
79
80 # Methods can be a request step
81 $c->forward(qw/MyApp::Model::Something forward_to_me/);
fc7ec1d9 82
83 # Or just methods
84 print $c->comp('MyApp::Model::Something')->test;
85
86 print $c->comp('MyApp::Model::Something')->{foo};
87
88=head1 DESCRIPTION
89
90This is the universal base class for Catalyst components
91(Model/View/Controller).
92
93It provides you with a generic new() for instantiation through Catalyst's
94component loader with config() support and a process() method placeholder.
95
23f9d934 96=head1 METHODS
97
98=over 4
99
100=item new($c)
fc7ec1d9 101
102=cut
103
104sub new {
105 my ( $self, $c ) = @_;
0bbb5a1f 106
a268a011 107 # Temporary fix, some components does not pass context to constructor
108 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
109
0bbb5a1f 110 return $self->NEXT::new( { %{ $self->config }, %{$arguments} } );
fc7ec1d9 111}
112
23f9d934 113# remember to leave blank lines between the consecutive =item's
114# otherwise the pod tools don't recognize the subsequent =items
115
116=item $c->config
117
118=item $c->config($hashref)
119
120=item $c->config($key, $value, ...)
fc7ec1d9 121
122=cut
123
124sub config {
125 my $self = shift;
126 $self->_config( {} ) unless $self->_config;
812a28c9 127 if (@_) {
c19e2f4a 128 my $config = @_ > 1 ? {@_} : $_[0];
fc7ec1d9 129 while ( my ( $key, $val ) = each %$config ) {
130 $self->_config->{$key} = $val;
131 }
132 }
133 return $self->_config;
134}
135
23f9d934 136=item $c->process()
fc7ec1d9 137
138=cut
139
d70195d8 140sub process {
a2f2cde9 141
0bbb5a1f 142 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
143 . " did not override Catalyst::Base::process" );
d70195d8 144}
fc7ec1d9 145
bea4160a 146=back
147
fc7ec1d9 148=head1 SEE ALSO
149
150L<Catalyst>.
151
152=head1 AUTHOR
153
154Sebastian Riedel, C<sri@cpan.org>
61b1e958 155Marcus Ramberg, C<mramberg@cpan.org>
ba599d1c 156Matt S Trout, C<mst@shadowcatsystems.co.uk>
fc7ec1d9 157
158=head1 COPYRIGHT
159
160This program is free software, you can redistribute it and/or modify it under
161the same terms as Perl itself.
162
163=cut
164
1651;