1 package Catalyst::Component;
4 use MooseX::ClassAttribute;
14 class_has _plugins => ( is => 'rw' );
19 Catalyst::Component - Catalyst Component Base Class
23 # lib/MyApp/Model/Something.pm
24 package MyApp::Model::Something;
26 use base 'Catalyst::Component';
28 __PACKAGE__->config( foo => 'bar' );
36 my ( $self, $c ) = @_;
37 $c->response->output( $self->{foo} );
42 # Methods can be a request step
43 $c->forward(qw/MyApp::Model::Something forward_to_me/);
46 print $c->comp('MyApp::Model::Something')->test;
48 print $c->comp('MyApp::Model::Something')->{foo};
52 This is the universal base class for Catalyst components
53 (Model/View/Controller).
55 It provides you with a generic new() for instantiation through Catalyst's
56 component loader with config() support and a process() method placeholder.
60 #to do: are we switching to moose-style key => value constructors from
61 # catalyst-style {key => value} constructors ?
65 my ( $self, $c ) = @_;
67 # Temporary fix, some components does not pass context to constructor
68 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
69 my $merged = $self->merge_config_hashes( $self->config, $arguments );
70 $orig->( $self, $merged );
74 my ( $self, $c ) = @_;
76 # Temporary fix, some components does not pass context to constructor
77 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
79 #Moose TODO: I don't think I fully grok NEXT. is this here for MI or something?
80 # how can we have a next here? this -is- the base class....
81 if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) {
85 if ( my $new = $self->new( $c, $arguments ) ) {
89 my $class = ref $self || $self;
90 my $new = $self->merge_config_hashes(
91 $self->config, $arguments );
92 return bless $new, $class;
97 #Moose TODO: I have no fucking clue what's going on here (groditi)
100 my $config_sub = $self->can('_config');
101 my $config = $self->$config_sub();
102 #my $config = $self->_config;
104 my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
106 $self->merge_config_hashes( $config, $newconfig )
109 # this is a bit of a kludge, required to make
110 # __PACKAGE__->config->{foo} = 'bar';
111 # work in a subclass. Calling the Class::Data::Inheritable setter
112 # will create a new _config method in the current class if it's
113 # currently inherited from the superclass. So, the can() call will
114 # return a different subref in that case and that means we know to
115 # copy and reset the value stored in the class data.
117 $self->_config( $config );
119 if ((my $config_sub_now = $self->can('_config')) ne $config_sub) {
121 #this is retarded. if we want a new ref we could do: { %$config }
122 $config = $self->merge_config_hashes( $config, {} );
123 $self->$config_sub_now( $config );
129 sub merge_config_hashes {
130 my ( $self, $lefthash, $righthash ) = @_;
132 return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
137 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
138 . " did not override Catalyst::Component::process" );
147 =head2 new($c, $arguments)
149 Called by COMPONENT to instantiate the component; should return an object
150 to be stored in the application's component hash.
152 =head2 COMPONENT($c, $arguments)
154 If this method is present (as it is on all Catalyst::Component subclasses,
155 it is called by Catalyst during setup_components with the application class
156 as $c and any config entry on the application for this component (for example,
157 in the case of MyApp::Controller::Foo this would be
158 MyApp->config->{'Controller::Foo'}). The arguments are expected to be a
159 hashref and are merged with the __PACKAGE__->config hashref before calling
160 ->new to instantiate the component.
164 =head2 $c->config($hashref)
166 =head2 $c->config($key, $value, ...)
168 Accessor for this component's config hash. Config values can be set as
169 key value pair, or you can specify a hashref. In either case the keys
170 will be merged with any existing config settings. Each component in
171 a Catalyst application has it's own config hash.
175 This is the default method called on a Catalyst component in the dispatcher.
176 For instance, Views implement this action to render the response body
177 when you forward to them. The default is an abstract method.
179 =head2 $c->merge_config_hashes( $hashref, $hashref )
181 Merges two hashes together recursively, giving right-hand precedence.
182 Alias for the method in L<Catalyst::Utils>.
184 =head1 OPTIONAL METHODS
186 =head2 ACCEPT_CONTEXT($c, @args)
188 Catalyst components are normally initalized during server startup, either
189 as a Class or a Instance. However, some components require information about
190 the current request. To do so, they can implement an ACCEPT_CONTEXT method.
192 If this method is present, it is called during $c->comp/controller/model/view
193 with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
194 would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
195 ($c, 'bar', 'baz')) and the return value of this method is returned to the
196 calling code in the application rather than the component itself.
200 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
204 Sebastian Riedel, C<sri@cpan.org>
205 Marcus Ramberg, C<mramberg@cpan.org>
206 Matt S Trout, C<mst@shadowcatsystems.co.uk>
210 This program is free software, you can redistribute it and/or modify it under
211 the same terms as Perl itself.