Commit | Line | Data |
aae30f91 |
1 | package Catalyst::Plugin::SubRequest; |
2 | |
3 | use strict; |
61114b68 |
4 | use warnings; |
588fd7ac |
5 | use Time::HiRes qw/tv_interval/; |
aae30f91 |
6 | |
61114b68 |
7 | our $VERSION = '0.15'; |
aae30f91 |
8 | |
aae30f91 |
9 | =head1 NAME |
10 | |
11 | Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst |
12 | |
13 | =head1 SYNOPSIS |
14 | |
15 | use Catalyst 'SubRequest'; |
16 | |
885f6da0 |
17 | $c->subreq('/test/foo/bar', { template => 'magic.tt' }); |
aae30f91 |
18 | |
4f38f6a7 |
19 | $c->subreq( { path => '/test/foo/bar', |
20 | body => $body }, |
21 | { template => 'magic.tt' }); |
22 | |
aae30f91 |
23 | =head1 DESCRIPTION |
24 | |
8c464987 |
25 | Make subrequests to actions in Catalyst. Uses the catalyst |
26 | dispatcher, so it will work like an external url call. |
aae30f91 |
27 | |
28 | =head1 METHODS |
29 | |
a1e0150f |
30 | =over 4 |
aae30f91 |
31 | |
4f38f6a7 |
32 | =item subreq [path as string or hash ref], [stash as hash ref], [parameters as hash ref] |
aae30f91 |
33 | |
34 | =item sub_request |
35 | |
4f38f6a7 |
36 | Takes a full path to a path you'd like to dispatch to. |
37 | If the path is passed as a hash ref then it can include body, action, match and path. |
38 | Any additional parameters are put into the stash. |
aae30f91 |
39 | |
a1e0150f |
40 | =back |
aae30f91 |
41 | |
42 | =cut |
43 | |
44 | *subreq = \&sub_request; |
45 | |
46 | sub sub_request { |
885f6da0 |
47 | my ( $c, $path, $stash, $params ) = @_; |
8c464987 |
48 | |
5bd316a5 |
49 | $path =~ s#^/##; |
29ec3000 |
50 | |
51 | $params ||= {}; |
52 | |
53 | my %request_mods = ( |
54 | body => undef, |
55 | action => undef, |
56 | match => undef, |
57 | parameters => $params, |
58 | ); |
59 | |
60 | if (ref $path eq 'HASH') { |
61 | @request_mods{keys %$path} = values %$path; |
62 | } else { |
63 | $request_mods{path} = $path; |
64 | } |
61114b68 |
65 | $request_mods{_body} = delete $request_mods{body}; |
29ec3000 |
66 | |
67 | my $fake_engine = bless( |
68 | { |
69 | orig_request => $c->req, |
70 | request_mods => \%request_mods, |
71 | }, |
72 | 'Catalyst::Plugin::SubRequest::Internal::FakeEngine' |
73 | ); |
74 | |
75 | my $class = ref($c); |
76 | |
77 | no strict 'refs'; |
78 | no warnings 'redefine'; |
79 | |
80 | local *{"${class}::engine"} = sub { $fake_engine }; |
81 | |
82 | my $inner_ctx = $class->prepare; |
83 | |
84 | $inner_ctx->stash($stash || {}); |
a1e0150f |
85 | |
86 | |
6162c29a |
87 | $c->stats->profile( |
88 | begin => 'subrequest: /' . $path, |
89 | comment => '', |
a1e0150f |
90 | ) if ($c->debug); |
91 | |
6162c29a |
92 | $inner_ctx->dispatch; |
93 | |
94 | $c->stats->profile( end => 'subrequest: /' . $path ) if ($c->debug); |
a1e0150f |
95 | |
29ec3000 |
96 | return $inner_ctx->response->body; |
aae30f91 |
97 | } |
98 | |
99 | =head1 SEE ALSO |
100 | |
101 | L<Catalyst>. |
102 | |
61114b68 |
103 | =head1 AUTHORS |
aae30f91 |
104 | |
105 | Marcus Ramberg, C<mramberg@cpan.org> |
106 | |
61114b68 |
107 | Tomas Doran (t0m) C<< bobtfish@bobtfish.net >> |
108 | |
aae30f91 |
109 | =head1 THANK YOU |
110 | |
111 | SRI, for writing the awesome Catalyst framework |
112 | |
113 | =head1 COPYRIGHT |
114 | |
85ec975f |
115 | Copyright (c) 2005 - 2008 |
61114b68 |
116 | the Catalyst::Plugin::SubRequest L</AUTHORS> |
85ec975f |
117 | as listed above. |
118 | |
119 | =head1 LICENSE |
120 | |
aae30f91 |
121 | This program is free software, you can redistribute it and/or modify it under |
122 | the same terms as Perl itself. |
123 | |
124 | =cut |
125 | |
29ec3000 |
126 | package # hide from PAUSE |
127 | Catalyst::Plugin::SubRequest::Internal::FakeEngine; |
128 | |
129 | sub AUTOLOAD { return 1; } # yeah yeah yeah |
130 | |
131 | sub prepare { |
132 | my ($self, $c) = @_; |
133 | my $req = $c->request; |
a1e0150f |
134 | |
cc9f9d31 |
135 | @{$req}{keys %{$self->{orig_request}}} = values %{$self->{orig_request}}; |
136 | while (my ($key,$value) = each %{$self->{request_mods}}) { |
597f0a02 |
137 | if (my $mut = $req->can($key)) { |
138 | $req->$mut($value); |
139 | } else { |
140 | $req->{$key} = $value; |
141 | } |
142 | } |
29ec3000 |
143 | } |
144 | |
aae30f91 |
145 | 1; |