Better configuration key name
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionChain.pm
CommitLineData
141459fa 1package Catalyst::ActionChain;
2
059c085b 3use Moose;
4extends qw(Catalyst::Action);
141459fa 5
059c085b 6has chain => (is => 'rw');
0fc2d522 7no Moose;
8
141459fa 9=head1 NAME
10
11Catalyst::ActionChain - Chain of Catalyst Actions
12
13=head1 SYNOPSIS
14
b2ddf6d7 15See L<Catalyst::Manual::Intro> for more info about Chained actions.
141459fa 16
17=head1 DESCRIPTION
18
19This class represents a chain of Catalyst Actions. It behaves exactly like
20the action at the *end* of the chain except on dispatch it will execute all
21the actions in the chain in order.
22
b2ddf6d7 23=cut
141459fa 24
141459fa 25sub dispatch {
26 my ( $self, $c ) = @_;
d7962771 27 my @captures = @{$c->req->captures||[]};
ee1ac377 28 my @chain = @{ $self->chain };
29 my $last = pop(@chain);
30 foreach my $action ( @chain ) {
d7962771 31 my @args;
0cff119a 32 if (my $cap = $action->number_of_captures) {
33 @args = splice(@captures, 0, $cap);
d7962771 34 }
35 local $c->request->{arguments} = \@args;
141459fa 36 $action->dispatch( $c );
569b665e 37
38 # break the chain if exception occurs in the middle of chain
6e8520be 39 return if (@{$c->error} && $c->config->{abort_chain_on_error_fix});
141459fa 40 }
ee1ac377 41 $last->dispatch( $c );
141459fa 42}
43
b2ddf6d7 44sub from_chain {
45 my ( $self, $actions ) = @_;
46 my $final = $actions->[-1];
47 return $self->new({ %$final, chain => $actions });
48}
49
0cff119a 50sub number_of_captures {
51 my ( $self ) = @_;
52 my $chain = $self->chain;
53 my $captures = 0;
54
55 $captures += $_->number_of_captures for @$chain;
56 return $captures;
57}
58
e5ecd5bc 59__PACKAGE__->meta->make_immutable;
b2ddf6d7 601;
61
62__END__
63
64=head1 METHODS
65
66=head2 chain
67
68Accessor for the action chain; will be an arrayref of the Catalyst::Action
69objects encapsulated by this chain.
70
71=head2 dispatch( $c )
72
73Dispatch this action chain against a context; will dispatch the encapsulated
74actions in order.
75
141459fa 76=head2 from_chain( \@actions )
77
78Takes a list of Catalyst::Action objects and constructs and returns a
79Catalyst::ActionChain object representing a chain of these actions
80
0cff119a 81=head2 number_of_captures
82
83Returns the total number of captures for the entire chain of actions.
84
059c085b 85=head2 meta
86
87Provided by Moose
141459fa 88
2f381252 89=head1 AUTHORS
141459fa 90
2f381252 91Catalyst Contributors, see Catalyst.pm
141459fa 92
93=head1 COPYRIGHT
94
536bee89 95This library is free software. You can redistribute it and/or modify it under
141459fa 96the same terms as Perl itself.
97
98=cut