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