document $c->detach with no args
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Component.pm
CommitLineData
158c88c0 1package Catalyst::Component;
2
3use strict;
684d10ed 4use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
158c88c0 5use NEXT;
358e1592 6use Catalyst::Utils;
158c88c0 7
d0d4d785 8__PACKAGE__->mk_classdata($_) for qw/_config _plugins/;
158c88c0 9
10=head1 NAME
11
12Catalyst::Component - Catalyst Component Base Class
13
14=head1 SYNOPSIS
15
16 # lib/MyApp/Model/Something.pm
17 package MyApp::Model::Something;
18
e7f1cf73 19 use base 'Catalyst::Component';
158c88c0 20
21 __PACKAGE__->config( foo => 'bar' );
22
23 sub test {
24 my $self = shift;
25 return $self->{foo};
26 }
27
28 sub forward_to_me {
29 my ( $self, $c ) = @_;
30 $c->response->output( $self->{foo} );
31 }
32
33 1;
34
35 # Methods can be a request step
36 $c->forward(qw/MyApp::Model::Something forward_to_me/);
37
38 # Or just methods
39 print $c->comp('MyApp::Model::Something')->test;
40
41 print $c->comp('MyApp::Model::Something')->{foo};
42
43=head1 DESCRIPTION
44
45This is the universal base class for Catalyst components
46(Model/View/Controller).
47
48It provides you with a generic new() for instantiation through Catalyst's
49component loader with config() support and a process() method placeholder.
50
51=head1 METHODS
52
825dbf85 53=head2 new($c, $arguments)
54
55Called by COMPONENT to instantiate the component; should return an object
56to be stored in the application's component hash.
158c88c0 57
58=cut
59
60sub new {
61 my ( $self, $c ) = @_;
62
63 # Temporary fix, some components does not pass context to constructor
64 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
65
baf6a3db 66 return $self->NEXT::new( $self->merge_config_hashes( $self->config, $arguments ) );
158c88c0 67}
68
825dbf85 69=head2 COMPONENT($c, $arguments)
70
71If this method is present (as it is on all Catalyst::Component subclasses,
72it is called by Catalyst during setup_components with the application class
73as $c and any config entry on the application for this component (for example,
74in the case of MyApp::Controller::Foo this would be
75MyApp->config->{'Controller::Foo'}). The arguments are expected to be a hashref
76and are merged with the __PACKAGE__->config hashref before calling ->new to
77instantiate the component.
22247e54 78
79=cut
80
81sub COMPONENT {
82 my ( $self, $c ) = @_;
83
84 # Temporary fix, some components does not pass context to constructor
85 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
86
87 if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) {
88 return $new;
89 }
90 else {
91 if ( my $new = $self->new( $c, $arguments ) ) {
92 return $new;
93 }
94 else {
95 my $class = ref $self || $self;
baf6a3db 96 my $new = $self->merge_config_hashes( $self->config, $arguments );
22247e54 97 return bless $new, $class;
98 }
99 }
100}
101
b5ecfcf0 102# remember to leave blank lines between the consecutive =head2's
103# otherwise the pod tools don't recognize the subsequent =head2s
158c88c0 104
b5ecfcf0 105=head2 $c->config
158c88c0 106
b5ecfcf0 107=head2 $c->config($hashref)
158c88c0 108
b5ecfcf0 109=head2 $c->config($key, $value, ...)
158c88c0 110
111=cut
112
113sub config {
114 my $self = shift;
5e707396 115 my $config = $self->_config;
116 unless ($config) {
c7a54b4e 117 $self->_config( $config = {} );
5e707396 118 }
158c88c0 119 if (@_) {
baf6a3db 120 my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
121 $self->_config(
122 $self->merge_config_hashes( $config, $newconfig )
123 );
158c88c0 124 }
5e707396 125 return $config;
158c88c0 126}
127
b5ecfcf0 128=head2 $c->process()
158c88c0 129
130=cut
131
132sub process {
133
134 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
135 . " did not override Catalyst::Component::process" );
136}
137
358e1592 138=head2 $c->merge_config_hashes( $hashref, $hashref )
baf6a3db 139
140Merges two hashes together recursively, giving right-hand precedence.
141
142=cut
143
144sub merge_config_hashes {
145 my ( $self, $lefthash, $righthash ) = @_;
146
358e1592 147 return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
baf6a3db 148}
149
825dbf85 150=head1 OPTIONAL METHODS
151
152=head2 ACCEPT_CONTEXT($c, @args)
153
154Catalyst components are normally initalized during server startup, either
155as a Class or a Instance. However, some components require information about
156the current request. To do so, they can implement an ACCEPT_CONTEXT method.
157
158If this method is present, it is called during $c->comp/controller/model/view
159with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
160would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
161($c, 'bar', 'baz')) and the return value of this method is returned to the
162calling code in the application rather than the component itself.
163
158c88c0 164=head1 SEE ALSO
165
e7f1cf73 166L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
158c88c0 167
168=head1 AUTHOR
169
170Sebastian Riedel, C<sri@cpan.org>
171Marcus Ramberg, C<mramberg@cpan.org>
172Matt S Trout, C<mst@shadowcatsystems.co.uk>
173
174=head1 COPYRIGHT
175
176This program is free software, you can redistribute it and/or modify it under
177the same terms as Perl itself.
178
179=cut
180
1811;