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