Updated some core stuff, cleanups, better errors...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
CommitLineData
fc7ec1d9 1package Catalyst::Base;
2
3use strict;
4use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
84cf74e7 5use Catalyst::Utils;
fc7ec1d9 6use 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 13sub 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 20sub FETCH_CODE_ATTRIBUTES { $_[0]->_attr_cache->{ $_[1] } || () }
d70195d8 21
fc7ec1d9 22=head1 NAME
23
24Catalyst::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
57This is the universal base class for Catalyst components
58(Model/View/Controller).
59
60It provides you with a generic new() for instantiation through Catalyst's
61component 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
71sub 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
92sub 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 108sub process {
109 die( ( ref $_[0] || $_[0] ) . " did not override Catalyst::Base::process" );
110}
fc7ec1d9 111
bea4160a 112=back
113
fc7ec1d9 114=head1 SEE ALSO
115
116L<Catalyst>.
117
118=head1 AUTHOR
119
120Sebastian Riedel, C<sri@cpan.org>
61b1e958 121Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 122
123=head1 COPYRIGHT
124
125This program is free software, you can redistribute it and/or modify it under
126the same terms as Perl itself.
127
128=cut
129
1301;