Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Base; |
2 | |
3 | use strict; |
4 | use base qw/Class::Data::Inheritable Class::Accessor::Fast/; |
9b793a51 |
5 | use Catalyst::Utils; |
a2f2cde9 |
6 | use Catalyst::Exception; |
fc7ec1d9 |
7 | use NEXT; |
8 | |
9b793a51 |
9 | # Some caches... |
812a28c9 |
10 | __PACKAGE__->mk_classdata($_) for qw/_attr_cache _action_cache _config/; |
11 | __PACKAGE__->_attr_cache( {} ); |
12 | __PACKAGE__->_action_cache( [] ); |
ac733264 |
13 | |
61b1e958 |
14 | # note - see attributes(3pm) |
ac733264 |
15 | sub MODIFY_CODE_ATTRIBUTES { |
16 | my ( $class, $code, @attrs ) = @_; |
812a28c9 |
17 | $class->_attr_cache->{$code} = [@attrs]; |
18 | push @{ $class->_action_cache }, [ $code, [@attrs] ]; |
ac733264 |
19 | return (); |
20 | } |
fc7ec1d9 |
21 | |
812a28c9 |
22 | sub FETCH_CODE_ATTRIBUTES { $_[0]->_attr_cache->{ $_[1] } || () } |
d70195d8 |
23 | |
fc7ec1d9 |
24 | =head1 NAME |
25 | |
26 | Catalyst::Base - Catalyst Universal Base Class |
27 | |
28 | =head1 SYNOPSIS |
29 | |
30 | # lib/MyApp/Model/Something.pm |
31 | package MyApp::Model::Something; |
32 | |
33 | use base 'Catalyst::Base'; |
34 | |
35 | __PACKAGE__->config( foo => 'bar' ); |
36 | |
37 | sub test { |
38 | my $self = shift; |
39 | return $self->{foo}; |
40 | } |
41 | |
42 | sub forward_to_me { |
43 | my ( $self, $c ) = @_; |
44 | $c->response->output( $self->{foo} ); |
45 | } |
46 | |
47 | 1; |
48 | |
49 | # Methods can be a request step |
50 | $c->forward(qw/MyApp::Model::Something forward_to_me/); |
fc7ec1d9 |
51 | |
52 | # Or just methods |
53 | print $c->comp('MyApp::Model::Something')->test; |
54 | |
55 | print $c->comp('MyApp::Model::Something')->{foo}; |
56 | |
57 | =head1 DESCRIPTION |
58 | |
59 | This is the universal base class for Catalyst components |
60 | (Model/View/Controller). |
61 | |
62 | It provides you with a generic new() for instantiation through Catalyst's |
63 | component loader with config() support and a process() method placeholder. |
64 | |
23f9d934 |
65 | =head1 METHODS |
66 | |
67 | =over 4 |
68 | |
69 | =item new($c) |
fc7ec1d9 |
70 | |
71 | =cut |
72 | |
73 | sub new { |
74 | my ( $self, $c ) = @_; |
9b793a51 |
75 | |
76 | # You'll find yourself naked and strung from a tree if you ever |
77 | # remove this again, k? :) |
78 | my $class = ref $self || $self; |
79 | my $appname = Catalyst::Utils::class2appclass($class); |
80 | my $suffix = Catalyst::Utils::class2classsuffix($class); |
81 | my $appconfig = {}; |
82 | |
83 | # Not catched, for compatibility with non Catalyst apps |
84 | eval '$appconfig = $appname->config->{$suffix} || {}'; |
85 | my $config = { %{ $self->config }, %{$appconfig} }; |
86 | |
a268a011 |
87 | # Temporary fix, some components does not pass context to constructor |
88 | my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; |
89 | |
9b793a51 |
90 | return $self->NEXT::new( { %{$config}, %{$arguments} } ); |
fc7ec1d9 |
91 | } |
92 | |
23f9d934 |
93 | # remember to leave blank lines between the consecutive =item's |
94 | # otherwise the pod tools don't recognize the subsequent =items |
95 | |
96 | =item $c->config |
97 | |
98 | =item $c->config($hashref) |
99 | |
100 | =item $c->config($key, $value, ...) |
fc7ec1d9 |
101 | |
102 | =cut |
103 | |
104 | sub config { |
105 | my $self = shift; |
106 | $self->_config( {} ) unless $self->_config; |
812a28c9 |
107 | if (@_) { |
c19e2f4a |
108 | my $config = @_ > 1 ? {@_} : $_[0]; |
fc7ec1d9 |
109 | while ( my ( $key, $val ) = each %$config ) { |
110 | $self->_config->{$key} = $val; |
111 | } |
112 | } |
113 | return $self->_config; |
114 | } |
115 | |
23f9d934 |
116 | =item $c->process() |
fc7ec1d9 |
117 | |
118 | =cut |
119 | |
d70195d8 |
120 | sub process { |
a2f2cde9 |
121 | |
9b793a51 |
122 | Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] ) |
123 | . " did not override Catalyst::Base::process" ); |
d70195d8 |
124 | } |
fc7ec1d9 |
125 | |
4aff785c |
126 | =item FETCH_CODE_ATTRIBUTES |
127 | |
128 | =item MODIFY_CODE_ATTRIBUTES |
129 | |
bea4160a |
130 | =back |
131 | |
fc7ec1d9 |
132 | =head1 SEE ALSO |
133 | |
134 | L<Catalyst>. |
135 | |
136 | =head1 AUTHOR |
137 | |
138 | Sebastian Riedel, C<sri@cpan.org> |
61b1e958 |
139 | Marcus Ramberg, C<mramberg@cpan.org> |
fc7ec1d9 |
140 | |
141 | =head1 COPYRIGHT |
142 | |
143 | This program is free software, you can redistribute it and/or modify it under |
144 | the same terms as Perl itself. |
145 | |
146 | =cut |
147 | |
148 | 1; |