Fixed some bugs and improved logs
[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;
6
7__PACKAGE__->mk_classdata($_) for qw/_config/;
8
9=head1 NAME
10
11Catalyst::Component - Catalyst Component Base Class
12
13=head1 SYNOPSIS
14
15 # lib/MyApp/Model/Something.pm
16 package MyApp::Model::Something;
17
18 use base 'Catalyst::Base';
19
20 __PACKAGE__->config( foo => 'bar' );
21
22 sub test {
23 my $self = shift;
24 return $self->{foo};
25 }
26
27 sub forward_to_me {
28 my ( $self, $c ) = @_;
29 $c->response->output( $self->{foo} );
30 }
31
32 1;
33
34 # Methods can be a request step
35 $c->forward(qw/MyApp::Model::Something forward_to_me/);
36
37 # Or just methods
38 print $c->comp('MyApp::Model::Something')->test;
39
40 print $c->comp('MyApp::Model::Something')->{foo};
41
42=head1 DESCRIPTION
43
44This is the universal base class for Catalyst components
45(Model/View/Controller).
46
47It provides you with a generic new() for instantiation through Catalyst's
48component loader with config() support and a process() method placeholder.
49
50=head1 METHODS
51
52=over 4
53
54=item new($c)
55
56=cut
57
58sub new {
59 my ( $self, $c ) = @_;
60
61 # Temporary fix, some components does not pass context to constructor
62 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
63
64 return $self->NEXT::new( { %{ $self->config }, %{$arguments} } );
65}
66
67# remember to leave blank lines between the consecutive =item's
68# otherwise the pod tools don't recognize the subsequent =items
69
70=item $c->config
71
72=item $c->config($hashref)
73
74=item $c->config($key, $value, ...)
75
76=cut
77
78sub config {
79 my $self = shift;
80 $self->_config( {} ) unless $self->_config;
81 if (@_) {
82 my $config = @_ > 1 ? {@_} : $_[0];
83 while ( my ( $key, $val ) = each %$config ) {
84 $self->_config->{$key} = $val;
85 }
86 }
87 return $self->_config;
88}
89
90=item $c->process()
91
92=cut
93
94sub process {
95
96 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
97 . " did not override Catalyst::Component::process" );
98}
99
100=back
101
102=head1 SEE ALSO
103
104L<Catalyst>.
105
106=head1 AUTHOR
107
108Sebastian Riedel, C<sri@cpan.org>
109Marcus Ramberg, C<mramberg@cpan.org>
110Matt S Trout, C<mst@shadowcatsystems.co.uk>
111
112=head1 COPYRIGHT
113
114This program is free software, you can redistribute it and/or modify it under
115the same terms as Perl itself.
116
117=cut
118
1191;