1 package Catalyst::Base;
4 use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
6 use Catalyst::Exception;
10 __PACKAGE__->mk_classdata($_) for qw/_attr_cache _action_cache _config/;
11 __PACKAGE__->_attr_cache( {} );
12 __PACKAGE__->_action_cache( [] );
14 # note - see attributes(3pm)
15 sub MODIFY_CODE_ATTRIBUTES {
16 my ( $class, $code, @attrs ) = @_;
17 $class->_attr_cache->{$code} = [@attrs];
18 push @{ $class->_action_cache }, [ $code, [@attrs] ];
22 sub FETCH_CODE_ATTRIBUTES { $_[0]->_attr_cache->{ $_[1] } || () }
26 Catalyst::Base - Catalyst Universal Base Class
30 # lib/MyApp/Model/Something.pm
31 package MyApp::Model::Something;
33 use base 'Catalyst::Base';
35 __PACKAGE__->config( foo => 'bar' );
43 my ( $self, $c ) = @_;
44 $c->response->output( $self->{foo} );
49 # Methods can be a request step
50 $c->forward(qw/MyApp::Model::Something forward_to_me/);
53 print $c->comp('MyApp::Model::Something')->test;
55 print $c->comp('MyApp::Model::Something')->{foo};
59 This is the universal base class for Catalyst components
60 (Model/View/Controller).
62 It provides you with a generic new() for instantiation through Catalyst's
63 component loader with config() support and a process() method placeholder.
74 my ( $self, $c ) = @_;
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);
83 # Not catched, for compatibility with non Catalyst apps
84 eval '$appconfig = $appname->config->{$suffix} || {}';
85 my $config = { %{ $self->config }, %{$appconfig} };
87 # Temporary fix, some components does not pass context to constructor
88 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
90 return $self->NEXT::new( { %{$config}, %{$arguments} } );
93 # remember to leave blank lines between the consecutive =item's
94 # otherwise the pod tools don't recognize the subsequent =items
98 =item $c->config($hashref)
100 =item $c->config($key, $value, ...)
106 $self->_config( {} ) unless $self->_config;
108 my $config = @_ > 1 ? {@_} : $_[0];
109 while ( my ( $key, $val ) = each %$config ) {
110 $self->_config->{$key} = $val;
113 return $self->_config;
122 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
123 . " did not override Catalyst::Base::process" );
126 =item FETCH_CODE_ATTRIBUTES
128 =item MODIFY_CODE_ATTRIBUTES
138 Sebastian Riedel, C<sri@cpan.org>
139 Marcus Ramberg, C<mramberg@cpan.org>
143 This program is free software, you can redistribute it and/or modify it under
144 the same terms as Perl itself.