Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Base; |
2 | |
3 | use strict; |
ba599d1c |
4 | use base qw/Catalyst::AttrContainer Class::Accessor::Fast/; |
b7783788 |
5 | |
a2f2cde9 |
6 | use Catalyst::Exception; |
fc7ec1d9 |
7 | use 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 |
13 | sub _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 |
23 | sub _BEGIN : Private { |
1143712c |
24 | my ( $self, $c ) = @_; |
0bbb5a1f |
25 | my $begin = @{ $c->get_action( 'begin', $c->namespace, 1 ) }[-1]; |
1143712c |
26 | return 1 unless $begin; |
27 | $begin->[0]->execute($c); |
0bbb5a1f |
28 | return !@{ $c->error }; |
1143712c |
29 | } |
ba599d1c |
30 | |
0bbb5a1f |
31 | sub _AUTO : Private { |
1143712c |
32 | my ( $self, $c ) = @_; |
0bbb5a1f |
33 | my @auto = @{ $c->get_action( 'auto', $c->namespace, 1 ) }; |
1143712c |
34 | foreach my $auto (@auto) { |
35 | $auto->[0]->execute($c); |
36 | return 0 unless $c->state; |
ba599d1c |
37 | } |
1143712c |
38 | return 1; |
39 | } |
ba599d1c |
40 | |
0bbb5a1f |
41 | sub _ACTION : Private { |
1143712c |
42 | my ( $self, $c ) = @_; |
43 | $c->action->execute($c); |
0bbb5a1f |
44 | return !@{ $c->error }; |
1143712c |
45 | } |
ba599d1c |
46 | |
0bbb5a1f |
47 | sub _END : Private { |
1143712c |
48 | my ( $self, $c ) = @_; |
0bbb5a1f |
49 | my $end = @{ $c->get_action( 'end', $c->namespace, 1 ) }[-1]; |
1143712c |
50 | return 1 unless $end; |
51 | $end->[0]->execute($c); |
0bbb5a1f |
52 | return !@{ $c->error }; |
ba599d1c |
53 | } |
d70195d8 |
54 | |
fc7ec1d9 |
55 | =head1 NAME |
56 | |
57 | Catalyst::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 | |
90 | This is the universal base class for Catalyst components |
91 | (Model/View/Controller). |
92 | |
93 | It provides you with a generic new() for instantiation through Catalyst's |
94 | component 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 | |
104 | sub 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 | |
124 | sub 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 |
140 | sub 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 | |
150 | L<Catalyst>. |
151 | |
152 | =head1 AUTHOR |
153 | |
154 | Sebastian Riedel, C<sri@cpan.org> |
61b1e958 |
155 | Marcus Ramberg, C<mramberg@cpan.org> |
ba599d1c |
156 | Matt S Trout, C<mst@shadowcatsystems.co.uk> |
fc7ec1d9 |
157 | |
158 | =head1 COPYRIGHT |
159 | |
160 | This program is free software, you can redistribute it and/or modify it under |
161 | the same terms as Perl itself. |
162 | |
163 | =cut |
164 | |
165 | 1; |