Jifty/Continuation.pm

filter: Statementstmt 23.2% 17 / 73 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 44.4% 4 / 9 Podpod 100% 5 / 5 totaltotal 20.3% 26 / 128 CC 20 SCAR 35.2
CC
20
Cov
16%
new
56.0
return_path_matches
42.0
call
42.0
CRAP 33.9
« Jifty/Dispatcher.pm ↑ Summary Jifty/View/Static/Handler.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Continuation;
5  
6 =head1 NAME
7  
8 Jifty::Continuation - Allows for basic continuation-based programming
9  
10 =head1 DESCRIPTION
11  
12 In programming, a continuation is a construct that allows you to freeze the current state of a program and then recover that state later by calling the continuation. For example, you could save a continuation when throwing an exception to save the state, an exception handler could resolve the problem that caused the exception, and then call the continuation to resume execution at the point where the exception was thrown now that the problem has been solved.
13  
14 In Jifty, continuations are used to save the state of a request (and sometimes the response). Continuations can be used in situations such as these:
15  
16 =over
17  
18 =item 1.
19  
20 A user visits a page that requires login to view. The dispatcher saves a continuation and then sends the user off to the login page. Once the user logs in successfully, the login action can call the continuation to return the user back to the original page.
21  
22 =item 2.
23  
24 A blogging application might have a "Edit" link on each post to allow the editor to jump to the change page. If this link includes a saved continuation, then the "Save" button could trigger that continuation to be called to return the user back to the original page where they clicked "Edit". This way, it could return the user to the view page, or a list page, or an administrative view depending on which page the user started the edit process from.
25  
26 =item 3.
27  
28 If you have a wizard for editing some information in your application, but entering some data may require jumping to another page you can save a continuation to allow the user to return after editing. If that page also requires a jump to yet another page, you can save another continuation. Since continuations save a stack of previous continuations, you can return twice to get back to the wizard.
29  
30 =back
31  
32 C<Jifty::Continuation> handles the details of saving this information for later recovery. When a continuation is saved, the current request and response are saved to the database in the current user's session. When a continuation is called, the current request and response become those that were saved in the continuation. A continuation can be called at any point in the same session.
33  
34 Continuations store a L<Jifty::Request> object and the
35 L<Jifty::Response> object for the request. They can also store
36 arbitrary code to be run when the continuation is called.
37  
38 Continuations can also be arbitrarily nested. This means that
39 returning from one continuation will drop you into the continuation
40 that is one higher in the stack.
41  
42 Continuations are generally created just before their request would
43 take effect, activated by the presence of certain query parameters.
44 The rest of the request is saved, its execution is to be continued at a
45 later time.
46  
47 Continuations are run after any actions have run. When a continuation
48 is run, it restores the request that it has saved away into it, and
49 pulls into that request the values any return values that were
50 specified when it was created. The continuations code block, if any,
51 is then called, and then the filled-in request is then passed to the
52 L<Jifty::Dispatcher>.
53  
54 =cut
55  
56  
57 1 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 $Storable::Deparse = 1;
59 1 $Storable::Eval = 1;
60 1 $Storable::forgive_me = 1;
61  
62 1 ▶use base qw/Class::Accessor::Fast Jifty::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 1 __PACKAGE__->mk_accessors(qw(id parent
65 request response code
66 version
67 ));
68  
69 =head2 new PARAMHASH
70  
71 Saves a continuation at the current state. Possible arguments in the
72 C<PARAMHASH>:
73  
74 =over
75  
76 =item parent
77  
78 A L<Jifty::Continuation> object, or the C<id> of one. This represents
79 the continuation that this continuation should return to when it is
80 called. Defaults to the current continuation of the current
81 L<Jifty::Request>.
82  
83 =item request
84  
85 The L<Jifty::Request> object to save away. Defaults to an empty
86 L<Jifty::Request> object.
87  
88 =item response
89  
90 The L<Jifty::Response> object that will be loaded up when the
91 continuation is run. Most of the time, the response isn't stored in
92 the continuation, since the continuation was saved away B<before> the
93 actions got run. In the case when continuations are used to preserve
94 state across a redirect, however, we tuck the L<Jifty::Response> value
95 of the previous request into the continuation as well. Defaults to an
96 empty L<Jifty::Response> object.
97  
98 =item code
99  
100 An optional subroutine reference to evaluate when the continuation is
101 called.
102  
103 =back
104  
105 =cut
106  
107 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
108 0 my $class = shift;
109 0 my $self = bless { }, $class;
110  
111 0 my %args = (
112 parent => Jifty->web->request->continuation,
113 request => Jifty::Request->new(),
114 response => Jifty::Response->new(),
115 code => undef,
116 @_,
117 version => 2,
118 );
119  
120 # We don't want refs
121 $args{parent} = $args{parent}->id
122 0 ▶ if $args{parent} and ref $args{parent};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"parent"} and ref $args{"parent"} 0 0 0
Condition0%0/3
$args{"parent"} and ref $args{"parent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"parent"} and ref $args{"parent"}
$args{"parent"} ref $args{"parent"}
Truth table0%0/3
A and B
A= $args{"parent"}B= ref $args{"parent"}
ABresult
0X0
100
111
123  
124 # We're getting most of our properties from the arguments
125 0 for (keys %args) {
126 0 ▶ $self->$_($args{$_}) if $self->can($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($_) 0 0 0
127 }
128  
129 # Generate a hopefully unique ID
130 # FIXME: use a real ID
131 0 my $key = Jifty->web->serial . "_" . int(rand(10)) . int(rand(10)) . int(rand(10)) . int(rand(10)) . int(rand(10)) . int(rand(10));
132 0 $self->id($key);
133  
134 # Make sure we don't store any of the connection information
135 0 my $req = $self->request;
136 0 local $req->{env};
137 0 ▶ local $req->{_body_parser}{input_handle} if defined $req->{_body_parser};
Branch: 1 branch
BranchTrueFalseTotal
if defined $req->{"_body_parser"} 0 0 0
138 # We may also need to clean out the top request, if this is a subrequest
139 0 $req = $req->top_request;
140 0 local $req->{env};
141 0 ▶ local $req->{_body_parser}{input_handle} if defined $req->{_body_parser};
Branch: 1 branch
BranchTrueFalseTotal
if defined $req->{"_body_parser"} 0 0 0
142  
143 # Save it into the session
144 0 Jifty->web->session->set_continuation($key => $self);
145  
146 0 return $self;
147 }
148  
149 =head2 return_path_matches
150  
151 Returns true if the continuation matches the current request's path,
152 and it would return to its caller in this context. This can be used
153 to ask "are we about to call a continuation?"
154  
155 =cut
156  
157 0 ▶sub return_path_matches {
Subroutine return_path_matches: not called CC=6 SCAR=37.4
Pod documented
158 0 my $self = shift;
159  
160 0 ▶ return unless Jifty->web->request->path eq $self->request->path
Branch: 1 branch
BranchTrueFalseTotal
unless "Jifty"->web->request->path eq $self->request->path or "Jifty"->web->request->path eq $self->request->uri->canonical->path 0 0 0
Condition0%0/3
"Jifty"->web->request->path eq $self->request->path or "Jifty"->web->request->path eq $self->request->uri->canonical->path
l!l&&r!l&&!r
000
MC/DC0%0/2
"Jifty"->web->request->path eq $self->request->path or "Jifty"->web->request->path eq $self->request->uri->canonical->path
"Jifty"->web->request->path eq $self->request->path "Jifty"->web->request->path eq $self->request->uri->canonical->path
Truth table0%0/3
A or B
A= "Jifty"->web->request->path eq $self->request->pathB= "Jifty"->web->request->path eq $self->request->uri->canonical->path
ABresult
1X1
011
000
161 or Jifty->web->request->path eq $self->request->uri->canonical->path;
162  
163 0 my $args = Jifty->web->request->arguments;
164 0 ▶ return unless scalar keys %{$args} == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$args;} == 1 0 0 0
165  
166 0 ▶ return unless exists $args->{"J:RETURN"} and $args->{"J:RETURN"} eq $self->id;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"J:RETURN"} and $args->{"J:RETURN"} eq $self->id 0 0 0
Condition0%0/3
exists $args->{"J:RETURN"} and $args->{"J:RETURN"} eq $self->id
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args->{"J:RETURN"} and $args->{"J:RETURN"} eq $self->id
exists $args->{"J:RETURN"} $args->{"J:RETURN"} eq $self->id
Truth table0%0/3
A and B
A= exists $args->{"J:RETURN"}B= $args->{"J:RETURN"} eq $self->id
ABresult
0X0
100
111
167  
168 0 return 1;
169 }
170  
171 =head2 call
172  
173 Call the continuation; this is generally done during request
174 processing, after actions have been run.
175 L<Jifty::Request::Mapper>-controlled values are filled into the stored
176 request based on the current request and response. During the
177 process, another continuation is created, with the filled-in results
178 of the current actions included, and the browser is redirected to the
179 proper path, with that continuation.
180  
181 =cut
182  
183 0 ▶sub call {
Subroutine call: not called CC=6 SCAR=37.4
Pod documented
184 0 my $self = shift;
185  
186 0 $self->log->debug("Redirect to @{[$self->request->path]} via continuation");
187 0 ▶ if (Jifty->web->request->argument('_webservice_redirect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty"->web->request->argument("_webservice_redirect")) 0 0 0
188 # for continuation - perform internal redirect under webservices.
189 0 Jifty->web->webservices_redirect($self->request->path);
190 0 return;
191 }
192  
193 # Clone our request
194 0 my $request = $self->request->clone;
195  
196 # Fill in return value(s) into correct part of $request
197 0 $request->do_mapping;
198  
199 0 my $response = $self->response;
200  
201 # If the current response has results, we need to pull them
202 # in. For safety, monikers from the saved continuation
203 # override those from the request prior to the call
204 0 ▶ if (Jifty->web->response->results) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty"->web->response->results) 0 0 0
205 0 $response = dclone(Jifty->web->response);
206 0 my %results = $self->response->results;
207 0 $response->result($_ => $results{$_}) for keys %results;
208 }
209  
210 # Make a new continuation with that request
211 0 my $next = Jifty::Continuation->new(parent => $self->parent,
212 request => $request,
213 response => $response,
214 code => $self->code,
215 );
216 0 ▶ $next->request->continuation(Jifty->web->session->get_continuation($next->parent))
Branch: 1 branch
BranchTrueFalseTotal
if defined $next->parent 0 0 0
217 if defined $next->parent;
218  
219 # Redirect to right page if we're not there already
220 # $next maybe only set path
221 0 ▶ my $path =
Branch: 1 branch
BranchTrueFalseTotal
$next->request->request_uri ? : 0 0 0
222 $next->request->request_uri
223 ? URI->new( $next->request->request_uri )->path
224 : $next->request->path;
225 0 Jifty->web->_redirect($path . "?J:RETURN=" . $next->id);
226 0 return 1;
227 }
228  
229 =head2 return
230  
231 Returns from the continuation by pulling out the stored request, and
232 setting that to be the active request. This shouldn't need to be
233 called by hand -- use L<Jifty::Request/return_from_continuation>,
234 which ensures that all requirements are met before it calls this.
235  
236 =cut
237  
238 0 ▶sub return {
Subroutine return: not called CC=3 SCAR=24.8
Pod documented
239 0 my $self = shift;
240  
241 # Pull state information out of the continuation and set it
242 # up; we use clone so that the continuation itself is
243 # immutable.
244 0 Jifty->web->response(dclone($self->response));
245  
246 # Run any code in the continuation
247 0 ▶ $self->code->(Jifty->web->request)
Branch: 1 branch
BranchTrueFalseTotal
if $self->code 0 0 0
248 if $self->code;
249  
250 # We want to preserve the current actual request environment
251 # (headers, etc)
252 0 my $env = Jifty->web->request->top_request->env;
253 0 my $headers = Jifty->web->request->top_request->headers;
254 # XXX TODO: should we be saving cookies too since they're no longer pulled
255 # from env as of 1d90579c4448ceba113fae1bbdfd515c021b5ae0?
256  
257 # Set the current request to the one in the continuation
258 0 Jifty->web->request($self->request->clone);
259  
260 # Restore the environment we came in with
261 0 Jifty->web->request->top_request->{env} = $env;
262 0 Jifty->web->request->top_request->headers($headers);
263 0 ▶ Jifty->web->request->setup_subrequest_env
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->web->request->is_subrequest 0 0 0
264 if Jifty->web->request->is_subrequest;
265  
266 0 return Jifty->web->request;
267 }
268  
269 =head2 delete
270  
271 Remove the continuation, and any continuations that would return to
272 its scope, from the session.
273  
274 =cut
275  
276 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
277 0 my $self = shift;
278  
279 # Remove all continuations that point to me
280 0 my %continuations = Jifty->web->session->continuations;
281 0 $_->delete for grep {$_->parent eq $self->id} values %continuations;
282  
283 # Finally, remove me from the list of continuations
284 0 Jifty->web->session->remove_continuation($self->id);
285  
286 }
287  
288 =head1 SEE ALSO
289  
290 L<Jifty::Manual::Continuations>
291  
292 =head1 LICENSE
293  
294 Jifty is Copyright 2005-2010 Best Practical Solutions, LLC.
295 Jifty is distributed under the same terms as Perl itself.
296  
297 =cut
298  
299 1 1;
« Jifty/Dispatcher.pm ↑ Summary Jifty/View/Static/Handler.pm »