1 package Catalyst::Component;
5 use Class::MOP::Object;
7 use Class::C3::Adopt::NEXT;
11 with 'MooseX::Emulate::Class::Accessor::Fast';
12 with 'Catalyst::ClassData';
17 Catalyst::Component - Catalyst Component Base Class
21 # lib/MyApp/Model/Something.pm
22 package MyApp::Model::Something;
24 use base 'Catalyst::Component';
26 __PACKAGE__->config( foo => 'bar' );
34 my ( $self, $c ) = @_;
35 $c->response->output( $self->{foo} );
40 # Methods can be a request step
41 $c->forward(qw/MyApp::Model::Something forward_to_me/);
44 print $c->comp('MyApp::Model::Something')->test;
46 print $c->comp('MyApp::Model::Something')->{foo};
50 This is the universal base class for Catalyst components
51 (Model/View/Controller).
53 It provides you with a generic new() for instantiation through Catalyst's
54 component loader with config() support and a process() method placeholder.
58 __PACKAGE__->mk_classdata('_plugins');
59 __PACKAGE__->mk_classdata('_config');
64 # Temporary fix, some components does not pass context to constructor
65 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
67 my $args = $self->merge_config_hashes( $self->config, $arguments );
73 my ( $self, $c ) = @_;
75 # Temporary fix, some components does not pass context to constructor
76 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
77 if( my $next = $self->next::can ){
78 my $class = blessed $self || $self;
79 my ($next_package) = Class::MOP::get_code_info($next);
80 warn "There is a COMPONENT method resolving after Catalyst::Component in ${next_package}. This behavior is deprecated and will stop working in future releases.";
81 return $next->($self, $arguments);
83 return $self->new($c, $arguments);
88 my $config = $self->_config || {};
90 my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
92 $self->merge_config_hashes( $config, $newconfig )
95 # this is a bit of a kludge, required to make
96 # __PACKAGE__->config->{foo} = 'bar';
97 # work in a subclass. If we don't have the package symbol in the
98 # current class we know we need to copy up to ours, which calling
99 # the setter will do for us.
100 my $meta = $self->Class::MOP::Object::meta();
101 unless ($meta->has_package_symbol('$_config')) {
103 $config = $self->merge_config_hashes( $config, {} );
104 $self->_config( $config );
110 sub merge_config_hashes {
111 my ( $self, $lefthash, $righthash ) = @_;
113 return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
118 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
119 . " did not override Catalyst::Component::process" );
124 __PACKAGE__->meta->make_immutable;
131 =head2 new($c, $arguments)
133 Called by COMPONENT to instantiate the component; should return an object
134 to be stored in the application's component hash.
136 =head2 COMPONENT($c, $arguments)
138 If this method is present (as it is on all Catalyst::Component subclasses,
139 it is called by Catalyst during setup_components with the application class
140 as $c and any config entry on the application for this component (for example,
141 in the case of MyApp::Controller::Foo this would be
142 MyApp->config->{'Controller::Foo'}). The arguments are expected to be a
143 hashref and are merged with the __PACKAGE__->config hashref before calling
144 ->new to instantiate the component.
148 =head2 $c->config($hashref)
150 =head2 $c->config($key, $value, ...)
152 Accessor for this component's config hash. Config values can be set as
153 key value pair, or you can specify a hashref. In either case the keys
154 will be merged with any existing config settings. Each component in
155 a Catalyst application has it's own config hash.
159 This is the default method called on a Catalyst component in the dispatcher.
160 For instance, Views implement this action to render the response body
161 when you forward to them. The default is an abstract method.
163 =head2 $c->merge_config_hashes( $hashref, $hashref )
165 Merges two hashes together recursively, giving right-hand precedence.
166 Alias for the method in L<Catalyst::Utils>.
168 =head1 OPTIONAL METHODS
170 =head2 ACCEPT_CONTEXT($c, @args)
172 Catalyst components are normally initalized during server startup, either
173 as a Class or a Instance. However, some components require information about
174 the current request. To do so, they can implement an ACCEPT_CONTEXT method.
176 If this method is present, it is called during $c->comp/controller/model/view
177 with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
178 would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
179 ($c, 'bar', 'baz')) and the return value of this method is returned to the
180 calling code in the application rather than the component itself.
184 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
188 Catalyst Contributors, see Catalyst.pm
192 This program is free software, you can redistribute it and/or modify it under
193 the same terms as Perl itself.