1 package Catalyst::Component;
5 use Class::MOP::Object;
7 use Class::C3::Adopt::NEXT;
8 use Devel::InnerPackage ();
11 use Scalar::Util 'blessed';
12 use namespace::clean -except => 'meta';
14 with 'MooseX::Emulate::Class::Accessor::Fast';
15 with 'Catalyst::ClassData';
20 Catalyst::Component - Catalyst Component Base Class
24 # lib/MyApp/Model/Something.pm
25 package MyApp::Model::Something;
27 use base 'Catalyst::Component';
29 __PACKAGE__->config( foo => 'bar' );
37 my ( $self, $c ) = @_;
38 $c->response->output( $self->{foo} );
43 # Methods can be a request step
44 $c->forward(qw/MyApp::Model::Something forward_to_me/);
47 print $c->comp('MyApp::Model::Something')->test;
49 print $c->comp('MyApp::Model::Something')->{foo};
53 This is the universal base class for Catalyst components
54 (Model/View/Controller).
56 It provides you with a generic new() for instantiation through Catalyst's
57 component loader with config() support and a process() method placeholder.
61 __PACKAGE__->mk_classdata('_plugins');
62 __PACKAGE__->mk_classdata('_config');
64 has catalyst_component_name => ( is => 'ro' ); # Cannot be required => 1 as context
65 # class @ISA component - HATE
66 # Make accessor callable as a class method, as we need to call setup_actions
67 # on the application class, which we don't have an instance of, ewwwww
68 # Also, naughty modules like Catalyst::View::JSON try to write to _everything_,
69 # so spit a warning, ignore that (and try to do the right thing anyway) here..
70 around catalyst_component_name => sub {
71 my ($orig, $self) = (shift, shift);
72 Carp::cluck("Tried to write to the catalyst_component_name accessor - is your component broken or just mad? (Write ignored - using default value.)") if scalar @_;
73 blessed($self) ? $self->$orig() || blessed($self) : $self;
81 $args = $_[0] if ref($_[0]) eq 'HASH';
82 } elsif (@_ == 2) { # is it ($app, $args) or foo => 'bar' ?
84 $args = $_[1] if ref($_[1]) eq 'HASH';
85 } elsif (Class::MOP::is_class_loaded($_[0]) &&
86 $_[0]->isa('Catalyst') && ref($_[1]) eq 'HASH') {
91 } elsif (@_ % 2 == 0) {
95 return $class->merge_config_hashes( $class->config, $args );
99 my ( $class, $c ) = @_;
101 # Temporary fix, some components does not pass context to constructor
102 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
103 if ( my $next = $class->next::can ) {
104 my ($next_package) = Class::MOP::get_code_info($next);
105 warn "There is a COMPONENT method resolving after Catalyst::Component in ${next_package}.\n";
106 warn "This behavior can no longer be supported, and so your application is probably broken.\n";
107 warn "Your linearized isa hierarchy is: " . join(', ', @{ mro::get_linear_isa($class) }) . "\n";
108 warn "Please see perldoc Catalyst::Upgrading for more information about this issue.\n";
110 return $class->new($c, $arguments);
115 # Uncomment once sane to do so
116 #Carp::cluck("config method called on instance") if ref $self;
117 my $config = $self->_config || {};
119 my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
121 $self->merge_config_hashes( $config, $newconfig )
124 # this is a bit of a kludge, required to make
125 # __PACKAGE__->config->{foo} = 'bar';
126 # work in a subclass.
127 # TODO maybe this should be a ClassData option?
128 my $class = blessed($self) || $self;
129 my $meta = Class::MOP::get_metaclass_by_name($class);
130 unless ($meta->has_package_symbol('$_config')) {
131 # Call merge_hashes to ensure we deep copy the parent
132 # config onto the subclass
133 $self->_config( Catalyst::Utils::merge_hashes($config, {}) );
136 return $self->_config;
139 sub merge_config_hashes {
140 my ( $self, $lefthash, $righthash ) = @_;
142 return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
147 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
148 . " did not override Catalyst::Component::process" );
152 my ($class, $component) = @_;
153 return Devel::InnerPackage::list_packages( $component );
156 __PACKAGE__->meta->make_immutable;
164 =head2 new($app, $arguments)
166 Called by COMPONENT to instantiate the component; should return an object
167 to be stored in the application's component hash.
171 C<< my $component_instance = $component->COMPONENT($app, $arguments); >>
173 If this method is present (as it is on all Catalyst::Component subclasses),
174 it is called by Catalyst during setup_components with the application class
175 as $app and any config entry on the application for this component (for example,
176 in the case of MyApp::Controller::Foo this would be
177 C<< MyApp->config('Controller::Foo' => \%conf >>).
179 The arguments are expected to be a hashref and are merged with the
180 C<< __PACKAGE__->config >> hashref before calling C<< ->new >>
181 to instantiate the component.
183 You can override it in your components to do custom instantiation, using
187 my ($class, $app, $args) = @_;
188 $args = $class->merge_config_hashes($class->config, $args);
189 return $class->new($app, $args);
194 =head2 $c->config($hashref)
196 =head2 $c->config($key, $value, ...)
198 Accessor for this component's config hash. Config values can be set as
199 key value pair, or you can specify a hashref. In either case the keys
200 will be merged with any existing config settings. Each component in
201 a Catalyst application has its own config hash.
203 The component's config hash is merged with any config entry on the
204 application for this component and passed to C<new()> (as mentioned
205 above at L</COMPONENT>). The common practice to access the merged
206 config is to use a Moose attribute for each config entry on the
211 This is the default method called on a Catalyst component in the dispatcher.
212 For instance, Views implement this action to render the response body
213 when you forward to them. The default is an abstract method.
215 =head2 $c->merge_config_hashes( $hashref, $hashref )
217 Merges two hashes together recursively, giving right-hand precedence.
218 Alias for the method in L<Catalyst::Utils>.
220 =head2 $c->expand_modules( $setup_component_config )
222 Return a list of extra components that this component has created. By default,
223 it just looks for a list of inner packages of this component
227 =head1 OPTIONAL METHODS
229 =head2 ACCEPT_CONTEXT($c, @args)
231 Catalyst components are normally initialized during server startup, either
232 as a Class or a Instance. However, some components require information about
233 the current request. To do so, they can implement an ACCEPT_CONTEXT method.
235 If this method is present, it is called during $c->comp/controller/model/view
236 with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
237 would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
238 ($c, 'bar', 'baz')) and the return value of this method is returned to the
239 calling code in the application rather than the component itself.
243 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
247 Catalyst Contributors, see Catalyst.pm
251 This library is free software. You can redistribute it and/or modify it under
252 the same terms as Perl itself.