Commit | Line | Data |
141459fa |
1 | package Catalyst::ActionChain; |
2 | |
6f1f968a |
3 | use MRO::Compat; |
4 | use mro 'c3'; |
059c085b |
5 | use Moose; |
6 | extends qw(Catalyst::Action); |
141459fa |
7 | |
059c085b |
8 | has chain => (is => 'rw'); |
141459fa |
9 | |
0fc2d522 |
10 | no Moose; |
11 | |
141459fa |
12 | =head1 NAME |
13 | |
14 | Catalyst::ActionChain - Chain of Catalyst Actions |
15 | |
16 | =head1 SYNOPSIS |
17 | |
b2ddf6d7 |
18 | See L<Catalyst::Manual::Intro> for more info about Chained actions. |
141459fa |
19 | |
20 | =head1 DESCRIPTION |
21 | |
22 | This class represents a chain of Catalyst Actions. It behaves exactly like |
23 | the action at the *end* of the chain except on dispatch it will execute all |
24 | the actions in the chain in order. |
25 | |
b2ddf6d7 |
26 | =cut |
141459fa |
27 | |
141459fa |
28 | sub dispatch { |
29 | my ( $self, $c ) = @_; |
d7962771 |
30 | my @captures = @{$c->req->captures||[]}; |
ee1ac377 |
31 | my @chain = @{ $self->chain }; |
32 | my $last = pop(@chain); |
33 | foreach my $action ( @chain ) { |
d7962771 |
34 | my @args; |
1c34f703 |
35 | if (my $cap = $action->attributes->{CaptureArgs}) { |
d7962771 |
36 | @args = splice(@captures, 0, $cap->[0]); |
37 | } |
38 | local $c->request->{arguments} = \@args; |
141459fa |
39 | $action->dispatch( $c ); |
40 | } |
ee1ac377 |
41 | $last->dispatch( $c ); |
141459fa |
42 | } |
43 | |
b2ddf6d7 |
44 | sub from_chain { |
45 | my ( $self, $actions ) = @_; |
46 | my $final = $actions->[-1]; |
47 | return $self->new({ %$final, chain => $actions }); |
48 | } |
49 | |
e5ecd5bc |
50 | __PACKAGE__->meta->make_immutable; |
b2ddf6d7 |
51 | 1; |
52 | |
53 | __END__ |
54 | |
55 | =head1 METHODS |
56 | |
57 | =head2 chain |
58 | |
59 | Accessor for the action chain; will be an arrayref of the Catalyst::Action |
60 | objects encapsulated by this chain. |
61 | |
62 | =head2 dispatch( $c ) |
63 | |
64 | Dispatch this action chain against a context; will dispatch the encapsulated |
65 | actions in order. |
66 | |
141459fa |
67 | =head2 from_chain( \@actions ) |
68 | |
69 | Takes a list of Catalyst::Action objects and constructs and returns a |
70 | Catalyst::ActionChain object representing a chain of these actions |
71 | |
059c085b |
72 | =head2 meta |
73 | |
74 | Provided by Moose |
141459fa |
75 | |
141459fa |
76 | =head1 AUTHOR |
77 | |
78 | Matt S. Trout |
79 | |
80 | =head1 COPYRIGHT |
81 | |
82 | This program is free software, you can redistribute it and/or modify it under |
83 | the same terms as Perl itself. |
84 | |
85 | =cut |