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