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