5e222c23db665c4db4d27a0f0f3bb0f4dfe5b4a2
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionChain.pm
1 package Catalyst::ActionChain;
2
3 use Moose;
4 extends qw(Catalyst::Action);
5
6 has chain => (is => 'rw');
7 no Moose;
8
9 =head1 NAME
10
11 Catalyst::ActionChain - Chain of Catalyst Actions
12
13 =head1 SYNOPSIS
14
15 See L<Catalyst::Manual::Intro> for more info about Chained actions.
16
17 =head1 DESCRIPTION
18
19 This class represents a chain of Catalyst Actions. It behaves exactly like
20 the action at the *end* of the chain except on dispatch it will execute all
21 the actions in the chain in order.
22
23 =cut
24
25 sub dispatch {
26     my ( $self, $c ) = @_;
27     my @captures = @{$c->req->captures||[]};
28     my @chain = @{ $self->chain };
29     my $last = pop(@chain);
30     foreach my $action ( @chain ) {
31         my @args;
32         if (my $cap = $action->number_of_captures) {
33           @args = splice(@captures, 0, $cap);
34         }
35         local $c->request->{arguments} = \@args;
36         $action->dispatch( $c );
37
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
40         # is now considered true by default, so unless someone explicitly sets
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);
45     }
46     $last->dispatch( $c );
47 }
48
49 sub from_chain {
50     my ( $self, $actions ) = @_;
51     my $final = $actions->[-1];
52     return $self->new({ %$final, chain => $actions });
53 }
54
55 sub 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
64 # the scheme defined at the end of the chain is the one we use
65 # but warn if too many.
66
67 sub scheme {
68   my $self = shift;
69   my @chain = @{ $self->chain };
70   my ($scheme, @more) = map {
71     exists $_->attributes->{Scheme} ? $_->attributes->{Scheme}[0] : ();
72   } reverse @chain;
73
74   warn "$self is a chain with two many Scheme attributes (only one is allowed)"
75     if @more;
76
77   return $scheme;
78 }
79
80 __PACKAGE__->meta->make_immutable;
81 1;
82
83 __END__
84
85 =head1 METHODS
86
87 =head2 chain
88
89 Accessor for the action chain; will be an arrayref of the Catalyst::Action
90 objects encapsulated by this chain.
91
92 =head2 dispatch( $c )
93
94 Dispatch this action chain against a context; will dispatch the encapsulated
95 actions in order.
96
97 =head2 from_chain( \@actions )
98
99 Takes a list of Catalyst::Action objects and constructs and returns a
100 Catalyst::ActionChain object representing a chain of these actions
101
102 =head2 number_of_captures
103
104 Returns the total number of captures for the entire chain of actions.
105
106 =head2 scheme
107
108 Any defined scheme for the actionchain
109
110 =head2 meta
111
112 Provided by Moose
113
114 =head1 AUTHORS
115
116 Catalyst Contributors, see Catalyst.pm
117
118 =head1 COPYRIGHT
119
120 This library is free software. You can redistribute it and/or modify it under
121 the same terms as Perl itself.
122
123 =cut