1 package Catalyst::Base;
4 use base qw/Catalyst::AttrContainer Class::Accessor::Fast/;
6 use Catalyst::Exception;
11 __PACKAGE__->mk_classdata($_) for qw/_config _dispatch_steps/;
13 __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
15 sub _DISPATCH : Private {
16 my ( $self, $c ) = @_;
18 foreach my $disp ( @{ $self->_dispatch_steps } ) {
19 last unless $c->forward($disp);
25 sub _BEGIN : Private {
26 my ( $self, $c ) = @_;
27 my $begin = ($c->get_actions( 'begin', $c->namespace))[-1];
28 return 1 unless $begin;
30 return !@{ $c->error };
34 my ( $self, $c ) = @_;
35 my @auto = $c->get_actions('auto', $c->namespace);
36 foreach my $auto (@auto) {
38 return 0 unless $c->state;
43 sub _ACTION : Private {
44 my ( $self, $c ) = @_;
45 $c->action->execute($c);
46 return !@{ $c->error };
50 my ( $self, $c ) = @_;
51 my $end = ($c->get_actions( 'end', $c->namespace))[-1];
54 return !@{ $c->error };
57 sub action_namespace {
58 my ( $self, $c ) = @_;
60 Catalyst::Utils::class2prefix(
61 ref $self, $c->config->{case_sensitive} ) || '';
64 sub register_actions {
65 my ( $self, $c ) = @_;
66 my $class = ref $self || $self;
67 my $namespace = $self->action_namespace( $c );
69 $methods{$self->can($_)} = $_ for @{Class::Inspector->methods($class)||[]};
70 foreach my $cache (@{$self->_action_cache}) {
71 my $code = $cache->[0];
72 my $method = $methods{$code};
74 my $attrs = $self->_parse_attrs(@{$cache->[1]});
75 if ($attrs->{Private} && ( keys %$attrs > 1 ) ) {
76 $c->log->debug( 'Bad action definition "'
77 . join( ' ', @{$cache->[1]} )
78 . qq/" for "$class->$method"/ )
82 my $reverse = $namespace ? "$namespace/$method" : $method;
83 my $action = Catalyst::Action->new(
88 namespace => $namespace,
93 $c->dispatcher->register($c, $action);
98 my ( $self, @attrs ) = @_;
100 foreach my $attr (@attrs) {
102 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
104 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+)\s*\))?$/ ) ) {
106 if ( defined $value ) {
107 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
109 push( @{ $attributes{$key} }, $value );
118 Catalyst::Base - Catalyst Universal Base Class
122 # lib/MyApp/Model/Something.pm
123 package MyApp::Model::Something;
125 use base 'Catalyst::Base';
127 __PACKAGE__->config( foo => 'bar' );
135 my ( $self, $c ) = @_;
136 $c->response->output( $self->{foo} );
141 # Methods can be a request step
142 $c->forward(qw/MyApp::Model::Something forward_to_me/);
145 print $c->comp('MyApp::Model::Something')->test;
147 print $c->comp('MyApp::Model::Something')->{foo};
151 This is the universal base class for Catalyst components
152 (Model/View/Controller).
154 It provides you with a generic new() for instantiation through Catalyst's
155 component loader with config() support and a process() method placeholder.
166 my ( $self, $c ) = @_;
168 # Temporary fix, some components does not pass context to constructor
169 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
171 return $self->NEXT::new( { %{ $self->config }, %{$arguments} } );
174 # remember to leave blank lines between the consecutive =item's
175 # otherwise the pod tools don't recognize the subsequent =items
179 =item $c->config($hashref)
181 =item $c->config($key, $value, ...)
187 $self->_config( {} ) unless $self->_config;
189 my $config = @_ > 1 ? {@_} : $_[0];
190 while ( my ( $key, $val ) = each %$config ) {
191 $self->_config->{$key} = $val;
194 return $self->_config;
203 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
204 . " did not override Catalyst::Base::process" );
215 Sebastian Riedel, C<sri@cpan.org>
216 Marcus Ramberg, C<mramberg@cpan.org>
217 Matt S Trout, C<mst@shadowcatsystems.co.uk>
221 This program is free software, you can redistribute it and/or modify it under
222 the same terms as Perl itself.