added dependency to Text::ASCITable for some eyecandy in logs,
[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/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     unless ( $caller->isa($self) ) {
130         no strict 'refs';
131         push @{"$caller\::ISA"}, $self;
132     }
133
134     if ( $caller->engine ) {
135         return;    # Catalyst is allready initialized
136     }
137
138     unless ( $caller->log ) {
139         $caller->log( Catalyst::Log->new );
140     }
141
142     if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($caller) . '_DEBUG' } ) {
143         no strict 'refs';
144         *{"$caller\::debug"} = sub { 1 };
145         $caller->log->debug('Debug messages enabled');
146     }
147
148     my $engine = 'Catalyst::Engine::CGI';
149
150     if ( $ENV{MOD_PERL} ) {
151
152         require mod_perl;
153
154         if ( $mod_perl::VERSION >= 1.99 ) {
155             $engine = 'Catalyst::Engine::Apache::MP2';
156         }
157         else {
158             $engine = 'Catalyst::Engine::Apache::MP1';
159         }
160     }
161
162     my @plugins;
163     foreach (@options) {
164         if (/^\-Debug$/) {
165             next if $caller->debug;
166             no strict 'refs';
167             *{"$caller\::debug"} = sub { 1 };
168             $caller->log->debug('Debug messages enabled');
169         }
170         elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" }
171         elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) }
172         else {
173             my $plugin = "Catalyst::Plugin::$_";
174
175             $plugin->require;
176
177             if ($@) {
178                 $caller->log->error(qq/Couldn't load plugin "$plugin", "$@"/);
179             }
180             else {
181                 push @plugins, "  + $plugin";
182                 no strict 'refs';
183                 push @{"$caller\::ISA"}, $plugin;
184             }
185         }
186     }
187     my $t = Text::ASCIITable->new;
188     $t->setCols('Class');
189     $t->addRow($_) for @plugins;
190     $caller->log->debug( 'Loaded plugins', $t->draw )
191       if ( @plugins && $caller->debug );
192
193     # Engine
194     $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}"
195       if $ENV{CATALYST_ENGINE};
196
197     $engine->require;
198     die qq/Couldn't load engine "$engine", "$@"/ if $@;
199     {
200         no strict 'refs';
201         push @{"$caller\::ISA"}, $engine;
202     }
203     $caller->engine($engine);
204     $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug;
205 }
206
207 =item $c->engine
208
209 Contains the engine class.
210
211 =item $c->log
212
213 Contains the logging object.  Unless it is already set Catalyst sets this up with a
214 C<Catalyst::Log> object.  To use your own log class:
215
216     $c->log( MyLogger->new );
217     $c->log->info("now logging with my own logger!");
218
219 Your log class should implement the methods described in the C<Catalyst::Log>
220 man page.
221
222
223 =back
224
225 =head1 SUPPORT
226
227 IRC:
228
229     Join #catalyst on irc.perl.org.
230
231 Mailing-Lists:
232
233     http://lists.rawmode.org/mailman/listinfo/catalyst
234     http://lists.rawmode.org/mailman/listinfo/catalyst-dev
235
236 =head1 SEE ALSO
237
238 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
239 L<Catalyst::Response>, L<Catalyst::Engine>
240
241 =head1 AUTHOR
242
243 Sebastian Riedel, C<sri@oook.de>
244
245 =head1 THANK YOU
246
247 Andrew Ford, Andrew Ruthven, Christian Hansen, Christopher Hicks,
248 Dan Sully, Danijel Milicevic, David Naughton, Gary Ashton Jones,
249 Jesse Sheidlower, Johan Lindstrom, Marcus Ramberg, Tatsuhiko Miyagawa
250 and all the others who've helped.
251
252 =head1 LICENSE
253
254 This library is free software . You can redistribute it and/or modify it under
255 the same terms as perl itself.
256
257 =cut
258
259 1;