start using Class::C3, may need to add a reinitalize bit later, not sure
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionChain.pm
CommitLineData
141459fa 1package Catalyst::ActionChain;
2
0fc2d522 3use Class::C3;
059c085b 4use Moose;
5extends qw(Catalyst::Action);
141459fa 6
059c085b 7has chain => (is => 'rw');
141459fa 8
0fc2d522 9no Moose;
10
141459fa 11=head1 NAME
12
13Catalyst::ActionChain - Chain of Catalyst Actions
14
15=head1 SYNOPSIS
16
b2ddf6d7 17See L<Catalyst::Manual::Intro> for more info about Chained actions.
141459fa 18
19=head1 DESCRIPTION
20
21This class represents a chain of Catalyst Actions. It behaves exactly like
22the action at the *end* of the chain except on dispatch it will execute all
23the actions in the chain in order.
24
b2ddf6d7 25=cut
141459fa 26
b2ddf6d7 27use overload (
141459fa 28
b2ddf6d7 29 # Stringify to reverse for debug output etc.
30 q{""} => sub { shift->{reverse} },
141459fa 31
b2ddf6d7 32 # Codulate to execute to invoke the encapsulated action coderef
33 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
34
35 # Make general $stuff still work
36 fallback => 1,
37
38);
141459fa 39
141459fa 40
41sub dispatch {
42 my ( $self, $c ) = @_;
d7962771 43 my @captures = @{$c->req->captures||[]};
ee1ac377 44 my @chain = @{ $self->chain };
45 my $last = pop(@chain);
46 foreach my $action ( @chain ) {
d7962771 47 my @args;
1c34f703 48 if (my $cap = $action->attributes->{CaptureArgs}) {
d7962771 49 @args = splice(@captures, 0, $cap->[0]);
50 }
51 local $c->request->{arguments} = \@args;
141459fa 52 $action->dispatch( $c );
53 }
ee1ac377 54 $last->dispatch( $c );
141459fa 55}
56
b2ddf6d7 57sub from_chain {
58 my ( $self, $actions ) = @_;
59 my $final = $actions->[-1];
60 return $self->new({ %$final, chain => $actions });
61}
62
e5ecd5bc 63__PACKAGE__->meta->make_immutable;
b2ddf6d7 641;
65
66__END__
67
68=head1 METHODS
69
70=head2 chain
71
72Accessor for the action chain; will be an arrayref of the Catalyst::Action
73objects encapsulated by this chain.
74
75=head2 dispatch( $c )
76
77Dispatch this action chain against a context; will dispatch the encapsulated
78actions in order.
79
141459fa 80=head2 from_chain( \@actions )
81
82Takes a list of Catalyst::Action objects and constructs and returns a
83Catalyst::ActionChain object representing a chain of these actions
84
059c085b 85=head2 meta
86
87Provided by Moose
141459fa 88
141459fa 89=head1 AUTHOR
90
91Matt S. Trout
92
93=head1 COPYRIGHT
94
95This program is free software, you can redistribute it and/or modify it under
96the same terms as Perl itself.
97
98=cut