1 package Catalyst::Component;
4 use MooseX::Adopt::Class::Accessor::Fast;
7 with 'MooseX::Emulate::Class::Accessor::Fast';
8 with 'Catalyst::ClassData';
13 Catalyst::Component - Catalyst Component Base Class
17 # lib/MyApp/Model/Something.pm
18 package MyApp::Model::Something;
20 use base 'Catalyst::Component';
22 __PACKAGE__->config( foo => 'bar' );
30 my ( $self, $c ) = @_;
31 $c->response->output( $self->{foo} );
36 # Methods can be a request step
37 $c->forward(qw/MyApp::Model::Something forward_to_me/);
40 print $c->comp('MyApp::Model::Something')->test;
42 print $c->comp('MyApp::Model::Something')->{foo};
46 This is the universal base class for Catalyst components
47 (Model/View/Controller).
49 It provides you with a generic new() for instantiation through Catalyst's
50 component loader with config() support and a process() method placeholder.
54 __PACKAGE__->mk_classdata($_) for qw/_config _plugins/;
57 my ( $orig, $self) = @_;
59 # Temporary fix, some components does not pass context to constructor
60 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
62 my $args = $self->merge_config_hashes( $self->config, $arguments );
63 $self->$orig( $args );
69 my ( $self, $c ) = @_;
71 # Temporary fix, some components does not pass context to constructor
72 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
73 return $self->new($c, $arguments);
78 my $config_sub = $self->can('_config');
79 my $config = $self->$config_sub() || {};
81 my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
83 $self->merge_config_hashes( $config, $newconfig )
86 # this is a bit of a kludge, required to make
87 # __PACKAGE__->config->{foo} = 'bar';
88 # work in a subclass. Calling the Class::Data::Inheritable setter
89 # will create a new _config method in the current class if it's
90 # currently inherited from the superclass. So, the can() call will
91 # return a different subref in that case and that means we know to
92 # copy and reset the value stored in the class data.
94 $self->_config( $config );
96 if ((my $config_sub_now = $self->can('_config')) ne $config_sub) {
98 $config = $self->merge_config_hashes( $config, {} );
99 $self->$config_sub_now( $config );
105 sub merge_config_hashes {
106 my ( $self, $lefthash, $righthash ) = @_;
108 return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
113 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
114 . " did not override Catalyst::Component::process" );
123 =head2 new($c, $arguments)
125 Called by COMPONENT to instantiate the component; should return an object
126 to be stored in the application's component hash.
128 =head2 COMPONENT($c, $arguments)
130 If this method is present (as it is on all Catalyst::Component subclasses,
131 it is called by Catalyst during setup_components with the application class
132 as $c and any config entry on the application for this component (for example,
133 in the case of MyApp::Controller::Foo this would be
134 MyApp->config->{'Controller::Foo'}). The arguments are expected to be a
135 hashref and are merged with the __PACKAGE__->config hashref before calling
136 ->new to instantiate the component.
140 =head2 $c->config($hashref)
142 =head2 $c->config($key, $value, ...)
144 Accessor for this component's config hash. Config values can be set as
145 key value pair, or you can specify a hashref. In either case the keys
146 will be merged with any existing config settings. Each component in
147 a Catalyst application has it's own config hash.
151 This is the default method called on a Catalyst component in the dispatcher.
152 For instance, Views implement this action to render the response body
153 when you forward to them. The default is an abstract method.
155 =head2 $c->merge_config_hashes( $hashref, $hashref )
157 Merges two hashes together recursively, giving right-hand precedence.
158 Alias for the method in L<Catalyst::Utils>.
160 =head1 OPTIONAL METHODS
162 =head2 ACCEPT_CONTEXT($c, @args)
164 Catalyst components are normally initalized during server startup, either
165 as a Class or a Instance. However, some components require information about
166 the current request. To do so, they can implement an ACCEPT_CONTEXT method.
168 If this method is present, it is called during $c->comp/controller/model/view
169 with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
170 would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
171 ($c, 'bar', 'baz')) and the return value of this method is returned to the
172 calling code in the application rather than the component itself.
176 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
180 Sebastian Riedel, C<sri@cpan.org>
181 Marcus Ramberg, C<mramberg@cpan.org>
182 Matt S Trout, C<mst@shadowcatsystems.co.uk>
186 This program is free software, you can redistribute it and/or modify it under
187 the same terms as Perl itself.