Merge branch 'pr/135' into release-candidates/rc-5.90116
[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
f9bdcfac 38 # break the chain if exception occurs in the middle of chain. We
39 # check the global config flag 'abort_chain_on_error_fix', but this
566678d0 40 # is now considered true by default, so unless someone explicitly sets
f9bdcfac 41 # it to false we default it to true (if its not defined).
42 my $abort = defined($c->config->{abort_chain_on_error_fix}) ?
43 $c->config->{abort_chain_on_error_fix} : 1;
44 return if ($c->has_errors && $abort);
141459fa 45 }
ee1ac377 46 $last->dispatch( $c );
141459fa 47}
48
b2ddf6d7 49sub from_chain {
50 my ( $self, $actions ) = @_;
51 my $final = $actions->[-1];
52 return $self->new({ %$final, chain => $actions });
53}
54
0cff119a 55sub number_of_captures {
56 my ( $self ) = @_;
57 my $chain = $self->chain;
58 my $captures = 0;
59
60 $captures += $_->number_of_captures for @$chain;
61 return $captures;
62}
63
86a399db 64sub match_captures {
65 my ($self, $c, $captures) = @_;
66 my @captures = @{$captures||[]};
67
68 foreach my $link(@{$self->chain}) {
69 my @local_captures = splice @captures,0,$link->number_of_captures;
70 return unless $link->match_captures($c, \@local_captures);
71 }
72 return 1;
73}
ec4d7259 74sub match_captures_constraints {
75 my ($self, $c, $captures) = @_;
76 my @captures = @{$captures||[]};
77
78 foreach my $link(@{$self->chain}) {
79 my @local_captures = splice @captures,0,$link->number_of_captures;
80 next unless $link->has_captures_constraints;
81 return unless $link->match_captures_constraints($c, \@local_captures);
82 }
83 return 1;
84}
86a399db 85
342d2169 86# the scheme defined at the end of the chain is the one we use
87# but warn if too many.
88
89sub scheme {
90 my $self = shift;
91 my @chain = @{ $self->chain };
92 my ($scheme, @more) = map {
93 exists $_->attributes->{Scheme} ? $_->attributes->{Scheme}[0] : ();
94 } reverse @chain;
95
96 warn "$self is a chain with two many Scheme attributes (only one is allowed)"
97 if @more;
98
99 return $scheme;
100}
101
e5ecd5bc 102__PACKAGE__->meta->make_immutable;
b2ddf6d7 1031;
104
105__END__
106
107=head1 METHODS
108
109=head2 chain
110
111Accessor for the action chain; will be an arrayref of the Catalyst::Action
112objects encapsulated by this chain.
113
114=head2 dispatch( $c )
115
116Dispatch this action chain against a context; will dispatch the encapsulated
117actions in order.
118
141459fa 119=head2 from_chain( \@actions )
120
121Takes a list of Catalyst::Action objects and constructs and returns a
122Catalyst::ActionChain object representing a chain of these actions
123
0cff119a 124=head2 number_of_captures
125
126Returns the total number of captures for the entire chain of actions.
127
86a399db 128=head2 match_captures
129
130Match all the captures that this chain encloses, if any.
131
342d2169 132=head2 scheme
133
134Any defined scheme for the actionchain
135
059c085b 136=head2 meta
137
138Provided by Moose
141459fa 139
2f381252 140=head1 AUTHORS
141459fa 141
2f381252 142Catalyst Contributors, see Catalyst.pm
141459fa 143
144=head1 COPYRIGHT
145
536bee89 146This library is free software. You can redistribute it and/or modify it under
141459fa 147the same terms as Perl itself.
148
149=cut