Commit | Line | Data |
158c88c0 |
1 | package Catalyst::Component; |
2 | |
a7caa492 |
3 | use Moose; |
6a7254b5 |
4 | use Class::MOP; |
74c89dea |
5 | use Class::MOP::Object; |
e8b9f2a9 |
6 | use Catalyst::Utils; |
cb89a296 |
7 | use Class::C3::Adopt::NEXT; |
6a7254b5 |
8 | use MRO::Compat; |
9 | use mro 'c3'; |
7a5ed4ef |
10 | use Scalar::Util 'blessed'; |
7a5ed4ef |
11 | use namespace::clean -except => 'meta'; |
5595dd2f |
12 | |
a7caa492 |
13 | with 'MooseX::Emulate::Class::Accessor::Fast'; |
14 | with 'Catalyst::ClassData'; |
15 | |
16 | |
158c88c0 |
17 | =head1 NAME |
18 | |
19 | Catalyst::Component - Catalyst Component Base Class |
20 | |
21 | =head1 SYNOPSIS |
22 | |
23 | # lib/MyApp/Model/Something.pm |
24 | package MyApp::Model::Something; |
25 | |
e7f1cf73 |
26 | use base 'Catalyst::Component'; |
158c88c0 |
27 | |
28 | __PACKAGE__->config( foo => 'bar' ); |
29 | |
30 | sub test { |
31 | my $self = shift; |
32 | return $self->{foo}; |
33 | } |
34 | |
35 | sub forward_to_me { |
36 | my ( $self, $c ) = @_; |
37 | $c->response->output( $self->{foo} ); |
38 | } |
43c58153 |
39 | |
158c88c0 |
40 | 1; |
41 | |
42 | # Methods can be a request step |
43 | $c->forward(qw/MyApp::Model::Something forward_to_me/); |
44 | |
45 | # Or just methods |
46 | print $c->comp('MyApp::Model::Something')->test; |
47 | |
48 | print $c->comp('MyApp::Model::Something')->{foo}; |
49 | |
50 | =head1 DESCRIPTION |
51 | |
43c58153 |
52 | This is the universal base class for Catalyst components |
158c88c0 |
53 | (Model/View/Controller). |
54 | |
55 | It provides you with a generic new() for instantiation through Catalyst's |
56 | component loader with config() support and a process() method placeholder. |
57 | |
7cd1a42b |
58 | =cut |
158c88c0 |
59 | |
46d0346d |
60 | __PACKAGE__->mk_classdata('_plugins'); |
11b256bc |
61 | __PACKAGE__->mk_classdata('_config'); |
e8b9f2a9 |
62 | |
d2598ac8 |
63 | has _component_name => ( is => 'ro' ); # Cannot be required => 1 as context |
64 | # class @ISA component - HATE |
65 | # Make accessor callable as a class method, as we need to call setup_actions |
66 | # on the application class, which we don't have an instance of, ewwwww |
e65d000f |
67 | # Also, naughty modules like Catalyst::View::JSON try to write to _everything_, |
68 | # so spit a warning, ignore that (and try to do the right thing anyway) here.. |
d2598ac8 |
69 | around _component_name => sub { |
70 | my ($orig, $self) = (shift, shift); |
e65d000f |
71 | Carp::cluck("Tried to write to the _component_name accessor - is your component broken or just mad? (Write ignored - using default value.)") if scalar @_; |
72 | blessed($self) ? $self->$orig() || blessed($self) : $self; |
d2598ac8 |
73 | }; |
1b79e199 |
74 | |
2ef59958 |
75 | sub BUILDARGS { |
7a5ed4ef |
76 | my $class = shift; |
77 | my $args = {}; |
78 | |
79 | if (@_ == 1) { |
80 | $args = $_[0] if ref($_[0]) eq 'HASH'; |
81 | } elsif (@_ == 2) { # is it ($app, $args) or foo => 'bar' ? |
82 | if (blessed($_[0])) { |
83 | $args = $_[1] if ref($_[1]) eq 'HASH'; |
84 | } elsif (Class::MOP::is_class_loaded($_[0]) && |
85 | $_[0]->isa('Catalyst') && ref($_[1]) eq 'HASH') { |
86 | $args = $_[1]; |
87 | } elsif ($_[0] == $_[1]) { |
88 | $args = $_[1]; |
89 | } else { |
90 | $args = +{ @_ }; |
91 | } |
92 | } elsif (@_ % 2 == 0) { |
93 | $args = +{ @_ }; |
94 | } |
43c58153 |
95 | |
7a5ed4ef |
96 | return $class->merge_config_hashes( $class->config, $args ); |
2ef59958 |
97 | } |
4090e3bb |
98 | |
22247e54 |
99 | sub COMPONENT { |
1b79e199 |
100 | my ( $class, $c ) = @_; |
22247e54 |
101 | |
102 | # Temporary fix, some components does not pass context to constructor |
103 | my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; |
1b79e199 |
104 | if ( my $next = $class->next::can ) { |
6a7254b5 |
105 | my ($next_package) = Class::MOP::get_code_info($next); |
7e2ec16e |
106 | warn "There is a COMPONENT method resolving after Catalyst::Component in ${next_package}.\n"; |
107 | warn "This behavior can no longer be supported, and so your application is probably broken.\n"; |
1cc8db0c |
108 | warn "Your linearized isa hierarchy is: " . join(', ', @{ mro::get_linear_isa($class) }) . "\n"; |
7e2ec16e |
109 | warn "Please see perldoc Catalyst::Upgrading for more information about this issue.\n"; |
6a7254b5 |
110 | } |
1b79e199 |
111 | return $class->new($c, $arguments); |
22247e54 |
112 | } |
113 | |
158c88c0 |
114 | sub config { |
11b256bc |
115 | my $self = shift; |
116 | my $config = $self->_config || {}; |
117 | if (@_) { |
118 | my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} }; |
119 | $self->_config( |
120 | $self->merge_config_hashes( $config, $newconfig ) |
121 | ); |
122 | } else { |
123 | # this is a bit of a kludge, required to make |
124 | # __PACKAGE__->config->{foo} = 'bar'; |
edffeb5a |
125 | # work in a subclass. |
7a5ed4ef |
126 | # TODO maybe this should be a ClassData option? |
e106a59f |
127 | my $class = blessed($self) || $self; |
128 | my $meta = Class::MOP::get_metaclass_by_name($class); |
74c89dea |
129 | unless ($meta->has_package_symbol('$_config')) { |
c03aaf03 |
130 | # Call merge_hashes to ensure we deep copy the parent |
131 | # config onto the subclass |
132 | $self->_config( Catalyst::Utils::merge_hashes($config, {}) ); |
46d0346d |
133 | } |
158c88c0 |
134 | } |
7a5ed4ef |
135 | return $self->_config; |
158c88c0 |
136 | } |
137 | |
7cd1a42b |
138 | sub merge_config_hashes { |
139 | my ( $self, $lefthash, $righthash ) = @_; |
158c88c0 |
140 | |
7cd1a42b |
141 | return Catalyst::Utils::merge_hashes( $lefthash, $righthash ); |
142 | } |
158c88c0 |
143 | |
144 | sub process { |
145 | |
146 | Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] ) |
147 | . " did not override Catalyst::Component::process" ); |
148 | } |
149 | |
46d0346d |
150 | __PACKAGE__->meta->make_immutable; |
7a5ed4ef |
151 | |
7cd1a42b |
152 | 1; |
baf6a3db |
153 | |
7cd1a42b |
154 | __END__ |
baf6a3db |
155 | |
7cd1a42b |
156 | =head1 METHODS |
baf6a3db |
157 | |
7cd1a42b |
158 | =head2 new($c, $arguments) |
baf6a3db |
159 | |
7cd1a42b |
160 | Called by COMPONENT to instantiate the component; should return an object |
161 | to be stored in the application's component hash. |
162 | |
7a5ed4ef |
163 | =head2 COMPONENT |
164 | |
165 | C<< my $component_instance = $component->COMPONENT($app, $arguments); >> |
7cd1a42b |
166 | |
167 | If this method is present (as it is on all Catalyst::Component subclasses, |
168 | it is called by Catalyst during setup_components with the application class |
169 | as $c and any config entry on the application for this component (for example, |
170 | in the case of MyApp::Controller::Foo this would be |
9779c885 |
171 | C<< MyApp->config('Controller::Foo' => \%conf >>). |
172 | The arguments are expected to be a hashref and are merged with the |
173 | C<< __PACKAGE__->config >> hashref before calling C<< ->new >> |
174 | to instantiate the component. |
7cd1a42b |
175 | |
7a5ed4ef |
176 | You can override it in your components to do custom instantiation, using |
177 | something like this: |
178 | |
179 | sub COMPONENT { |
180 | my ($class, $app, $args) = @_; |
181 | $args = $self->merge_config_hashes($self->config, $args); |
182 | return $class->new($app, $args); |
183 | } |
184 | |
7cd1a42b |
185 | =head2 $c->config |
186 | |
187 | =head2 $c->config($hashref) |
188 | |
189 | =head2 $c->config($key, $value, ...) |
190 | |
43c58153 |
191 | Accessor for this component's config hash. Config values can be set as |
7cd1a42b |
192 | key value pair, or you can specify a hashref. In either case the keys |
43c58153 |
193 | will be merged with any existing config settings. Each component in |
194 | a Catalyst application has its own config hash. |
7cd1a42b |
195 | |
196 | =head2 $c->process() |
197 | |
198 | This is the default method called on a Catalyst component in the dispatcher. |
43c58153 |
199 | For instance, Views implement this action to render the response body |
7cd1a42b |
200 | when you forward to them. The default is an abstract method. |
201 | |
202 | =head2 $c->merge_config_hashes( $hashref, $hashref ) |
203 | |
204 | Merges two hashes together recursively, giving right-hand precedence. |
205 | Alias for the method in L<Catalyst::Utils>. |
baf6a3db |
206 | |
825dbf85 |
207 | =head1 OPTIONAL METHODS |
208 | |
209 | =head2 ACCEPT_CONTEXT($c, @args) |
210 | |
f9c35d6c |
211 | Catalyst components are normally initialized during server startup, either |
825dbf85 |
212 | as a Class or a Instance. However, some components require information about |
213 | the current request. To do so, they can implement an ACCEPT_CONTEXT method. |
214 | |
215 | If this method is present, it is called during $c->comp/controller/model/view |
216 | with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/) |
217 | would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with |
218 | ($c, 'bar', 'baz')) and the return value of this method is returned to the |
219 | calling code in the application rather than the component itself. |
220 | |
158c88c0 |
221 | =head1 SEE ALSO |
222 | |
e7f1cf73 |
223 | L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>. |
158c88c0 |
224 | |
2f381252 |
225 | =head1 AUTHORS |
158c88c0 |
226 | |
2f381252 |
227 | Catalyst Contributors, see Catalyst.pm |
158c88c0 |
228 | |
229 | =head1 COPYRIGHT |
230 | |
536bee89 |
231 | This library is free software. You can redistribute it and/or modify it under |
158c88c0 |
232 | the same terms as Perl itself. |
233 | |
85d9fce6 |
234 | =cut |