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