450c88e05ac89f7613582e2b1f435a5c7d014dc5
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionChain.pm
1 package Catalyst::ActionChain;
2
3 use Class::C3;
4 use Moose;
5 extends qw(Catalyst::Action);
6
7 has chain => (is => 'rw');
8
9 no Moose;
10
11 =head1 NAME
12
13 Catalyst::ActionChain - Chain of Catalyst Actions
14
15 =head1 SYNOPSIS
16
17 See L<Catalyst::Manual::Intro> for more info about Chained actions.
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
25 =cut
26
27 sub dispatch {
28     my ( $self, $c ) = @_;
29     my @captures = @{$c->req->captures||[]};
30     my @chain = @{ $self->chain };
31     my $last = pop(@chain);
32     foreach my $action ( @chain ) {
33         my @args;
34         if (my $cap = $action->attributes->{CaptureArgs}) {
35           @args = splice(@captures, 0, $cap->[0]);
36         }
37         local $c->request->{arguments} = \@args;
38         $action->dispatch( $c );
39     }
40     $last->dispatch( $c );
41 }
42
43 sub from_chain {
44     my ( $self, $actions ) = @_;
45     my $final = $actions->[-1];
46     return $self->new({ %$final, chain => $actions });
47 }
48
49 __PACKAGE__->meta->make_immutable;
50 1;
51
52 __END__
53
54 =head1 METHODS
55
56 =head2 chain
57
58 Accessor for the action chain; will be an arrayref of the Catalyst::Action
59 objects encapsulated by this chain.
60
61 =head2 dispatch( $c )
62
63 Dispatch this action chain against a context; will dispatch the encapsulated
64 actions in order.
65
66 =head2 from_chain( \@actions )
67
68 Takes a list of Catalyst::Action objects and constructs and returns a
69 Catalyst::ActionChain object representing a chain of these actions
70
71 =head2 meta
72
73 Provided by Moose
74
75 =head1 AUTHOR
76
77 Matt S. Trout
78
79 =head1 COPYRIGHT
80
81 This program is free software, you can redistribute it and/or modify it under
82 the same terms as Perl itself.
83
84 =cut