Commit | Line | Data |
158c88c0 |
1 | package Catalyst::Component; |
2 | |
3 | use strict; |
684d10ed |
4 | use base qw/Class::Accessor::Fast Class::Data::Inheritable/; |
158c88c0 |
5 | use NEXT; |
6 | |
d0d4d785 |
7 | __PACKAGE__->mk_classdata($_) for qw/_config _plugins/; |
158c88c0 |
8 | |
9 | =head1 NAME |
10 | |
11 | Catalyst::Component - Catalyst Component Base Class |
12 | |
13 | =head1 SYNOPSIS |
14 | |
15 | # lib/MyApp/Model/Something.pm |
16 | package MyApp::Model::Something; |
17 | |
e7f1cf73 |
18 | use base 'Catalyst::Component'; |
158c88c0 |
19 | |
20 | __PACKAGE__->config( foo => 'bar' ); |
21 | |
22 | sub test { |
23 | my $self = shift; |
24 | return $self->{foo}; |
25 | } |
26 | |
27 | sub forward_to_me { |
28 | my ( $self, $c ) = @_; |
29 | $c->response->output( $self->{foo} ); |
30 | } |
31 | |
32 | 1; |
33 | |
34 | # Methods can be a request step |
35 | $c->forward(qw/MyApp::Model::Something forward_to_me/); |
36 | |
37 | # Or just methods |
38 | print $c->comp('MyApp::Model::Something')->test; |
39 | |
40 | print $c->comp('MyApp::Model::Something')->{foo}; |
41 | |
42 | =head1 DESCRIPTION |
43 | |
44 | This is the universal base class for Catalyst components |
45 | (Model/View/Controller). |
46 | |
47 | It provides you with a generic new() for instantiation through Catalyst's |
48 | component loader with config() support and a process() method placeholder. |
49 | |
50 | =head1 METHODS |
51 | |
825dbf85 |
52 | =head2 new($c, $arguments) |
53 | |
54 | Called by COMPONENT to instantiate the component; should return an object |
55 | to be stored in the application's component hash. |
158c88c0 |
56 | |
57 | =cut |
58 | |
59 | sub new { |
60 | my ( $self, $c ) = @_; |
61 | |
62 | # Temporary fix, some components does not pass context to constructor |
63 | my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; |
64 | |
baf6a3db |
65 | return $self->NEXT::new( $self->merge_config_hashes( $self->config, $arguments ) ); |
158c88c0 |
66 | } |
67 | |
825dbf85 |
68 | =head2 COMPONENT($c, $arguments) |
69 | |
70 | If this method is present (as it is on all Catalyst::Component subclasses, |
71 | it is called by Catalyst during setup_components with the application class |
72 | as $c and any config entry on the application for this component (for example, |
73 | in the case of MyApp::Controller::Foo this would be |
74 | MyApp->config->{'Controller::Foo'}). The arguments are expected to be a hashref |
75 | and are merged with the __PACKAGE__->config hashref before calling ->new to |
76 | instantiate the component. |
22247e54 |
77 | |
78 | =cut |
79 | |
80 | sub COMPONENT { |
81 | my ( $self, $c ) = @_; |
82 | |
83 | # Temporary fix, some components does not pass context to constructor |
84 | my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; |
85 | |
86 | if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) { |
87 | return $new; |
88 | } |
89 | else { |
90 | if ( my $new = $self->new( $c, $arguments ) ) { |
91 | return $new; |
92 | } |
93 | else { |
94 | my $class = ref $self || $self; |
baf6a3db |
95 | my $new = $self->merge_config_hashes( $self->config, $arguments ); |
22247e54 |
96 | return bless $new, $class; |
97 | } |
98 | } |
99 | } |
100 | |
b5ecfcf0 |
101 | # remember to leave blank lines between the consecutive =head2's |
102 | # otherwise the pod tools don't recognize the subsequent =head2s |
158c88c0 |
103 | |
b5ecfcf0 |
104 | =head2 $c->config |
158c88c0 |
105 | |
b5ecfcf0 |
106 | =head2 $c->config($hashref) |
158c88c0 |
107 | |
b5ecfcf0 |
108 | =head2 $c->config($key, $value, ...) |
158c88c0 |
109 | |
110 | =cut |
111 | |
112 | sub config { |
113 | my $self = shift; |
5e707396 |
114 | my $config = $self->_config; |
115 | unless ($config) { |
c7a54b4e |
116 | $self->_config( $config = {} ); |
5e707396 |
117 | } |
158c88c0 |
118 | if (@_) { |
baf6a3db |
119 | my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} }; |
120 | $self->_config( |
121 | $self->merge_config_hashes( $config, $newconfig ) |
122 | ); |
158c88c0 |
123 | } |
5e707396 |
124 | return $config; |
158c88c0 |
125 | } |
126 | |
b5ecfcf0 |
127 | =head2 $c->process() |
158c88c0 |
128 | |
129 | =cut |
130 | |
131 | sub process { |
132 | |
133 | Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] ) |
134 | . " did not override Catalyst::Component::process" ); |
135 | } |
136 | |
baf6a3db |
137 | =head2 $c->merge_hash_config( $hashref, $hashref ) |
138 | |
139 | Merges two hashes together recursively, giving right-hand precedence. |
140 | |
141 | =cut |
142 | |
143 | sub merge_config_hashes { |
144 | my ( $self, $lefthash, $righthash ) = @_; |
145 | |
146 | my %merged = %$lefthash; |
147 | for my $key ( keys %$righthash ) {\r |
148 | my $right_ref = ( ref $righthash->{ $key } || '' ) eq 'HASH';\r |
149 | my $left_ref = ( ( exists $lefthash->{ $key } && ref $lefthash->{ $key } ) || '' ) eq 'HASH';\r |
150 | if( $right_ref and $left_ref ) {\r |
151 | $merged{ $key } = $self->merge_config_hashes( |
152 | $lefthash->{ $key }, $righthash->{ $key } |
153 | );\r |
154 | } |
155 | else { |
156 | $merged{ $key } = $righthash->{ $key }; |
157 | }\r |
158 | } |
159 | |
160 | return \%merged; |
161 | } |
162 | |
825dbf85 |
163 | =head1 OPTIONAL METHODS |
164 | |
165 | =head2 ACCEPT_CONTEXT($c, @args) |
166 | |
167 | Catalyst components are normally initalized during server startup, either |
168 | as a Class or a Instance. However, some components require information about |
169 | the current request. To do so, they can implement an ACCEPT_CONTEXT method. |
170 | |
171 | If this method is present, it is called during $c->comp/controller/model/view |
172 | with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/) |
173 | would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with |
174 | ($c, 'bar', 'baz')) and the return value of this method is returned to the |
175 | calling code in the application rather than the component itself. |
176 | |
158c88c0 |
177 | =head1 SEE ALSO |
178 | |
e7f1cf73 |
179 | L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>. |
158c88c0 |
180 | |
181 | =head1 AUTHOR |
182 | |
183 | Sebastian Riedel, C<sri@cpan.org> |
184 | Marcus Ramberg, C<mramberg@cpan.org> |
185 | Matt S Trout, C<mst@shadowcatsystems.co.uk> |
186 | |
187 | =head1 COPYRIGHT |
188 | |
189 | This program is free software, you can redistribute it and/or modify it under |
190 | the same terms as Perl itself. |
191 | |
192 | =cut |
193 | |
194 | 1; |