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