| 1 | package Catalyst::ActionChain; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 102 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 102 | extends qw(Catalyst::Action); | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 102 | has chain => (is => 'rw'); | |||||||||||||||||||||||||||
| 7 | 102 | has _current_chain_actions => (is=>'rw', init_arg=>undef, predicate=>'_has_current_chain_actions'); | |||||||||||||||||||||||||||
| 8 | 102 | has _chain_last_action => (is=>'rw', init_arg=>undef, predicate=>'_has_chain_last_action', clearer=>'_clear_chain_last_action'); | |||||||||||||||||||||||||||
| 9 | 102 | has _chain_captures => (is=>'rw', init_arg=>undef); | |||||||||||||||||||||||||||
| 10 | 102 | has _chain_original_args => (is=>'rw', init_arg=>undef, clearer=>'_clear_chain_original_args'); | |||||||||||||||||||||||||||
| 11 | 102 | has _chain_next_args => (is=>'rw', init_arg=>undef, predicate=>'_has_chain_next_args', clearer=>'_clear_chain_next_args'); | |||||||||||||||||||||||||||
| 12 | 102 | has _context => (is => 'rw', weak_ref => 1); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 102 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Catalyst::ActionChain - Chain of Catalyst Actions | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | See L<Catalyst::Manual::Intro> for more info about Chained actions. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | This class represents a chain of Catalyst Actions. It behaves exactly like | ||||||||||||||||||||||||||||
| 27 | the action at the *end* of the chain except on dispatch it will execute all | ||||||||||||||||||||||||||||
| 28 | the actions in the chain in order. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 247 | ▶ | sub dispatch { | ||||||||||||||||||||||||||
|
Subroutine
dispatch: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 247 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 34 | 247 | ▶ | my @captures = @{$c->req->captures||[]}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$c->req->captures || []
MC/DC0%0/1
$c->req->captures || []
$c->req->captures
Truth table50%1/2
A
A= $c->req->captures
| |||||||||||||||||||||||||||||
| 35 | 247 | my @chain = @{ $self->chain }; | |||||||||||||||||||||||||||
| 36 | 247 | my $last = pop(@chain); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 247 | $self->_current_chain_actions(\@chain); | |||||||||||||||||||||||||||
| 39 | 247 | $self->_chain_last_action($last); | |||||||||||||||||||||||||||
| 40 | 247 | $self->_chain_captures(\@captures); | |||||||||||||||||||||||||||
| 41 | 247 | $self->_chain_original_args($c->request->{arguments}); | |||||||||||||||||||||||||||
| 42 | 247 | $self->_context($c); | |||||||||||||||||||||||||||
| 43 | 247 | $self->_dispatch_chain_actions($c); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 7 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=4 SCAR=15.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 7 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 48 | 7 | my $ctx = $self->_context; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 7 | ▶ | if($self->_has_chain_last_action) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 7 | ▶ | @args ? $self->_chain_next_args(\@args) : $self->_chain_next_args([]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 7 | $self->_dispatch_chain_actions($ctx); | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $ctx->action->chain->[-1]->next($ctx, @args) if $ctx->action->chain->[-1]->can('next'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | return $ctx->state; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 254 | ▶ | sub _dispatch_chain_actions { | ||||||||||||||||||||||||||
|
Subroutine
_dispatch_chain_actions: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 62 | 254 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 63 | 254 | ▶ | while( @{$self->_current_chain_actions||[]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->_current_chain_actions || []
MC/DC0%0/1
$self->_current_chain_actions || []
$self->_current_chain_actions
Truth table50%1/2
A
A= $self->_current_chain_actions
| |||||||||||||||||||||||||||||
| 64 | 299 | $self->_dispatch_chain_action($c); | |||||||||||||||||||||||||||
| 65 | 298 | ▶ | return if $self->_abort_needed($c); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 249 | ▶ | if($self->_has_chain_last_action) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 242 | $c->request->{arguments} = $self->_chain_original_args; | |||||||||||||||||||||||||||
| 69 | 242 | $self->_clear_chain_original_args; | |||||||||||||||||||||||||||
| 70 | 242 | ▶ | unshift @{$c->request->{arguments}}, @{ $self->_chain_next_args} if $self->_has_chain_next_args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 242 | $self->_clear_chain_next_args; | |||||||||||||||||||||||||||
| 72 | 242 | my $last_action = $self->_chain_last_action; | |||||||||||||||||||||||||||
| 73 | 242 | $self->_clear_chain_last_action; | |||||||||||||||||||||||||||
| 74 | 242 | $last_action->dispatch($c); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 299 | ▶ | sub _dispatch_chain_action { | ||||||||||||||||||||||||||
|
Subroutine
_dispatch_chain_action: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 79 | 299 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 80 | 299 | ▶ | my ($action, @remaining_actions) = @{ $self->_current_chain_actions||[] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_current_chain_actions || []
MC/DC0%0/1
$self->_current_chain_actions || []
$self->_current_chain_actions
Truth table50%1/2
A
A= $self->_current_chain_actions
| |||||||||||||||||||||||||||||
| 81 | 299 | $self->_current_chain_actions(\@remaining_actions); | |||||||||||||||||||||||||||
| 82 | 299 | my @args; | |||||||||||||||||||||||||||
| 83 | 299 | ▶ | if (my $cap = $action->number_of_captures) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 180 | ▶ | @args = splice(@{ $self->_chain_captures||[] }, 0, $cap); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_chain_captures || []
MC/DC0%0/1
$self->_chain_captures || []
$self->_chain_captures
Truth table50%1/2
A
A= $self->_chain_captures
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 299 | ▶ | unshift @args, @{ $self->_chain_next_args} if $self->_has_chain_next_args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 299 | $self->_clear_chain_next_args; | |||||||||||||||||||||||||||
| 88 | 299 | local $c->request->{arguments} = \@args; | |||||||||||||||||||||||||||
| 89 | 299 | $action->dispatch( $c ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 298 | ▶ | sub _abort_needed { | ||||||||||||||||||||||||||
|
Subroutine
_abort_needed: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 93 | 298 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 94 | 298 | ▶ | my $abort = defined($c->config->{abort_chain_on_error_fix}) ? $c->config->{abort_chain_on_error_fix} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 298 | ▶ | return 1 if ($c->has_errors && $abort); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c->has_errors and $abort
MC/DC100%2/2
$c->has_errors and $abort
$c->has_errors
$abort
Truth table100%3/3
A and B
A= $c->has_errorsB= $abort
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 342 | ▶ | sub from_chain { | ||||||||||||||||||||||||||
|
Subroutine
from_chain: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 342 | my ( $self, $actions ) = @_; | |||||||||||||||||||||||||||
| 100 | 342 | my $final = $actions->[-1]; | |||||||||||||||||||||||||||
| 101 | 342 | return $self->new({ %$final, chain => $actions }); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 67 | ▶ | sub number_of_captures { | ||||||||||||||||||||||||||
|
Subroutine
number_of_captures: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 67 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 106 | 67 | my $chain = $self->chain; | |||||||||||||||||||||||||||
| 107 | 67 | my $captures = 0; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 67 | $captures += $_->number_of_captures for @$chain; | |||||||||||||||||||||||||||
| 110 | 67 | return $captures; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub match_captures { | ||||||||||||||||||||||||||
|
Subroutine
match_captures: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($self, $c, $captures) = @_; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | my @captures = @{$captures||[]}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$captures || []
MC/DC0%0/1
$captures || []
$captures
Truth table0%0/2
A
A= $captures
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | foreach my $link(@{$self->chain}) { | |||||||||||||||||||||||||||
| 118 | 0 | my @local_captures = splice @captures,0,$link->number_of_captures; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | return unless $link->match_captures($c, \@local_captures); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | return 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 47 | ▶ | sub match_captures_constraints { | ||||||||||||||||||||||||||
|
Subroutine
match_captures_constraints: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 47 | my ($self, $c, $captures) = @_; | |||||||||||||||||||||||||||
| 125 | 47 | ▶ | my @captures = @{$captures||[]}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$captures || []
MC/DC0%0/1
$captures || []
$captures
Truth table50%1/2
A
A= $captures
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 47 | foreach my $link(@{$self->chain}) { | |||||||||||||||||||||||||||
| 128 | 116 | my @local_captures = splice @captures,0,$link->number_of_captures; | |||||||||||||||||||||||||||
| 129 | 116 | ▶ | next unless $link->has_captures_constraints; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 13 | ▶ | return unless $link->match_captures_constraints($c, \@local_captures); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 44 | return 1; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # the scheme defined at the end of the chain is the one we use | ||||||||||||||||||||||||||||
| 136 | # but warn if too many. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 48 | ▶ | sub scheme { | ||||||||||||||||||||||||||
|
Subroutine
scheme: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 48 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 48 | my @chain = @{ $self->chain }; | |||||||||||||||||||||||||||
| 141 | my ($scheme, @more) = map { | ||||||||||||||||||||||||||||
| 142 | 48 | ▶ | exists $_->attributes->{Scheme} ? $_->attributes->{Scheme}[0] : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | } reverse @chain; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 48 | ▶ | warn "$self is a chain with two many Scheme attributes (only one is allowed)" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | if @more; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 48 | return $scheme; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 102 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 152 | 102 | 1; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||