X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=8e0b5130df39dfb0523b5e62c85b0ce0a42c19b5;hb=e7c0c583d6e12e822dd26bf1282eb610c9a59351;hp=63dbee96ab47d5a04224ecfa40694e88acf160f9;hpb=1c470b06b8c6fec5226397ea2bc017ad5021fd38;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 63dbee9..8e0b513 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -116,20 +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}; - last unless $c->state; + return if scalar @{ $c->error }; } - if ( my $action = @{ $c->get_action( $action, $default ) }[-1] ) { - $c->execute( @{ $action->[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; } - for my $end ( reverse @{ $c->get_action( 'end', $namespace ) } ) { + + # 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] } ); + } + } + + # Execute last end + if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) { $c->execute( @{ $end->[0] } ); - return if scalar @{$c->error}; - last unless $c->state; + return if scalar @{ $c->error }; } } else { @@ -191,8 +203,12 @@ 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); $c->state(0); @@ -214,7 +230,7 @@ sub finalize { if ( my $location = $c->response->redirect ) { $c->log->debug(qq/Redirecting to "$location"/) if $c->debug; $c->response->header( Location => $location ); - $c->response->status(302) if $c->response->status !~ /3\d\d$/; + $c->response->status(302) if $c->response->status !~ /^3\d\d$/; } if ( $#{ $c->error } >= 0 ) { @@ -424,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; } @@ -442,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; } @@ -778,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} ) {