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 | |
11 | __PACKAGE__->_dispatch_steps([ qw/_BEGIN _AUTO _ACTION/ ]); |
ac733264 |
12 | |
ba599d1c |
13 | sub _DISPATCH :Private { |
14 | my ( $self, $c ) = @_; |
1143712c |
15 | |
16 | foreach my $disp (@{$self->_dispatch_steps}) { |
17 | last unless $c->forward($disp); |
ba599d1c |
18 | } |
19 | |
1143712c |
20 | $c->forward('_END'); |
21 | } |
fc7ec1d9 |
22 | |
1143712c |
23 | sub _BEGIN :Private { |
24 | my ( $self, $c ) = @_; |
25 | my $begin = @{ $c->get_action('begin', $c->namespace, 1) }[-1]; |
26 | return 1 unless $begin; |
27 | $begin->[0]->execute($c); |
28 | return !@{$c->error}; |
29 | } |
ba599d1c |
30 | |
1143712c |
31 | sub _AUTO :Private { |
32 | my ( $self, $c ) = @_; |
33 | my @auto = @{ $c->get_action('auto', $c->namespace, 1) }; |
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 | |
1143712c |
41 | sub _ACTION :Private { |
42 | my ( $self, $c ) = @_; |
43 | $c->action->execute($c); |
44 | return !@{$c->error}; |
45 | } |
ba599d1c |
46 | |
1143712c |
47 | sub _END :Private { |
48 | my ( $self, $c ) = @_; |
49 | my $end = @{ $c->get_action('end', $c->namespace, 1) }[-1]; |
50 | return 1 unless $end; |
51 | $end->[0]->execute($c); |
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 ) = @_; |
b7783788 |
106 | |
a268a011 |
107 | # Temporary fix, some components does not pass context to constructor |
108 | my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; |
109 | |
b7783788 |
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 | |
b7783788 |
142 | Catalyst::Exception->throw( |
143 | message => ( ref $_[0] || $_[0] ) . " did not override Catalyst::Base::process" |
144 | ); |
d70195d8 |
145 | } |
fc7ec1d9 |
146 | |
4aff785c |
147 | =item FETCH_CODE_ATTRIBUTES |
148 | |
149 | =item MODIFY_CODE_ATTRIBUTES |
150 | |
bea4160a |
151 | =back |
152 | |
fc7ec1d9 |
153 | =head1 SEE ALSO |
154 | |
155 | L<Catalyst>. |
156 | |
157 | =head1 AUTHOR |
158 | |
159 | Sebastian Riedel, C<sri@cpan.org> |
61b1e958 |
160 | Marcus Ramberg, C<mramberg@cpan.org> |
ba599d1c |
161 | Matt S Trout, C<mst@shadowcatsystems.co.uk> |
fc7ec1d9 |
162 | |
163 | =head1 COPYRIGHT |
164 | |
165 | This program is free software, you can redistribute it and/or modify it under |
166 | the same terms as Perl itself. |
167 | |
168 | =cut |
169 | |
170 | 1; |