Commit | Line | Data |
141459fa |
1 | package Catalyst::ActionChain; |
2 | |
059c085b |
3 | use Moose; |
4 | extends qw(Catalyst::Action); |
141459fa |
5 | |
059c085b |
6 | has chain => (is => 'rw'); |
0fc2d522 |
7 | no Moose; |
8 | |
141459fa |
9 | =head1 NAME |
10 | |
11 | Catalyst::ActionChain - Chain of Catalyst Actions |
12 | |
13 | =head1 SYNOPSIS |
14 | |
b2ddf6d7 |
15 | See L<Catalyst::Manual::Intro> for more info about Chained actions. |
141459fa |
16 | |
17 | =head1 DESCRIPTION |
18 | |
19 | This class represents a chain of Catalyst Actions. It behaves exactly like |
20 | the action at the *end* of the chain except on dispatch it will execute all |
21 | the actions in the chain in order. |
22 | |
b2ddf6d7 |
23 | =cut |
141459fa |
24 | |
141459fa |
25 | sub dispatch { |
26 | my ( $self, $c ) = @_; |
d7962771 |
27 | my @captures = @{$c->req->captures||[]}; |
ee1ac377 |
28 | my @chain = @{ $self->chain }; |
29 | my $last = pop(@chain); |
30 | foreach my $action ( @chain ) { |
d7962771 |
31 | my @args; |
0cff119a |
32 | if (my $cap = $action->number_of_captures) { |
33 | @args = splice(@captures, 0, $cap); |
d7962771 |
34 | } |
35 | local $c->request->{arguments} = \@args; |
141459fa |
36 | $action->dispatch( $c ); |
569b665e |
37 | |
f9bdcfac |
38 | # break the chain if exception occurs in the middle of chain. We |
39 | # check the global config flag 'abort_chain_on_error_fix', but this |
40 | # is now considered true by default, so unless someone explictly sets |
41 | # it to false we default it to true (if its not defined). |
42 | my $abort = defined($c->config->{abort_chain_on_error_fix}) ? |
43 | $c->config->{abort_chain_on_error_fix} : 1; |
44 | return if ($c->has_errors && $abort); |
141459fa |
45 | } |
ee1ac377 |
46 | $last->dispatch( $c ); |
141459fa |
47 | } |
48 | |
b2ddf6d7 |
49 | sub from_chain { |
50 | my ( $self, $actions ) = @_; |
51 | my $final = $actions->[-1]; |
52 | return $self->new({ %$final, chain => $actions }); |
53 | } |
54 | |
0cff119a |
55 | sub number_of_captures { |
56 | my ( $self ) = @_; |
57 | my $chain = $self->chain; |
58 | my $captures = 0; |
59 | |
60 | $captures += $_->number_of_captures for @$chain; |
61 | return $captures; |
62 | } |
63 | |
e5ecd5bc |
64 | __PACKAGE__->meta->make_immutable; |
b2ddf6d7 |
65 | 1; |
66 | |
67 | __END__ |
68 | |
69 | =head1 METHODS |
70 | |
71 | =head2 chain |
72 | |
73 | Accessor for the action chain; will be an arrayref of the Catalyst::Action |
74 | objects encapsulated by this chain. |
75 | |
76 | =head2 dispatch( $c ) |
77 | |
78 | Dispatch this action chain against a context; will dispatch the encapsulated |
79 | actions in order. |
80 | |
141459fa |
81 | =head2 from_chain( \@actions ) |
82 | |
83 | Takes a list of Catalyst::Action objects and constructs and returns a |
84 | Catalyst::ActionChain object representing a chain of these actions |
85 | |
0cff119a |
86 | =head2 number_of_captures |
87 | |
88 | Returns the total number of captures for the entire chain of actions. |
89 | |
059c085b |
90 | =head2 meta |
91 | |
92 | Provided by Moose |
141459fa |
93 | |
2f381252 |
94 | =head1 AUTHORS |
141459fa |
95 | |
2f381252 |
96 | Catalyst Contributors, see Catalyst.pm |
141459fa |
97 | |
98 | =head1 COPYRIGHT |
99 | |
536bee89 |
100 | This library is free software. You can redistribute it and/or modify it under |
141459fa |
101 | the same terms as Perl itself. |
102 | |
103 | =cut |