removing unnecessary code in Catalyst.pm
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
1 package Catalyst;
2
3 use strict;
4 use base 'Class::Data::Inheritable';
5 use UNIVERSAL::require;
6 use Catalyst::Log;
7
8 __PACKAGE__->mk_classdata($_) for qw/_config engine log/;
9
10 our $VERSION = '4.34';
11 our @ISA;
12
13 =head1 NAME
14
15 Catalyst - The Elegant MVC Web Application Framework
16
17 =head1 SYNOPSIS
18
19     # use the helper to start a new application
20     catalyst.pl MyApp
21     cd MyApp
22
23     # add models, views, controllers
24     script/create.pl model Something
25     script/create.pl view Stuff
26     script/create.pl controller Yada
27
28     # built in testserver
29     script/server.pl
30
31     # command line interface
32     script/test.pl /yada
33
34
35     use Catalyst;
36
37     use Catalyst qw/My::Module My::OtherModule/;
38
39     use Catalyst '-Debug';
40
41     use Catalyst qw/-Debug -Engine=CGI/;
42
43     __PACKAGE__->action( '!default' => sub { $_[1]->res->output('Hello') } );
44
45     __PACKAGE__->action(
46         'index.html' => sub {
47             my ( $self, $c ) = @_;
48             $c->res->output('Hello');
49             $c->forward('_foo');
50         }
51     );
52
53     __PACKAGE__->action(
54         '/^product[_]*(\d*).html$/' => sub {
55             my ( $self, $c ) = @_;
56             $c->stash->{template} = 'product.tt';
57             $c->stash->{product} = $c->req->snippets->[0];
58         }
59     );
60
61 See also L<Catalyst::Manual::Intro>
62
63 =head1 DESCRIPTION
64
65 Catalyst is based upon L<Maypole>, which you should consider for smaller
66 projects.
67
68 The key concept of Catalyst is DRY (Don't Repeat Yourself).
69
70 See L<Catalyst::Manual> for more documentation.
71
72 Catalyst plugins can be loaded by naming them as arguments to the "use Catalyst" statement.
73 Omit the C<Catalyst::Plugin::> prefix from the plugin name, 
74 so C<Catalyst::Plugin::My::Module> becomes C<My::Module>.
75
76     use Catalyst 'My::Module';
77
78 Special flags like -Debug and -Engine can also be specifed as arguments when
79 Catalyst is loaded:
80
81     use Catalyst qw/-Debug My::Module/;
82
83 The position of plugins and flags in the chain is important, because they are
84 loaded in exactly the order that they appear.
85
86 The following flags are supported:
87
88 =over 4
89
90 =item -Debug
91
92 enables debug output, i.e.:
93
94     use Catalyst '-Debug';
95
96 this is equivalent to:
97
98     use Catalyst;
99     sub debug { 1 }
100
101 =item -Engine
102
103 Force Catalyst to use a specific engine.
104 Omit the C<Catalyst::Engine::> prefix of the engine name, i.e.:
105
106     use Catalyst '-Engine=CGI';
107
108 =back
109
110 =head1 METHODS
111
112 =over 4
113
114 =item debug
115
116 Overload to enable debug messages.
117
118 =cut
119
120 sub debug { 0 }
121
122 =item config
123
124 Returns a hashref containing your applications settings.
125
126 =cut
127
128 sub config {
129     my $self = shift;
130     $self->_config( {} ) unless $self->_config;
131     if ( $_[0] ) {
132         my $config = $_[1] ? {@_} : $_[0];
133         while ( my ( $key, $val ) = each %$config ) {
134             $self->_config->{$key} = $val;
135         }
136     }
137     return $self->_config;
138 }
139
140 sub import {
141     my ( $self, @options ) = @_;
142     my $caller = caller(0);
143
144     unless ( $caller->isa($self) ) {
145         no strict 'refs';
146         push @{"$caller\::ISA"}, $self;
147     }
148
149     unless ( $caller->log ) {
150         $caller->log( Catalyst::Log->new );
151     }
152
153     # Options
154     my $engine =
155       $ENV{MOD_PERL} ? 'Catalyst::Engine::Apache' : 'Catalyst::Engine::CGI';
156     foreach (@options) {
157         if (/^\-Debug$/) {
158             no warnings;
159             no strict 'refs';
160             *{"$caller\::debug"} = sub { 1 };
161             $caller->log->debug('Debug messages enabled');
162         }
163         elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" }
164         elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) }
165         else {
166             my $plugin = "Catalyst::Plugin::$_";
167
168             # Plugin caller should be our application class
169             eval "package $caller; require $plugin";
170             if ($@) {
171                 $caller->log->error(qq/Couldn't load plugin "$plugin", "$@"/);
172             }
173             else {
174                 $caller->log->debug(qq/Loaded plugin "$plugin"/)
175                   if $caller->debug;
176                 no strict 'refs';
177                 push @{"$caller\::ISA"}, $plugin;
178             }
179         }
180     }
181
182     # Engine
183     $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}"
184       if $ENV{CATALYST_ENGINE};
185     $engine->require;
186     die qq/Couldn't load engine "$engine", "$@"/ if $@;
187     {
188         no strict 'refs';
189         push @{"$caller\::ISA"}, $engine;
190     }
191     $caller->engine($engine);
192     $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug;
193 }
194
195 =item $c->engine
196
197 Contains the engine class.
198
199 =item $c->log
200
201 Contains the logging object.  Unless it is already set Catalyst sets this up with a
202 C<Catalyst::Log> object.  To use your own log class:
203
204     $c->log( MyLogger->new );
205     $c->log->info("now logging with my own logger!");
206
207 Your log class should implement the methods described in the C<Catalyst::Log>
208 man page.
209
210
211 =back
212
213 =head1 SUPPORT
214
215 IRC:
216
217     Join #catalyst on irc.perl.org.
218
219 Mailing-Lists:
220
221     http://lists.rawmode.org/mailman/listinfo/catalyst
222     http://lists.rawmode.org/mailman/listinfo/catalyst-dev
223     
224 =head1 SEE ALSO
225
226 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
227 L<Catalyst::Response>, L<Catalyst::Engine>
228
229 =head1 AUTHOR
230
231 Sebastian Riedel, C<sri@oook.de>
232
233 =head1 THANK YOU
234
235 Andrew Ford, Andrew Ruthven, Christian Hansen, Christopher Hicks,
236 Dan Sully, Danijel Milicevic, David Naughton, Gary Ashton Jones,
237 Jesse Sheidlower, Johan Lindstrom, Marcus Ramberg, Tatsuhiko Miyagawa
238 and all the others who've helped.
239
240 =head1 LICENSE
241
242 This library is free software . You can redistribute it and/or modify it under
243 the same terms as perl itself.
244
245 =cut
246
247 1;