Big change, new installer and home detection
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
1 package Catalyst::Base;
2
3 use strict;
4 use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
5 use Catalyst::Utils;
6 use NEXT;
7
8 __PACKAGE__->mk_classdata($_) for qw/_attrcache _cache _config/;
9 __PACKAGE__->_attrcache( {} );
10 __PACKAGE__->_cache( [] );
11
12 # note - see attributes(3pm)
13 sub MODIFY_CODE_ATTRIBUTES {
14     my ( $class, $code, @attrs ) = @_;
15     $class->_attrcache->{$code} = [@attrs];
16     push @{ $class->_cache }, [ $code, [@attrs] ];
17     return ();
18 }
19
20 sub FETCH_CODE_ATTRIBUTES { $_[0]->_attrcache->{ $_[1] } || () }
21
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/);
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
63 =head1 METHODS
64
65 =over 4
66
67 =item new($c)
68
69 =cut
70
71 sub new {
72     my ( $self, $c ) = @_;
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);
79 }
80
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, ...)
89
90 =cut
91
92 sub config {
93     my $self = shift;
94     $self->_config( {} ) unless $self->_config;
95     if ( $_[0] ) {
96         my $config = $_[1] ? {@_} : $_[0];
97         while ( my ( $key, $val ) = each %$config ) {
98             $self->_config->{$key} = $val;
99         }
100     }
101     return $self->_config;
102 }
103
104 =item $c->process()
105
106 =cut
107
108 sub process {
109     die( ( ref $_[0] || $_[0] ) . " did not override Catalyst::Base::process" );
110 }
111
112 =back
113
114 =head1 SEE ALSO
115
116 L<Catalyst>.
117
118 =head1 AUTHOR
119
120 Sebastian Riedel, C<sri@cpan.org>
121 Marcus Ramberg, C<mramberg@cpan.org>
122
123 =head1 COPYRIGHT
124
125 This program is free software, you can redistribute it and/or modify it under
126 the same terms as Perl itself.
127
128 =cut
129
130 1;