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 ); |
37 | } |
ee1ac377 |
38 | $last->dispatch( $c ); |
141459fa |
39 | } |
40 | |
b2ddf6d7 |
41 | sub from_chain { |
42 | my ( $self, $actions ) = @_; |
43 | my $final = $actions->[-1]; |
44 | return $self->new({ %$final, chain => $actions }); |
45 | } |
46 | |
0cff119a |
47 | sub number_of_captures { |
48 | my ( $self ) = @_; |
49 | my $chain = $self->chain; |
50 | my $captures = 0; |
51 | |
52 | $captures += $_->number_of_captures for @$chain; |
53 | return $captures; |
54 | } |
55 | |
e5ecd5bc |
56 | __PACKAGE__->meta->make_immutable; |
b2ddf6d7 |
57 | 1; |
58 | |
59 | __END__ |
60 | |
61 | =head1 METHODS |
62 | |
63 | =head2 chain |
64 | |
65 | Accessor for the action chain; will be an arrayref of the Catalyst::Action |
66 | objects encapsulated by this chain. |
67 | |
68 | =head2 dispatch( $c ) |
69 | |
70 | Dispatch this action chain against a context; will dispatch the encapsulated |
71 | actions in order. |
72 | |
141459fa |
73 | =head2 from_chain( \@actions ) |
74 | |
75 | Takes a list of Catalyst::Action objects and constructs and returns a |
76 | Catalyst::ActionChain object representing a chain of these actions |
77 | |
0cff119a |
78 | =head2 number_of_captures |
79 | |
80 | Returns the total number of captures for the entire chain of actions. |
81 | |
059c085b |
82 | =head2 meta |
83 | |
84 | Provided by Moose |
141459fa |
85 | |
2f381252 |
86 | =head1 AUTHORS |
141459fa |
87 | |
2f381252 |
88 | Catalyst Contributors, see Catalyst.pm |
141459fa |
89 | |
90 | =head1 COPYRIGHT |
91 | |
536bee89 |
92 | This library is free software. You can redistribute it and/or modify it under |
141459fa |
93 | the same terms as Perl itself. |
94 | |
95 | =cut |