Pod fix for Engine::FastCGI.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Component.pm
CommitLineData
158c88c0 1package Catalyst::Component;
2
a7caa492 3use Moose;
6a7254b5 4use Class::MOP;
a7caa492 5use MooseX::Adopt::Class::Accessor::Fast;
e8b9f2a9 6use Catalyst::Utils;
6a7254b5 7use MRO::Compat;
8use mro 'c3';
5595dd2f 9
a7caa492 10with 'MooseX::Emulate::Class::Accessor::Fast';
11with 'Catalyst::ClassData';
12
13
158c88c0 14=head1 NAME
15
16Catalyst::Component - Catalyst Component Base Class
17
18=head1 SYNOPSIS
19
20 # lib/MyApp/Model/Something.pm
21 package MyApp::Model::Something;
22
e7f1cf73 23 use base 'Catalyst::Component';
158c88c0 24
25 __PACKAGE__->config( foo => 'bar' );
26
27 sub test {
28 my $self = shift;
29 return $self->{foo};
30 }
31
32 sub forward_to_me {
33 my ( $self, $c ) = @_;
34 $c->response->output( $self->{foo} );
35 }
ac5c933b 36
158c88c0 37 1;
38
39 # Methods can be a request step
40 $c->forward(qw/MyApp::Model::Something forward_to_me/);
41
42 # Or just methods
43 print $c->comp('MyApp::Model::Something')->test;
44
45 print $c->comp('MyApp::Model::Something')->{foo};
46
47=head1 DESCRIPTION
48
ac5c933b 49This is the universal base class for Catalyst components
158c88c0 50(Model/View/Controller).
51
52It provides you with a generic new() for instantiation through Catalyst's
53component loader with config() support and a process() method placeholder.
54
7cd1a42b 55=cut
158c88c0 56
46d0346d 57__PACKAGE__->mk_classdata('_plugins');
11b256bc 58__PACKAGE__->mk_classdata('_config');
e8b9f2a9 59
2ef59958 60sub BUILDARGS {
61 my ($self) = @_;
62
158c88c0 63 # Temporary fix, some components does not pass context to constructor
64 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
e8b9f2a9 65
a7caa492 66 my $args = $self->merge_config_hashes( $self->config, $arguments );
2ef59958 67
68 return $args;
69}
4090e3bb 70
22247e54 71sub COMPONENT {
72 my ( $self, $c ) = @_;
73
74 # Temporary fix, some components does not pass context to constructor
75 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
6a7254b5 76 if( my $next = $self->next::can ){
77 my $class = blessed $self || $self;
78 my ($next_package) = Class::MOP::get_code_info($next);
79 warn "There is a COMPONENT method resolving after Catalyst::Component in ${next_package}. This behavior is deprecated and will stop working in future releases.";
80 return $next->($self, $arguments);
81 }
4090e3bb 82 return $self->new($c, $arguments);
22247e54 83}
84
158c88c0 85sub config {
11b256bc 86 my $self = shift;
87 my $config = $self->_config || {};
88 if (@_) {
89 my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
90 $self->_config(
91 $self->merge_config_hashes( $config, $newconfig )
92 );
93 } else {
94 # this is a bit of a kludge, required to make
95 # __PACKAGE__->config->{foo} = 'bar';
96 # work in a subclass. If we don't have the package symbol in the
97 # current class we know we need to copy up to ours, which calling
98 # the setter will do for us.
99
100 unless ($self->meta->has_package_symbol('$_config')) {
101
102 $config = $self->merge_config_hashes( $config, {} );
103 $self->_config( $config );
46d0346d 104 }
158c88c0 105 }
11b256bc 106 return $config;
158c88c0 107}
108
7cd1a42b 109sub merge_config_hashes {
110 my ( $self, $lefthash, $righthash ) = @_;
158c88c0 111
7cd1a42b 112 return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
113}
158c88c0 114
115sub process {
116
117 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
118 . " did not override Catalyst::Component::process" );
119}
120
213cf5bb 121no Moose;
46d0346d 122
123__PACKAGE__->meta->make_immutable;
7cd1a42b 1241;
baf6a3db 125
7cd1a42b 126__END__
baf6a3db 127
7cd1a42b 128=head1 METHODS
baf6a3db 129
7cd1a42b 130=head2 new($c, $arguments)
baf6a3db 131
7cd1a42b 132Called by COMPONENT to instantiate the component; should return an object
133to be stored in the application's component hash.
134
135=head2 COMPONENT($c, $arguments)
136
137If this method is present (as it is on all Catalyst::Component subclasses,
138it is called by Catalyst during setup_components with the application class
139as $c and any config entry on the application for this component (for example,
140in the case of MyApp::Controller::Foo this would be
ac5c933b 141MyApp->config->{'Controller::Foo'}). The arguments are expected to be a
142hashref and are merged with the __PACKAGE__->config hashref before calling
7cd1a42b 143->new to instantiate the component.
144
145=head2 $c->config
146
147=head2 $c->config($hashref)
148
149=head2 $c->config($key, $value, ...)
150
ac5c933b 151Accessor for this component's config hash. Config values can be set as
7cd1a42b 152key value pair, or you can specify a hashref. In either case the keys
ac5c933b 153will be merged with any existing config settings. Each component in
7cd1a42b 154a Catalyst application has it's own config hash.
155
156=head2 $c->process()
157
158This is the default method called on a Catalyst component in the dispatcher.
ac5c933b 159For instance, Views implement this action to render the response body
7cd1a42b 160when you forward to them. The default is an abstract method.
161
162=head2 $c->merge_config_hashes( $hashref, $hashref )
163
164Merges two hashes together recursively, giving right-hand precedence.
165Alias for the method in L<Catalyst::Utils>.
baf6a3db 166
825dbf85 167=head1 OPTIONAL METHODS
168
169=head2 ACCEPT_CONTEXT($c, @args)
170
171Catalyst components are normally initalized during server startup, either
172as a Class or a Instance. However, some components require information about
173the current request. To do so, they can implement an ACCEPT_CONTEXT method.
174
175If this method is present, it is called during $c->comp/controller/model/view
176with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
177would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
178($c, 'bar', 'baz')) and the return value of this method is returned to the
179calling code in the application rather than the component itself.
180
158c88c0 181=head1 SEE ALSO
182
e7f1cf73 183L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
158c88c0 184
2f381252 185=head1 AUTHORS
158c88c0 186
2f381252 187Catalyst Contributors, see Catalyst.pm
158c88c0 188
189=head1 COPYRIGHT
190
191This program is free software, you can redistribute it and/or modify it under
192the same terms as Perl itself.
193
85d9fce6 194=cut