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