X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst.pm;h=59b30c29a6a0f9893471da78a00b3d0683be662f;hb=6a0648863b2a3a4a745dc701d422b954cadc05b0;hp=7310d71be1aecca3bd4e6597b633f84318176b73;hpb=9b0a3e0fc295d2d2b0af184f4ed1d05db41fc2c4;p=catagits%2FCatalyst-Runtime.git
diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm
index 7310d71..59b30c2 100644
--- a/lib/Catalyst.pm
+++ b/lib/Catalyst.pm
@@ -1,7 +1,7 @@
package Catalyst;
use strict;
-use base 'Catalyst::Base';
+use base 'Catalyst::Component';
use bytes;
use UNIVERSAL::require;
use Catalyst::Exception;
@@ -10,6 +10,7 @@ use Catalyst::Request;
use Catalyst::Request::Upload;
use Catalyst::Response;
use Catalyst::Utils;
+use Catalyst::Controller;
use File::stat;
use NEXT;
use Text::SimpleTable;
@@ -17,7 +18,10 @@ use Path::Class;
use Time::HiRes qw/gettimeofday tv_interval/;
use URI;
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/
@@ -68,7 +72,7 @@ sub import {
unless ( $caller->isa('Catalyst') ) {
no strict 'refs';
- push @{"$caller\::ISA"}, $class;
+ push @{"$caller\::ISA"}, $class, 'Catalyst::Controller';
}
$caller->arguments( [@arguments] );
@@ -364,13 +368,23 @@ sub component {
if ( exists $c->components->{$try} ) {
- return $c->components->{$try};
+ my $comp = $c->components->{$try};
+ if ( ref $comp && $comp->can('ACCEPT_CONTEXT') ) {
+ return $comp->ACCEPT_CONTEXT($c);
+ }
+ else { return $comp }
}
}
foreach my $component ( keys %{ $c->components } ) {
-
- return $c->components->{$component} if $component =~ /$name/i;
+ my $comp;
+ $comp = $c->components->{$component} if $component =~ /$name/i;
+ if ($comp) {
+ if ( ref $comp && $comp->can('ACCEPT_CONTEXT') ) {
+ return $comp->ACCEPT_CONTEXT($c);
+ }
+ else { return $comp }
+ }
}
}
@@ -431,6 +445,12 @@ Returns or takes a hashref containing the application's configuration.
__PACKAGE__->config({ db => 'dsn:SQLite:foo.db' });
+You can also use a L Welcome to the wonderful world of Catalyst.
This MVC
framework will make web development something you had
- never expected it to be: Fun, rewarding and quick.
That really depends on what you want to do. We do, however, provide you with a few starting points.
@@ -795,12 +825,12 @@ perldoc controllers, - models and - views, + models, and + views; they can save you a lot of work.script/${prefix}_create.pl -help
Also, be sure to check out the vast and growing - collection of plugins for Catalyst on CPAN, + collection of plugins for Catalyst on CPAN; you are likely to find what you need there.
@@ -890,14 +920,8 @@ sub execute { $class = $c->components->{$class} || $class; $c->state(0); - my $callsub = - ( caller(0) )[0]->isa('Catalyst::Action') - ? ( caller(2) )[3] - : ( caller(1) )[3]; - - my $action = ''; if ( $c->debug ) { - $action = "$code"; + my $action = "$code"; $action = "/$action" unless $action =~ /\-\>/; $c->counter->{"$code"}++; @@ -909,8 +933,61 @@ sub execute { return $c->state; } - $action = "-> $action" if $callsub =~ /forward$/; + # determine if the call was the result of a forward + # 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 $node = Tree::Simple->new( + { + action => $action, + elapsed => undef, # to be filled in later + } + ); + $node->setUID( "$code" . $c->counter->{"$code"} ); + + unless ( ( $code->name =~ /^_.*/ ) + && ( !$c->config->{show_internal_actions} ) ) + { + + # is this a root-level call or a forwarded call? + if ( $callsub =~ /forward$/ ) { + + # forward, locate the caller + if ( my $parent = $c->stack->[-1] ) { + my $visitor = Tree::Simple::Visitor::FindByUID->new; + $visitor->searchForUID( + "$parent" . $c->counter->{"$parent"} ); + $c->{stats}->accept($visitor); + if ( my $result = $visitor->getResult ) { + $result->addChild($node); + } + } + else { + + # forward with no caller may come from a plugin + $c->{stats}->addChild($node); + } + } + else { + + # root-level call + $c->{stats}->addChild($node); + } + } } + push( @{ $c->stack }, $code ); my $elapsed = 0; my $start = 0; @@ -922,14 +999,28 @@ sub execute { unless ( ( $code->name =~ /^_.*/ ) && ( !$c->config->{show_internal_actions} ) ) { - push @{ $c->{stats} }, [ $action, sprintf( '%fs', $elapsed ) ]; + + # FindByUID uses an internal die, so we save the existing error + my $error = $@; + + # locate the node in the tree and update the elapsed time + my $visitor = Tree::Simple::Visitor::FindByUID->new; + $visitor->searchForUID( "$code" . $c->counter->{"$code"} ); + $c->{stats}->accept($visitor); + if ( my $result = $visitor->getResult ) { + my $value = $result->getNodeValue; + $value->{elapsed} = sprintf( '%fs', $elapsed ); + $result->setNodeValue($value); + } + + # restore error + $@ = $error || undef; } } my $last = ${ $c->stack }[-1]; pop( @{ $c->stack } ); if ( my $error = $@ ) { - if ( $error eq $DETACH ) { die $DETACH if $c->depth > 1 } else { unless ( ref $error ) { @@ -1099,11 +1190,11 @@ sub handle_request { # Always expect worst case! my $status = -1; eval { - my @stats = (); + my $stats = ( $class->debug ) ? Tree::Simple->new: q{}; my $handler = sub { my $c = $class->prepare(@arguments); - $c->{stats} = \@stats; + $c->{stats} = $stats; $c->dispatch; return $c->finalize; }; @@ -1117,7 +1208,15 @@ sub handle_request { ( $elapsed == 0 ? '??' : ( 1 / $elapsed ) ); my $t = Text::SimpleTable->new( [ 64, 'Action' ], [ 9, 'Time' ] ); - for my $stat (@stats) { $t->row( $stat->[0], $stat->[1] ) } + $stats->traverse( + sub { + my $action = shift; + my $stat = $action->getNodeValue; + $t->row( ( q{ } x $action->getDepth ) . $stat->{action}, + $stat->{elapsed} || '??' ); + } + ); + $class->log->info( "Request took ${elapsed}s ($av/s)\n" . $t->draw ); } @@ -1451,7 +1550,7 @@ sub setup_components { my $callback = sub { my ( $component, $context ) = @_; - unless ( $component->isa('Catalyst::Component') ) { + unless ( $component->can('COMPONENT') ) { return $component; } @@ -1460,7 +1559,7 @@ sub setup_components { my $instance; - eval { $instance = $component->new( $context, $config ); }; + eval { $instance = $component->COMPONENT( $context, $config ); }; if ( my $error = $@ ) { @@ -1576,19 +1675,19 @@ sub setup_engine { if ( $software eq 'mod_perl' ) { if ( !$engine ) { - + if ( $version >= 1.99922 ) { $engine = 'Catalyst::Engine::Apache2::MP20'; } - + elsif ( $version >= 1.9901 ) { $engine = 'Catalyst::Engine::Apache2::MP19'; } - + elsif ( $version >= 1.24 ) { $engine = 'Catalyst::Engine::Apache::MP13'; } - + else { Catalyst::Exception->throw( message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/ );