X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst.pm;h=d93707fcd26f0c6c52b191687f125f150d1fedb0;hp=0226ba12ef9baae85e38bfac7362f9ff888bdc3d;hb=8327e2e21fc9ea72c357e876f868001ad9712474;hpb=313751843f67518acde027acf604f327c1679420 diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 0226ba1..d93707f 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -21,7 +21,6 @@ use Scalar::Util qw/weaken/; use Tree::Simple qw/use_weak_refs/; use Tree::Simple::Visitor::FindByUID; use attributes; -use YAML (); __PACKAGE__->mk_accessors( qw/counter request response state action stack namespace/ @@ -48,18 +47,18 @@ our $DETACH = "catalyst_detach\n"; require Module::Pluggable::Fast; # Helper script generation -our $CATALYST_SCRIPT_GEN = 25; +our $CATALYST_SCRIPT_GEN = 27; __PACKAGE__->mk_classdata($_) for qw/components arguments dispatcher engine log dispatcher_class - engine_class context_class request_class response_class/; + engine_class context_class request_class response_class setup_finished/; __PACKAGE__->dispatcher_class('Catalyst::Dispatcher'); __PACKAGE__->engine_class('Catalyst::Engine::CGI'); __PACKAGE__->request_class('Catalyst::Request'); __PACKAGE__->response_class('Catalyst::Response'); -our $VERSION = '5.62'; +our $VERSION = '5.64'; sub import { my ( $class, @arguments ) = @_; @@ -246,7 +245,10 @@ in an arrayref. The action will receive the arguments in C<@_> and C<$c-Ereq-Eargs>. Upon returning from the function, C<$c-Ereq-Eargs> will be restored to the previous values. - $c->forward('/foo'); +Any data Ced from the action forwarded to, will be returned by the +call to forward. + + my $foodata = $c->forward('/foo'); $c->forward('index'); $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/); $c->forward('MyApp::View::TT'); @@ -369,7 +371,7 @@ sub component { if ( exists $c->components->{$try} ) { my $comp = $c->components->{$try}; - if ( ref $comp && $comp->can('ACCEPT_CONTEXT') ) { + if ( eval { $comp->can('ACCEPT_CONTEXT'); } ) { return $comp->ACCEPT_CONTEXT($c); } else { return $comp } @@ -443,7 +445,7 @@ sub view { Returns or takes a hashref containing the application's configuration. - __PACKAGE__->config({ db => 'dsn:SQLite:foo.db' }); + __PACKAGE__->config( { db => 'dsn:SQLite:foo.db' } ); You can also use a L config file like myapp.yml in your applications home directory. @@ -451,6 +453,17 @@ applications home directory. --- db: dsn:SQLite:foo.db +=cut + +sub config { + my $c = shift; + + $c->log->warn("Setting config after setup has been run is not a good idea.") + if ( @_ and $c->setup_finished ); + + $c->NEXT::config(@_); +} + =head2 $c->debug Overload to enable debug messages (same as -Debug option). @@ -471,10 +484,16 @@ L. =head2 $c->log -Returns the logging object instance. Unless it is already set, Catalyst -sets this up with a L object. To use your own log class: +Returns the logging object instance. Unless it is already set, Catalyst sets +this up with a L object. To use your own log class, set the +logger with the C<< __PACKAGE__->log >> method prior to calling +C<< __PACKAGE__->setup >>. + + __PACKAGE__->log( MyLogger->new ); + __PACKAGE__->setup; + +And later: - $c->log( MyLogger->new ); $c->log->info( 'Now logging with my own logger!' ); Your log class should implement the methods described in the @@ -581,15 +600,6 @@ sub setup { $class->setup_home( delete $flags->{home} ); - # YAML config support - my $confpath = $class->config->{file} - || $class->path_to( - ( Catalyst::Utils::appprefix( ref $class || $class ) . '.yml' ) ); - my $conf = {}; - $conf = YAML::LoadFile($confpath) if -f $confpath; - my $oldconf = $class->config; - $class->config( { %$oldconf, %$conf } ); - $class->setup_log( delete $flags->{log} ); $class->setup_plugins( delete $flags->{plugins} ); $class->setup_dispatcher( delete $flags->{dispatcher} ); @@ -679,6 +689,8 @@ EOF $class->log->info("$name powered by Catalyst $Catalyst::VERSION"); } $class->log->_flush() if $class->log->can('_flush'); + + $class->setup_finished(1); } =head2 $c->uri_for( $path, [ @args ] ) @@ -705,12 +717,15 @@ sub uri_for { $namespace = '' if $path =~ /^\//; $path =~ s/^\///; + my $params = (scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {}); + # join args with '/', or a blank string my $args = ( scalar @args ? '/' . join( '/', @args ) : '' ); $args =~ s/^\/// unless $path; my $res = URI->new_abs( URI->new_abs( "$path$args", "$basepath$namespace" ), $base ) ->canonical; + $res->query_form(%$params); $res; } @@ -821,6 +836,7 @@ sub welcome_message {

If you want to jump right into web development with Catalyst you might want to check out the documentation.

perldoc Catalyst::Manual::Intro
+perldoc Catalyst::Manual::Tutorial
 perldoc Catalyst::Manual

What to do next?

Next it's time to write an actual application. Use the @@ -917,7 +933,7 @@ via $c->error. sub execute { my ( $c, $class, $code ) = @_; - $class = $c->components->{$class} || $class; + $class = $c->component($class) || $class; $c->state(0); if ( $c->debug ) { @@ -934,15 +950,20 @@ sub execute { } # determine if the call was the result of a forward - my $callsub_index = ( caller(0) )[0]->isa('Catalyst::Action') ? 2 : 1; - if ( ( caller($callsub_index) )[3] =~ /^NEXT/ ) { - - # work around NEXT if execute was extended by a plugin - $callsub_index += 3; + # this is done by walking up the call stack and looking for a calling + # sub of Catalyst::forward before the eval + my $callsub = q{}; + for my $index ( 1 .. 10 ) { + last + if ( ( caller($index) )[0] eq 'Catalyst' + && ( caller($index) )[3] eq '(eval)' ); + + if ( ( caller($index) )[3] =~ /forward$/ ) { + $callsub = ( caller($index) )[3]; + $action = "-> $action"; + last; + } } - my $callsub = ( caller($callsub_index) )[3]; - - $action = "-> $action" if $callsub =~ /forward$/; my $node = Tree::Simple->new( { @@ -1565,7 +1586,7 @@ sub setup_components { } Catalyst::Exception->throw( message => -qq/Couldn't instantiate component "$component", "new() didn't return a object"/ +qq/Couldn't instantiate component "$component", "COMPONENT() didn't return a object"/ ) unless ref $instance; return $instance;