Commit | Line | Data |
158c88c0 |
1 | package Catalyst::Component; |
2 | |
3 | use strict; |
684d10ed |
4 | use base qw/Class::Accessor::Fast Class::Data::Inheritable/; |
158c88c0 |
5 | use NEXT; |
6 | |
d0d4d785 |
7 | __PACKAGE__->mk_classdata($_) for qw/_config _plugins/; |
158c88c0 |
8 | |
9 | =head1 NAME |
10 | |
11 | Catalyst::Component - Catalyst Component Base Class |
12 | |
13 | =head1 SYNOPSIS |
14 | |
15 | # lib/MyApp/Model/Something.pm |
16 | package MyApp::Model::Something; |
17 | |
e7f1cf73 |
18 | use base 'Catalyst::Component'; |
158c88c0 |
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 | |
44 | This is the universal base class for Catalyst components |
45 | (Model/View/Controller). |
46 | |
47 | It provides you with a generic new() for instantiation through Catalyst's |
48 | component loader with config() support and a process() method placeholder. |
49 | |
50 | =head1 METHODS |
51 | |
b5ecfcf0 |
52 | =head2 new($c) |
158c88c0 |
53 | |
54 | =cut |
55 | |
56 | sub new { |
57 | my ( $self, $c ) = @_; |
58 | |
59 | # Temporary fix, some components does not pass context to constructor |
60 | my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; |
61 | |
62 | return $self->NEXT::new( { %{ $self->config }, %{$arguments} } ); |
63 | } |
64 | |
22247e54 |
65 | =head2 COMPONENT($c) |
66 | |
67 | =cut |
68 | |
69 | sub COMPONENT { |
70 | my ( $self, $c ) = @_; |
71 | |
72 | # Temporary fix, some components does not pass context to constructor |
73 | my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; |
74 | |
75 | if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) { |
76 | return $new; |
77 | } |
78 | else { |
79 | if ( my $new = $self->new( $c, $arguments ) ) { |
80 | return $new; |
81 | } |
82 | else { |
83 | my $class = ref $self || $self; |
84 | my $new = { %{ $self->config }, %{$arguments} }; |
85 | return bless $new, $class; |
86 | } |
87 | } |
88 | } |
89 | |
b5ecfcf0 |
90 | # remember to leave blank lines between the consecutive =head2's |
91 | # otherwise the pod tools don't recognize the subsequent =head2s |
158c88c0 |
92 | |
b5ecfcf0 |
93 | =head2 $c->config |
158c88c0 |
94 | |
b5ecfcf0 |
95 | =head2 $c->config($hashref) |
158c88c0 |
96 | |
b5ecfcf0 |
97 | =head2 $c->config($key, $value, ...) |
158c88c0 |
98 | |
99 | =cut |
100 | |
101 | sub config { |
102 | my $self = shift; |
103 | $self->_config( {} ) unless $self->_config; |
104 | if (@_) { |
105 | my $config = @_ > 1 ? {@_} : $_[0]; |
106 | while ( my ( $key, $val ) = each %$config ) { |
107 | $self->_config->{$key} = $val; |
108 | } |
109 | } |
110 | return $self->_config; |
111 | } |
112 | |
b5ecfcf0 |
113 | =head2 $c->process() |
158c88c0 |
114 | |
115 | =cut |
116 | |
117 | sub process { |
118 | |
119 | Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] ) |
120 | . " did not override Catalyst::Component::process" ); |
121 | } |
122 | |
158c88c0 |
123 | =head1 SEE ALSO |
124 | |
e7f1cf73 |
125 | L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>. |
158c88c0 |
126 | |
127 | =head1 AUTHOR |
128 | |
129 | Sebastian Riedel, C<sri@cpan.org> |
130 | Marcus Ramberg, C<mramberg@cpan.org> |
131 | Matt S Trout, C<mst@shadowcatsystems.co.uk> |
132 | |
133 | =head1 COPYRIGHT |
134 | |
135 | This program is free software, you can redistribute it and/or modify it under |
136 | the same terms as Perl itself. |
137 | |
138 | =cut |
139 | |
140 | 1; |