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