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