X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=84b7ab09ffe8caed07d8e59077c6dca14c5021f4;hb=597d4987a6929a19b18743ba111a6b55f2b3b496;hp=41a5049c96928f55cd1c7aa815386c2dce7c917f;hpb=a135d186f7acefbbab74e8e6d9a43ccd3560d326;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 41a5049..84b7ab0 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -116,17 +116,32 @@ sub dispatch { my $results = $c->get_action( $action, $default ); $namespace ||= '/'; if ( @{$results} ) { - for my $begin ( @{ $c->get_action( 'begin', $namespace ) } ) { + + # Execute last begin + $c->state(1); + if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) { $c->execute( @{ $begin->[0] } ); + return if scalar @{ $c->error }; + } + + # Execute the auto chain + for my $auto ( @{ $c->get_action( 'auto', $namespace ) } ) { + $c->execute( @{ $auto->[0] } ); + return if scalar @{ $c->error }; + last unless $c->state; } - if ( my $action = $c->req->action ) { - for my $result ( @{ $c->get_action( $action, $default ) }[-1] ) { + + # Execute the action or last default + if ( ( my $action = $c->req->action ) && $c->state ) { + if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) { $c->execute( @{ $result->[0] } ); - last unless $default; } } - for my $end ( reverse @{ $c->get_action( 'end', $namespace ) } ) { + + # Execute last end + if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) { $c->execute( @{ $end->[0] } ); + return if scalar @{ $c->error }; } } else { @@ -188,10 +203,14 @@ sub execute { else { $c->state( &$code( $class, $c, @{ $c->req->args } ) ) } }; if ( my $error = $@ ) { - chomp $error; - $error = qq/Caught exception "$error"/; + + unless ( ref $error ) { + chomp $error; + $error = qq/Caught exception "$error"/; + } + $c->log->error($error); - $c->error($error) if $c->debug; + $c->error($error); $c->state(0); } return $c->state; @@ -421,6 +440,8 @@ sub forward { } for my $result ( @{$results} ) { $c->execute( @{ $result->[0] } ); + return if scalar @{ $c->error }; + last unless $c->state; } return $c->state; } @@ -439,17 +460,37 @@ sub get_action { $namespace = '' if $namespace eq '/'; my $parent = $c->tree; my @results; - my $result = $c->actions->{private}->{ $parent->getUID }->{$action}; - push @results, [$result] if $result; - my $visitor = Tree::Simple::Visitor::FindByPath->new; - for my $part ( split '/', $namespace ) { - $visitor->setSearchPath($part); - $parent->accept($visitor); - my $child = $visitor->getResult; - my $uid = $child->getUID if $child; - my $match = $c->actions->{private}->{$uid}->{$action} if $uid; - push @results, [$match] if $match; - $parent = $child if $child; + my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 ); + if ( $allowed{$action} ) { + my $result = $c->actions->{private}->{ $parent->getUID }->{$action}; + push @results, [$result] if $result; + my $visitor = Tree::Simple::Visitor::FindByPath->new; + for my $part ( split '/', $namespace ) { + $visitor->setSearchPath($part); + $parent->accept($visitor); + my $child = $visitor->getResult; + my $uid = $child->getUID if $child; + my $match = $c->actions->{private}->{$uid}->{$action} if $uid; + push @results, [$match] if $match; + $parent = $child if $child; + } + } + else { + if ($namespace) { + my $visitor = Tree::Simple::Visitor::FindByPath->new; + $visitor->setSearchPath( split '/', $namespace ); + $parent->accept($visitor); + my $child = $visitor->getResult; + my $uid = $child->getUID if $child; + my $match = $c->actions->{private}->{$uid}->{$action} + if $uid; + push @results, [$match] if $match; + } + else { + my $result = + $c->actions->{private}->{ $parent->getUID }->{$action}; + push @results, [$result] if $result; + } } return \@results; } @@ -775,7 +816,7 @@ sub set_action { $absolute = 1; } $absolute = 1 if $flags{global}; - my $name = $absolute ? $path : "$prefix/$path"; + my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path; $c->actions->{plain}->{$name} = [ $namespace, $code ]; } if ( my $regex = $flags{regex} ) {