Commit | Line | Data |
141459fa |
1 | package Catalyst::ActionChain; |
2 | |
3 | use strict; |
4 | use base qw/Catalyst::Action/; |
5 | |
141459fa |
6 | |
7 | =head1 NAME |
8 | |
9 | Catalyst::ActionChain - Chain of Catalyst Actions |
10 | |
11 | =head1 SYNOPSIS |
12 | |
b2ddf6d7 |
13 | See L<Catalyst::Manual::Intro> for more info about Chained actions. |
141459fa |
14 | |
15 | =head1 DESCRIPTION |
16 | |
17 | This class represents a chain of Catalyst Actions. It behaves exactly like |
18 | the action at the *end* of the chain except on dispatch it will execute all |
19 | the actions in the chain in order. |
20 | |
b2ddf6d7 |
21 | =cut |
141459fa |
22 | |
b2ddf6d7 |
23 | __PACKAGE__->mk_accessors(qw/chain/); |
141459fa |
24 | |
b2ddf6d7 |
25 | use overload ( |
141459fa |
26 | |
b2ddf6d7 |
27 | # Stringify to reverse for debug output etc. |
28 | q{""} => sub { shift->{reverse} }, |
141459fa |
29 | |
b2ddf6d7 |
30 | # Codulate to execute to invoke the encapsulated action coderef |
31 | '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; }, |
32 | |
33 | # Make general $stuff still work |
34 | fallback => 1, |
35 | |
36 | ); |
141459fa |
37 | |
141459fa |
38 | |
39 | sub dispatch { |
40 | my ( $self, $c ) = @_; |
d7962771 |
41 | my @captures = @{$c->req->captures||[]}; |
ee1ac377 |
42 | my @chain = @{ $self->chain }; |
43 | my $last = pop(@chain); |
44 | foreach my $action ( @chain ) { |
d7962771 |
45 | my @args; |
1c34f703 |
46 | if (my $cap = $action->attributes->{CaptureArgs}) { |
d7962771 |
47 | @args = splice(@captures, 0, $cap->[0]); |
48 | } |
49 | local $c->request->{arguments} = \@args; |
141459fa |
50 | $action->dispatch( $c ); |
51 | } |
ee1ac377 |
52 | $last->dispatch( $c ); |
141459fa |
53 | } |
54 | |
b2ddf6d7 |
55 | sub from_chain { |
56 | my ( $self, $actions ) = @_; |
57 | my $final = $actions->[-1]; |
58 | return $self->new({ %$final, chain => $actions }); |
59 | } |
60 | |
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 | |
82 | =cut |
83 | |
141459fa |
84 | =head1 AUTHOR |
85 | |
86 | Matt S. Trout |
87 | |
88 | =head1 COPYRIGHT |
89 | |
90 | This program is free software, you can redistribute it and/or modify it under |
91 | the same terms as Perl itself. |
92 | |
93 | =cut |