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