Move setup to C::Setup
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
CommitLineData
fc7ec1d9 1package Catalyst;
2
3use strict;
5d9a6d47 4use base qw[ Catalyst::Base Catalyst::Setup ];
fc7ec1d9 5use UNIVERSAL::require;
a2f2cde9 6use Catalyst::Exception;
fc7ec1d9 7use Catalyst::Log;
812a28c9 8use Catalyst::Utils;
5d9a6d47 9use NEXT;
0f7ecc53 10use Text::ASCIITable;
4f6748f1 11use Path::Class;
367d15f8 12our $CATALYST_SCRIPT_GEN = 4;
fc7ec1d9 13
abddf0b5 14our $VERSION = '5.24';
fc7ec1d9 15our @ISA;
16
17=head1 NAME
18
19Catalyst - The Elegant MVC Web Application Framework
20
21=head1 SYNOPSIS
22
23 # use the helper to start a new application
91864987 24 catalyst.pl MyApp
fc7ec1d9 25 cd MyApp
26
27 # add models, views, controllers
ae4e40a7 28 script/myapp_create.pl model Something
29 script/myapp_create.pl view Stuff
30 script/myapp_create.pl controller Yada
fc7ec1d9 31
32 # built in testserver
ae4e40a7 33 script/myapp_server.pl
fc7ec1d9 34
35 # command line interface
ae4e40a7 36 script/myapp_test.pl /yada
fc7ec1d9 37
38
fc7ec1d9 39 use Catalyst;
40
41 use Catalyst qw/My::Module My::OtherModule/;
42
43 use Catalyst '-Debug';
44
45 use Catalyst qw/-Debug -Engine=CGI/;
46
5a8ed4fe 47 sub default : Private { $_[1]->res->output('Hello') } );
48
e3dc9d78 49 sub index : Path('/index.html') {
5a8ed4fe 50 my ( $self, $c ) = @_;
51 $c->res->output('Hello');
064834ea 52 $c->forward('foo');
5a8ed4fe 53 }
54
064834ea 55 sub product : Regex('^product[_]*(\d*).html$') {
5a8ed4fe 56 my ( $self, $c ) = @_;
57 $c->stash->{template} = 'product.tt';
58 $c->stash->{product} = $c->req->snippets->[0];
59 }
fc7ec1d9 60
3803e98f 61See also L<Catalyst::Manual::Intro>
62
fc7ec1d9 63=head1 DESCRIPTION
64
fc7ec1d9 65The key concept of Catalyst is DRY (Don't Repeat Yourself).
66
67See L<Catalyst::Manual> for more documentation.
68
23f9d934 69Catalyst plugins can be loaded by naming them as arguments to the "use Catalyst" statement.
1985c30b 70Omit the C<Catalyst::Plugin::> prefix from the plugin name,
23f9d934 71so C<Catalyst::Plugin::My::Module> becomes C<My::Module>.
fc7ec1d9 72
73 use Catalyst 'My::Module';
74
23f9d934 75Special flags like -Debug and -Engine can also be specifed as arguments when
76Catalyst is loaded:
fc7ec1d9 77
78 use Catalyst qw/-Debug My::Module/;
79
23f9d934 80The position of plugins and flags in the chain is important, because they are
81loaded in exactly the order that they appear.
fc7ec1d9 82
23f9d934 83The following flags are supported:
84
85=over 4
86
87=item -Debug
88
89enables debug output, i.e.:
fc7ec1d9 90
91 use Catalyst '-Debug';
92
23f9d934 93this is equivalent to:
fc7ec1d9 94
95 use Catalyst;
96 sub debug { 1 }
97
23f9d934 98=item -Engine
fc7ec1d9 99
100Force Catalyst to use a specific engine.
23f9d934 101Omit the C<Catalyst::Engine::> prefix of the engine name, i.e.:
fc7ec1d9 102
103 use Catalyst '-Engine=CGI';
104
23f9d934 105=back
fc7ec1d9 106
23f9d934 107=head1 METHODS
108
109=over 4
110
111=item debug
fc7ec1d9 112
113Overload to enable debug messages.
114
115=cut
116
117sub debug { 0 }
118
23f9d934 119=item config
fc7ec1d9 120
121Returns a hashref containing your applications settings.
122
123=cut
124
fc7ec1d9 125sub import {
0319a12c 126 my ( $class, @arguments ) = @_;
38cb5be3 127
fc7ec1d9 128 my $caller = caller(0);
38cb5be3 129
130 if ( $caller eq 'main' ) {
131 return;
132 }
fc7ec1d9 133
99fe1710 134 # Prepare inheritance
0319a12c 135 unless ( $caller->isa($class) ) {
fc7ec1d9 136 no strict 'refs';
0319a12c 137 push @{"$caller\::ISA"}, $class;
1c99e125 138 }
38cb5be3 139
d96e14c2 140 if ( $caller->engine ) {
38cb5be3 141 $caller->log->warn( qq/Attempt to re-initialize "$caller"/ );
0319a12c 142 return;
d96e14c2 143 }
144
0319a12c 145 # Process options
146 my $flags = { };
147
148 foreach (@arguments) {
149
150 if ( /^-Debug$/ ) {
c5c6deba 151 $flags->{log} = ( $flags->{log} ) ? 'debug,' . $flags->{log} : 'debug';
0319a12c 152 }
153 elsif (/^-(\w+)=?(.*)$/) {
154 $flags->{ lc $1 } = $2;
155 }
156 else {
157 push @{ $flags->{plugins} }, $_;
158 }
159 }
160
161 $caller->setup_log ( delete $flags->{log} );
162 $caller->setup_plugins ( delete $flags->{plugins} );
163 $caller->setup_dispatcher ( delete $flags->{dispatcher} );
164 $caller->setup_engine ( delete $flags->{engine} );
165 $caller->setup_home ( delete $flags->{home} );
166
167 for my $flag ( sort keys %{ $flags } ) {
168
169 if ( my $code = $caller->can( 'setup_' . $flag ) ) {
170 &$code( $caller, delete $flags->{$flag} );
171 }
172 else {
173 $caller->log->warn(qq/Unknown flag "$flag"/);
174 }
175 }
176
177 $caller->log->warn( "You are running an old helper script! "
178 . "Please update your scripts by regenerating the "
179 . "application and copying over the new scripts." )
180 if ( $ENV{CATALYST_SCRIPT_GEN}
181 && ( $ENV{CATALYST_SCRIPT_GEN} < $CATALYST_SCRIPT_GEN ) );
182
183
184 if ( $caller->debug ) {
185
186 my @plugins = ();
187
188 {
189 no strict 'refs';
190 @plugins = grep { /^Catalyst::Plugin/ } @{"$caller\::ISA"};
191 }
192
193 if ( @plugins ) {
194 my $t = Text::ASCIITable->new;
195 $t->setOptions( 'hide_HeadRow', 1 );
196 $t->setOptions( 'hide_HeadLine', 1 );
197 $t->setCols('Class');
198 $t->setColWidth( 'Class', 75, 1 );
199 $t->addRow($_) for @plugins;
f45789b1 200 $caller->log->debug( "Loaded plugins:\n" . $t->draw );
0319a12c 201 }
202
203 my $dispatcher = $caller->dispatcher;
204 my $engine = $caller->engine;
205 my $home = $caller->config->{home};
206
207 $caller->log->debug(qq/Loaded dispatcher "$dispatcher"/);
208 $caller->log->debug(qq/Loaded engine "$engine"/);
209
210 $home
211 ? ( -d $home )
212 ? $caller->log->debug(qq/Found home "$home"/)
213 : $caller->log->debug(qq/Home "$home" doesn't exist/)
214 : $caller->log->debug(q/Couldn't find home/);
fc7ec1d9 215 }
0319a12c 216}
217
218=item $c->engine
219
220Contains the engine class.
fc7ec1d9 221
0319a12c 222=item $c->log
223
224Contains the logging object. Unless it is already set Catalyst sets this up with a
225C<Catalyst::Log> object. To use your own log class:
226
227 $c->log( MyLogger->new );
228 $c->log->info("now logging with my own logger!");
229
230Your log class should implement the methods described in the C<Catalyst::Log>
231man page.
232
233=item $c->plugin( $name, $class, @args )
234
235Instant plugins for Catalyst.
236Classdata accessor/mutator will be created, class loaded and instantiated.
237
238 MyApp->plugin( 'prototype', 'HTML::Prototype' );
239
240 $c->prototype->define_javascript_functions;
241
242=cut
243
244sub plugin {
245 my ( $class, $name, $plugin, @args ) = @_;
246 $plugin->require;
247
248 if ( my $error = $UNIVERSAL::require::ERROR ) {
249 Catalyst::Exception->throw(
250 message => qq/Couldn't load instant plugin "$plugin", "$error"/
251 );
252 }
253
254 eval { $plugin->import };
255 $class->mk_classdata($name);
256 my $obj;
257 eval { $obj = $plugin->new(@args) };
258
259 if ( $@ ) {
260 Catalyst::Exception->throw(
261 message => qq/Couldn't instantiate instant plugin "$plugin", "$@"/
262 );
263 }
264
265 $class->$name($obj);
266 $class->log->debug(qq/Initialized instant plugin "$plugin" as "$name"/)
267 if $class->debug;
268}
269
23f9d934 270=back
271
d1a31ac6 272=head1 LIMITATIONS
273
b2b7d352 274mod_perl2 support is considered experimental and may contain bugs.
d1a31ac6 275
3cb1db8c 276=head1 SUPPORT
277
278IRC:
279
280 Join #catalyst on irc.perl.org.
281
282Mailing-Lists:
283
284 http://lists.rawmode.org/mailman/listinfo/catalyst
285 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
1985c30b 286
432d507d 287Web:
288
289 http://catalyst.perl.org
290
fc7ec1d9 291=head1 SEE ALSO
292
61b1e958 293=over 4
294
295=item L<Catalyst::Manual> - The Catalyst Manual
296
297=item L<Catalyst::Engine> - Core Engine
298
299=item L<Catalyst::Log> - The Log Class.
300
301=item L<Catalyst::Request> - The Request Object
302
303=item L<Catalyst::Response> - The Response Object
304
305=item L<Catalyst::Test> - The test suite.
306
307=back
fc7ec1d9 308
309=head1 AUTHOR
310
311Sebastian Riedel, C<sri@oook.de>
312
313=head1 THANK YOU
314
84cf74e7 315Andy Grundman, Andrew Ford, Andrew Ruthven, Autrijus Tang, Christian Hansen,
ce2b098c 316Christopher Hicks, Dan Sully, Danijel Milicevic, David Naughton,
75aeff23 317Gary Ashton Jones, Geoff Richards, Jesse Sheidlower, Jody Belka,
9cee9588 318Johan Lindstrom, Juan Camacho, Leon Brocard, Marcus Ramberg,
319Tatsuhiko Miyagawa and all the others who've helped.
fc7ec1d9 320
321=head1 LICENSE
322
323This library is free software . You can redistribute it and/or modify it under
324the same terms as perl itself.
325
326=cut
327
3281;