From: Matt S Trout Date: Tue, 1 Nov 2005 03:48:52 +0000 (+0000) Subject: - Refactored get_action into get_action and get_actions X-Git-Tag: 5.7099_04~1067 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=a9dc674c99f36ff40d94b80753a1504074ba5e22 - Refactored get_action into get_action and get_actions --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index ef0a506..f1f2ebe 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -919,7 +919,7 @@ Finalize uploads. Cleans up any temporary files. sub finalize_uploads { my $c = shift; $c->engine->finalize_uploads( $c, @_ ) } -=item $c->get_action( $action, $namespace, $inherit ) +=item $c->get_action( $action, $namespace ) Get an action in a given namespace. @@ -927,6 +927,14 @@ Get an action in a given namespace. sub get_action { my $c = shift; $c->dispatcher->get_action( $c, @_ ) } +=item $c->get_actions( $action, $namespace ) + +Get all actions of a given name in a namespace and all base namespaces. + +=cut + +sub get_actions { my $c = shift; $c->dispatcher->get_actions( $c, @_ ) } + =item handle_request( $class, @arguments ) Handles the request. diff --git a/lib/Catalyst/Base.pm b/lib/Catalyst/Base.pm index cbd95ae..0703b5c 100644 --- a/lib/Catalyst/Base.pm +++ b/lib/Catalyst/Base.pm @@ -22,17 +22,17 @@ sub _DISPATCH : Private { sub _BEGIN : Private { my ( $self, $c ) = @_; - my $begin = @{ $c->get_action( 'begin', $c->namespace, 1 ) }[-1]; + my $begin = ($c->get_actions( 'begin', $c->namespace))[-1]; return 1 unless $begin; - $begin->[0]->execute($c); + $begin->execute($c); return !@{ $c->error }; } sub _AUTO : Private { my ( $self, $c ) = @_; - my @auto = @{ $c->get_action( 'auto', $c->namespace, 1 ) }; + my @auto = $c->get_actions('auto', $c->namespace); foreach my $auto (@auto) { - $auto->[0]->execute($c); + $auto->execute($c); return 0 unless $c->state; } return 1; @@ -46,9 +46,9 @@ sub _ACTION : Private { sub _END : Private { my ( $self, $c ) = @_; - my $end = @{ $c->get_action( 'end', $c->namespace, 1 ) }[-1]; + my $end = ($c->get_actions( 'end', $c->namespace))[-1]; return 1 unless $end; - $end->[0]->execute($c); + $end->execute($c); return !@{ $c->error }; } diff --git a/lib/Catalyst/DispatchType/Default.pm b/lib/Catalyst/DispatchType/Default.pm index 08a4948..dba3951 100644 --- a/lib/Catalyst/DispatchType/Default.pm +++ b/lib/Catalyst/DispatchType/Default.pm @@ -24,12 +24,12 @@ See L. sub match { my ( $self, $c, $path ) = @_; return if $path =~ m!/!; # Not at root yet, wait for it ... - my $result = @{ $c->get_action( 'default', $c->req->path, 1 ) || [] }[-1]; + my $result = ($c->get_actions('default', $c->req->path))[-1]; # Find default on namespace or super if ($result) { - $c->action( $result->[0] ); - $c->namespace( $result->[0]->namespace ); + $c->action( $result ); + $c->namespace( $result->namespace ); $c->req->action('default'); # default methods receive the controller name as the first argument unshift @{ $c->req->args }, $path; diff --git a/lib/Catalyst/DispatchType/Index.pm b/lib/Catalyst/DispatchType/Index.pm index 95ed8b5..59026cf 100644 --- a/lib/Catalyst/DispatchType/Index.pm +++ b/lib/Catalyst/DispatchType/Index.pm @@ -24,12 +24,11 @@ See L. sub match { my ( $self, $c, $path ) = @_; return if @{ $c->req->args }; - my $result = @{ $c->get_action( 'index', $path ) || [] }[-1]; + my $result = $c->get_action( 'index', $path ); - # Find default on namespace or super if ($result) { - $c->action( $result->[0] ); - $c->namespace( $result->[0]->namespace ); + $c->action( $result ); + $c->namespace( $result->namespace ); $c->req->action('index'); $c->req->match( $c->req->path ); return 1; diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 5def763..cd56ad4 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -88,7 +88,7 @@ sub forward { my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args; - my $results = []; + my $result; my $command_copy = $command; @@ -100,14 +100,14 @@ sub forward { } unless ( $command_copy =~ /\// ) { - $results = $c->get_action( $command_copy, '/' ); + $result = $c->get_action( $command_copy, '/' ); } else { my @extra_args; DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) { my $tail = $2; - $results = $c->get_action( $tail, $1 ); - if ( @{$results} ) { + $result = $c->get_action( $tail, $1 ); + if ( $result ) { $command = $tail; push( @{$arguments}, @extra_args ); last DESCEND; @@ -116,7 +116,7 @@ sub forward { } } - unless ( @{$results} ) { + unless ( $result ) { unless ( $c->components->{$command} ) { my $error = @@ -139,7 +139,7 @@ qq/Couldn't forward to command "$command". Invalid action or component./; namespace => $class, } ); - $results = [ [$action] ]; + $result = $action; } else { @@ -155,11 +155,7 @@ qq/Couldn't forward to command "$command". Invalid action or component./; local $c->request->{arguments} = [ @{$arguments} ]; - for my $result ( @{$results} ) { - $result->[0]->execute($c); - return if scalar @{ $c->error }; - last unless $c->state; - } + $result->execute($c); return $c->state; } @@ -197,41 +193,46 @@ sub prepare_action { if ( $c->debug && @args ); } -=item $self->get_action( $c, $action, $namespace, $inherit ) +=item $self->get_action( $c, $action, $namespace ) =cut sub get_action { - my ( $self, $c, $action, $namespace, $inherit ) = @_; + my ( $self, $c, $action, $namespace ) = @_; return [] unless $action; $namespace ||= ''; $namespace = '' if $namespace eq '/'; - $inherit ||= 0; my @match = $self->get_containers($namespace); - if ($inherit) { # Return [ [ $act_obj ], ... ] for valid containers - return [ - map { [ $_->{$action} ] } # Make [ $action_obj ] - grep { defined $_->{$action} } # If it exists in the container - map { $_->actions } # Get action hash for container - @match - ]; - } - else { - my $node = $match[-1]->actions; # Only bother looking at the last one + my $node = $match[-1]->actions; # Only bother looking at the last one - if ( defined $node->{$action} - && ( $node->{$action}->namespace eq $namespace ) ) - { - return [ [ $node->{$action} ] ]; - } - else { - return []; - } + if ( defined $node->{$action} + && ( $node->{$action}->namespace eq $namespace ) ) + { + return $node->{$action}; } } +=item $self->get_actions( $c, $action, $namespace ) + +=cut + +sub get_actions { + my ( $self, $c, $action, $namespace ) = @_; + return [] unless $action; + $namespace ||= ''; + $namespace = '' if $namespace eq '/'; + + my @match = $self->get_containers($namespace); + + return + map { $_->{$action} } + grep { defined $_->{$action} } # If it exists in the container + map { $_->actions } # Get action hash for container + @match +} + =item $self->get_containers( $namespace ) =cut