X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst.pm;h=42cbe8f4a9e4a2027fd40057f8755149efdde502;hp=5f47a656353f071977e4025da84a9304664b571c;hb=22247e54ccd3fbdaf286e77304b96e61ecc43a0f;hpb=d3e7a648cc29a8b45ddcd4ef731e3f9b0b4f919c diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 5f47a65..42cbe8f 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -10,12 +10,15 @@ use Catalyst::Request; use Catalyst::Request::Upload; use Catalyst::Response; use Catalyst::Utils; +use File::stat; use NEXT; use Text::SimpleTable; 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; __PACKAGE__->mk_accessors( @@ -43,7 +46,7 @@ our $DETACH = "catalyst_detach\n"; require Module::Pluggable::Fast; # Helper script generation -our $CATALYST_SCRIPT_GEN = 19; +our $CATALYST_SCRIPT_GEN = 25; __PACKAGE__->mk_classdata($_) for qw/components arguments dispatcher engine log dispatcher_class @@ -54,7 +57,7 @@ __PACKAGE__->engine_class('Catalyst::Engine::CGI'); __PACKAGE__->request_class('Catalyst::Request'); __PACKAGE__->response_class('Catalyst::Response'); -our $VERSION = '5.57'; +our $VERSION = '5.62'; sub import { my ( $class, @arguments ) = @_; @@ -363,13 +366,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 } + } } } @@ -388,7 +401,7 @@ Gets a L instance by name. sub controller { my ( $c, $name ) = @_; my $controller = $c->comp("Controller::$name"); - return $controller if $controller; + return $controller if defined $controller; return $c->comp("C::$name"); } @@ -403,7 +416,7 @@ Gets a L instance by name. sub model { my ( $c, $name ) = @_; my $model = $c->comp("Model::$name"); - return $model if $model; + return $model if defined $model; return $c->comp("M::$name"); } @@ -418,7 +431,7 @@ Gets a L instance by name. sub view { my ( $c, $name ) = @_; my $view = $c->comp("View::$name"); - return $view if $view; + return $view if defined $view; return $c->comp("V::$name"); } @@ -591,7 +604,9 @@ EOF { no strict 'refs'; - @plugins = grep { /^Catalyst::Plugin/ } @{"$class\::ISA"}; + @plugins = + map { $_ . ' ' . ( $_->VERSION || '' ) } + grep { /^Catalyst::Plugin/ } @{"$class\::ISA"}; } if (@plugins) { @@ -781,7 +796,7 @@ sub welcome_message {

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.

+ never expected it to be: Fun, rewarding, and quick.

What to do now?

That really depends on what you want to do. We do, however, provide you with a few starting points.

@@ -792,12 +807,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.

@@ -887,14 +902,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"}++; @@ -906,8 +915,56 @@ sub execute { return $c->state; } + # 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; + } + my $callsub = ( caller($callsub_index) )[3]; + $action = "-> $action" if $callsub =~ /forward$/; + + 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; @@ -919,18 +976,35 @@ 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 ) { chomp $error; - $error = qq/Caught exception "$error"/; + my $class = $last->class; + my $name = $last->name; + $error = qq/Caught exception in $class->$name "$error"/; } $c->error($error); $c->state(0); @@ -1015,7 +1089,19 @@ sub finalize_headers { # Content-Length if ( $c->response->body && !$c->response->content_length ) { - $c->response->content_length( bytes::length( $c->response->body ) ); + + # get the length from a filehandle + if ( ref $c->response->body && $c->response->body->can('read') ) { + if ( my $stat = stat $c->response->body ) { + $c->response->content_length( $stat->size ); + } + else { + $c->log->warn('Serving filehandle without a content-length'); + } + } + else { + $c->response->content_length( bytes::length( $c->response->body ) ); + } } # Errors @@ -1081,11 +1167,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; }; @@ -1099,7 +1185,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 ); } @@ -1442,7 +1536,7 @@ sub setup_components { my $instance; - eval { $instance = $component->new( $context, $config ); }; + eval { $instance = $component->COMPONENT( $context, $config ); }; if ( my $error = $@ ) { @@ -1541,7 +1635,7 @@ sub setup_engine { $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' }; } - if ( !$engine && $ENV{MOD_PERL} ) { + if ( $ENV{MOD_PERL} ) { # create the apache method { @@ -1557,21 +1651,25 @@ sub setup_engine { if ( $software eq 'mod_perl' ) { - if ( $version >= 1.99922 ) { - $engine = 'Catalyst::Engine::Apache2::MP20'; - } + if ( !$engine ) { - elsif ( $version >= 1.9901 ) { - $engine = 'Catalyst::Engine::Apache2::MP19'; - } + if ( $version >= 1.99922 ) { + $engine = 'Catalyst::Engine::Apache2::MP20'; + } - elsif ( $version >= 1.24 ) { - $engine = 'Catalyst::Engine::Apache::MP13'; - } + 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}/ ); + } - else { - Catalyst::Exception->throw( message => - qq/Unsupported mod_perl version: $ENV{MOD_PERL}/ ); } # install the correct mod_perl handler @@ -1845,6 +1943,8 @@ Wiki: =head1 SEE ALSO +=head2 L - All you need to start with Catalyst + =head2 L - The Catalyst Manual =head2 L, L - Base classes for components