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 | |
87c672db |
7 | our $VERSION = '0.16'; |
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 | |
87c672db |
17 | my $res_body = $c->subreq('/test/foo/bar', { template => 'magic.tt' }); |
aae30f91 |
18 | |
87c672db |
19 | my $res_body = $c->subreq( { |
20 | path => '/test/foo/bar', |
21 | body => $body |
22 | }, { |
23 | template => 'magic.tt' |
24 | }); |
25 | |
26 | # Get the full response object |
27 | my $res = $c->subreq_res('/test/foo/bar', { |
28 | template => 'mailz.tt' |
29 | }, { |
30 | param1 => 23 |
31 | }); |
32 | $c->log->warn( $res->content_type ); |
4f38f6a7 |
33 | |
aae30f91 |
34 | =head1 DESCRIPTION |
35 | |
8c464987 |
36 | Make subrequests to actions in Catalyst. Uses the catalyst |
37 | dispatcher, so it will work like an external url call. |
87c672db |
38 | Methods are provided both to get the body of the response and the full |
39 | response (L<Catalyst::Response>) object. |
aae30f91 |
40 | |
41 | =head1 METHODS |
42 | |
a1e0150f |
43 | =over 4 |
aae30f91 |
44 | |
4f38f6a7 |
45 | =item subreq [path as string or hash ref], [stash as hash ref], [parameters as hash ref] |
aae30f91 |
46 | |
51589f61 |
47 | =item subrequest |
48 | |
aae30f91 |
49 | =item sub_request |
50 | |
4f38f6a7 |
51 | Takes a full path to a path you'd like to dispatch to. |
87c672db |
52 | |
53 | If the path is passed as a hash ref then it can include body, action, |
54 | match and path. |
55 | |
56 | An optional second argument as hashref can contain data to put into the |
57 | stash of the subrequest. |
58 | |
59 | An optional third argument as hashref can contain data to pass as |
60 | parameters to the subrequest. |
61 | |
62 | Returns the body of the response. |
63 | |
64 | =item subreq_res [path as string or hash ref], [stash as hash ref], [parameters as hash ref] |
65 | |
51589f61 |
66 | =item subrequest_response |
67 | |
87c672db |
68 | =item sub_request_response |
69 | |
70 | Like C<sub_request()>, but returns a full L<Catalyst::Response> object. |
aae30f91 |
71 | |
a1e0150f |
72 | =back |
aae30f91 |
73 | |
74 | =cut |
75 | |
76 | *subreq = \&sub_request; |
51589f61 |
77 | *subrequest = \&sub_request; |
87c672db |
78 | *subreq_res = \&sub_request_response; |
51589f61 |
79 | *subrequest_response = \&sub_request_response; |
aae30f91 |
80 | |
81 | sub sub_request { |
87c672db |
82 | return shift->sub_request_response( @_ )->body ; |
83 | } |
84 | |
85 | sub sub_request_response { |
885f6da0 |
86 | my ( $c, $path, $stash, $params ) = @_; |
8c464987 |
87 | |
5bd316a5 |
88 | $path =~ s#^/##; |
29ec3000 |
89 | |
90 | $params ||= {}; |
91 | |
92 | my %request_mods = ( |
93 | body => undef, |
94 | action => undef, |
95 | match => undef, |
96 | parameters => $params, |
97 | ); |
98 | |
99 | if (ref $path eq 'HASH') { |
100 | @request_mods{keys %$path} = values %$path; |
101 | } else { |
102 | $request_mods{path} = $path; |
103 | } |
61114b68 |
104 | $request_mods{_body} = delete $request_mods{body}; |
29ec3000 |
105 | |
106 | my $fake_engine = bless( |
107 | { |
108 | orig_request => $c->req, |
109 | request_mods => \%request_mods, |
110 | }, |
111 | 'Catalyst::Plugin::SubRequest::Internal::FakeEngine' |
112 | ); |
113 | |
114 | my $class = ref($c); |
115 | |
116 | no strict 'refs'; |
117 | no warnings 'redefine'; |
118 | |
119 | local *{"${class}::engine"} = sub { $fake_engine }; |
120 | |
121 | my $inner_ctx = $class->prepare; |
122 | |
123 | $inner_ctx->stash($stash || {}); |
a1e0150f |
124 | |
125 | |
6162c29a |
126 | $c->stats->profile( |
127 | begin => 'subrequest: /' . $path, |
128 | comment => '', |
a1e0150f |
129 | ) if ($c->debug); |
130 | |
6162c29a |
131 | $inner_ctx->dispatch; |
132 | |
133 | $c->stats->profile( end => 'subrequest: /' . $path ) if ($c->debug); |
a1e0150f |
134 | |
87c672db |
135 | return $inner_ctx->response; |
aae30f91 |
136 | } |
137 | |
138 | =head1 SEE ALSO |
139 | |
140 | L<Catalyst>. |
141 | |
61114b68 |
142 | =head1 AUTHORS |
aae30f91 |
143 | |
144 | Marcus Ramberg, C<mramberg@cpan.org> |
145 | |
61114b68 |
146 | Tomas Doran (t0m) C<< bobtfish@bobtfish.net >> |
147 | |
aae30f91 |
148 | =head1 THANK YOU |
149 | |
150 | SRI, for writing the awesome Catalyst framework |
151 | |
152 | =head1 COPYRIGHT |
153 | |
85ec975f |
154 | Copyright (c) 2005 - 2008 |
61114b68 |
155 | the Catalyst::Plugin::SubRequest L</AUTHORS> |
85ec975f |
156 | as listed above. |
157 | |
158 | =head1 LICENSE |
159 | |
aae30f91 |
160 | This program is free software, you can redistribute it and/or modify it under |
161 | the same terms as Perl itself. |
162 | |
163 | =cut |
164 | |
29ec3000 |
165 | package # hide from PAUSE |
166 | Catalyst::Plugin::SubRequest::Internal::FakeEngine; |
167 | |
168 | sub AUTOLOAD { return 1; } # yeah yeah yeah |
169 | |
170 | sub prepare { |
171 | my ($self, $c) = @_; |
172 | my $req = $c->request; |
a1e0150f |
173 | |
cc9f9d31 |
174 | @{$req}{keys %{$self->{orig_request}}} = values %{$self->{orig_request}}; |
175 | while (my ($key,$value) = each %{$self->{request_mods}}) { |
597f0a02 |
176 | if (my $mut = $req->can($key)) { |
177 | $req->$mut($value); |
178 | } else { |
179 | $req->{$key} = $value; |
180 | } |
181 | } |
29ec3000 |
182 | } |
183 | |
aae30f91 |
184 | 1; |