Jifty/Handler.pm

filter: Statementstmt 23.4% 35 / 149 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 42.3% 11 / 26 Podpod 100% 8 / 8 totaltotal 24.3% 54 / 222 CC 21 SCAR 23.1
CC
21
Cov
19%
handle_request
22.6
psgi_app_static
20.0
psgi_app
20.0
CRAP 10.0
« Jifty/CAS.pm ↑ Summary Jifty/Plugin/SetupWizard/Action/TestDatabaseConnectivity.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Handler;
5  
6 =head1 NAME
7  
8 Jifty::Handler - Methods related to the finding and returning content
9  
10 =head1 SYNOPSIS
11  
12 use Jifty;
13 Jifty->new();
14  
15 my $handler = Jifty::Handler->new;
16 $handler->handle_request( $env );
17  
18 =head1 DESCRIPTION
19  
20 L<Jifty::Handler> provides methods required to find and return content
21 to the browser. L</handle_request>, for instance, is the main entry
22 point for HTTP requests.
23  
24 =cut
25  
26 1 ▶use base qw/Class::Accessor::Fast Jifty::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Jifty::View::Declare::Handler ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Class::Trigger;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use String::BufferStack;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Plack::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 __PACKAGE__->mk_accessors(qw(dispatcher _view_handlers stash buffer));
34  
35 =head2 new
36  
37 Create a new Jifty::Handler object. Generally, Jifty.pm does this only once at startup.
38  
39 =cut
40  
41 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=4.7
Pod documented
42 0 my $class = shift;
43 0 my $self = {};
44 0 bless $self, $class;
45  
46 0 $self->dispatcher( Jifty->app_class( "Dispatcher" ) );
47 0 Jifty::Util->require( $self->dispatcher );
48 0 $self->dispatcher->import_plugins;
49 0 eval { Jifty::Plugin::DumpDispatcher->dump_rules };
50  
51 0 $self->buffer(String::BufferStack->new( out_method => \&Jifty::View::out_method ));
52 {
53 0 my $buffer = $self->buffer;
54 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 0 ▶ *Jifty::Web::out = sub {shift;unshift @_,$buffer;goto \&String::BufferStack::append};
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 }
57 0 return $self;
58 }
59  
60  
61 =head2 view_handlers
62  
63 Returns a list of modules implementing view for your Jifty application.
64  
65 You can override this by specifying:
66  
67 framework:
68 View:
69 Handlers:
70 - Jifty::View::Something::Handler
71 - Jifty::View::SomethingElse::Handler
72  
73  
74 =cut
75  
76 0 ▶sub view_handlers {
Subroutine view_handlers: not called CC=3 SCAR=24.8
Pod documented
77 0 my @default = @{Jifty->config->framework('View')->{'Handlers'}};
78  
79 # If there's a (deprecated) fallback handler, and it's not already
80 # in our set of handlers, tack it on the end
81 0 my $fallback = Jifty->config->framework('View')->{'FallbackHandler'};
82 0 ▶ push @default, $fallback if defined $fallback and not grep {$_ eq $fallback} @default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fallback and not grep({$_ eq $fallback;} @default) 0 0 0
Condition0%0/3
defined $fallback and not grep({$_ eq $fallback;} @default)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fallback and not grep({$_ eq $fallback;} @default)
defined $fallback not grep({$_ eq $fallback;} @default)
Truth table0%0/3
A and not(B)
A= defined $fallbackB= not grep({$_ eq $fallback;} @default)
ABresult
0X0
100
111
83  
84 0 return @default;
85 }
86  
87  
88 =head2 setup_view_handlers
89  
90 Initialize all of our view handlers.
91  
92 =cut
93  
94 0 ▶sub setup_view_handlers {
Subroutine setup_view_handlers: not called CC=3 SCAR=24.8
Pod documented
95 0 my $self = shift;
96  
97 0 ▶ return if $self->_view_handlers;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_view_handlers 0 0 0
98  
99 0 $self->_view_handlers({});
100 0 foreach my $class ($self->view_handlers()) {
101 0 $self->_view_handlers->{$class} = $class->new();
102 }
103 }
104  
105 =head2 view ClassName
106  
107 Returns the Jifty view handler for C<ClassName>.
108  
109 =cut
110  
111 0 ▶sub view {
Subroutine view: not called CC=1 SCAR=6.9
Pod documented
112 0 my $self = shift;
113 0 my $class = shift;
114 0 $self->setup_view_handlers;
115 0 return $self->_view_handlers->{$class};
116 }
117  
118 =head2 psgi_app_static
119  
120 Returns a closure for L<PSGI> application that handles all static
121 content, including plugins.
122  
123 =cut
124  
125 0 ▶sub psgi_app_static {
Subroutine psgi_app_static: not called CC=4 SCAR=30.0
Pod documented
126 0 my $self = shift;
127  
128 # XXX: this is no longer needed, however TestApp-Mason is having a
129 # static::handler-less config
130 0 ▶ my $view_handler = $self->view('Jifty::View::Static::Handler')
Branch: 1 branch
BranchTrueFalseTotal
my $view_handler = $self->view("Jifty::View::Static::Handler") or return 0 0 0
131 or return;;
132  
133 0 require Plack::App::Cascade;
134 0 require Plack::App::File;
135 0 my $static = Plack::App::Cascade->new;
136  
137 0 my $app_class = Jifty->app_class;
138  
139 0 ▶ $static->add( $app_class->psgi_app_static )
Branch: 1 branch
BranchTrueFalseTotal
if $app_class->can("psgi_app_static") 0 0 0
140 if $app_class->can('psgi_app_static');
141  
142 $static->add(
143 Plack::App::File->new(
144 root => Jifty::Util->absolute_path(
145 Jifty->config->framework('Web')->{StaticRoot}
146 )
147 0 )->to_app
148 );
149  
150 0 for ( grep { defined $_ } map { $_->psgi_app_static } Jifty->plugins ) {
151 0 $static->add( $_ );
152 }
153  
154 $static->add( Plack::App::File->new
155 0 ( root => Jifty->config->framework('Web')->{DefaultStaticRoot} )->to_app );
156  
157 # the buffering and unsetting of psgi.streaming is to vivify the
158 # responded res from the $static cascade app.
159 0 ▶ builder {
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 enable 'Plack::Middleware::ConditionalGET';
161 enable
162 0 ▶ sub { my $app = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 ▶ sub { my $env = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
164 0 $env->{'psgi.streaming'} = 0;
165 0 my $res = $app->($env);
166 # skip streamy response
167 0 ▶ return $res unless ref($res) eq 'ARRAY' && $res->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res eq "ARRAY" and $res->[2] 0 0 0
Condition0%0/3
ref $res eq "ARRAY" and $res->[2]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $res eq "ARRAY" and $res->[2]
ref $res eq "ARRAY" $res->[2]
Truth table0%0/3
A and B
A= ref $res eq "ARRAY"B= $res->[2]
ABresult
0X0
100
111
168 0 my $h = Plack::Util::headers($res->[1]);;
169 0 $h->set( 'Cache-Control' => 'max-age=31536000, public' );
170 0 $h->set( 'Expires' => HTTP::Date::time2str( time() + 31536000 ) );
171 0 $res;
172 0 };
173 0 };
174 0 enable 'Plack::Middleware::BufferedStreaming';
175 0 $static;
176 0 };
177 }
178  
179 =head2 psgi_app
180  
181 Returns a closure for L<PSGI> application.
182  
183 =cut
184  
185 0 ▶sub psgi_app {
Subroutine psgi_app: not called CC=4 SCAR=30.0
Pod documented
186 0 my $self = shift;
187  
188 0 ▶ my $app = sub { $self->handle_request(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 my $static = $self->psgi_app_static;
190  
191 0 ▶ $app = builder {
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 mount '/static' => $static;
193 0 mount '/' => $app
194 }
195 0 ▶ if Jifty->config->framework("Web")->{PSGIStatic} && $static;
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->config->framework("Web")->{"PSGIStatic"} and $static 0 0 0
Condition0%0/3
"Jifty"->config->framework("Web")->{"PSGIStatic"} and $static
!ll&&!rl&&r
000
MC/DC0%0/2
"Jifty"->config->framework("Web")->{"PSGIStatic"} and $static
"Jifty"->config->framework("Web")->{"PSGIStatic"} $static
Truth table0%0/3
A and B
A= "Jifty"->config->framework("Web")->{"PSGIStatic"}B= $static
ABresult
0X0
100
111
196  
197 # CAS wrapper
198 0 $app = Jifty::CAS->wrap($app);
199  
200 # allow plugin to wrap $app
201 0 for ( Jifty->plugins ) {
202 0 $app = $_->wrap($app);
203 }
204  
205 0 return $app;
206 }
207  
208 =head2 handle_request
209  
210 When your server process (be it Jifty-internal, FastCGI or anything
211 else) wants to handle a request coming in from the outside world, you
212 should call C<handle_request>.
213  
214 =cut
215  
216 0 ▶sub handle_request {
Subroutine handle_request: not called CC=5 SCAR=31.2
Pod documented
217 0 my ($self, $env) = @_;
218 0 my $req = Plack::Request->new($env);
219 0 my $response;
220  
221 0 $self->setup_view_handlers;
222  
223 0 $self->call_trigger('before_request', $req);
224  
225 # Simple ensure stdout is not writable in next major release
226 1 ▶ use IO::Handle::Util qw(io_prototype io_to_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
227 my $trapio= io_prototype
228 0 ▶ print => sub {
Subroutine __ANON__: not called CC=3 SCAR=19.2
229 1 ▶ use Carp::Clan qw(^(Jifty::Handler|Carp::|IO::Handle::));
Subroutine BEGIN: called CC=1 SCAR=0.0
230 0 carp "printing to STDOUT is deprecated. Use outs, outs_raw or Jifty->web->response->body() instead";
231  
232 0 my $self = shift;
233 0 ▶ my $comma = defined $, ? $, : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $, ? : 0 0 0
234 0 ▶ my $slash = defined $\ ? $\ : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $\ ? : 0 0 0
235 0 Jifty->handler->buffer->out_method->( join($comma, @_) . $slash );
236 0 };
237  
238 0 local *STDOUT = io_to_glob($trapio);
239  
240 # this is scoped deeper because we want to make sure everything is cleaned
241 # up for the LeakDetector plugin. I tried putting the triggers in the
242 # method (Jifty::Server::handle_request) that calls this, but Jifty::Server
243 # isn't being loaded in time
244 {
245 # Build a new stash for the life of this request
246 0 $self->stash( {} );
247 0 local $Jifty::WEB = Jifty::Web->new();
248  
249 0 ▶ if ( Jifty->config->framework('DevelMode') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty"->config->framework("DevelMode")) 0 0 0
250 0 require Module::Refresh;
251 0 Module::Refresh->refresh;
252 0 Jifty::I18N->refresh;
253 }
254  
255 0 Jifty->web->request( Jifty::Request->promote( $req ) );
256 0 Jifty->web->response( Jifty::Response->new );
257  
258 0 $self->call_trigger('have_request');
259  
260 0 Jifty->api->reset;
261 0 for ( Jifty->plugins ) {
262 0 $_->new_request;
263 }
264 0 $self->log->info( Jifty->web->request->method . " request for " . Jifty->web->request->path );
265 0 Jifty->web->setup_session;
266 0 Jifty->web->session->set_cookie;
267  
268 0 Jifty::I18N->get_language_handle;
269  
270 # Return from the continuation if need be
271 0 ▶ unless (Jifty->web->request->return_from_continuation) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Jifty"->web->request->return_from_continuation) 0 0 0
272 0 $self->buffer->out_method(\&Jifty::View::out_method);
273 0 my $ret = $self->dispatcher->handle_request();
274 0 ▶ return $ret if $ret; # if dispatcher returns a coderef,
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
275 # it's a streamy response
276 }
277  
278 0 $self->call_trigger('before_cleanup', $req);
279  
280 0 $self->cleanup_request();
281 0 $response = Jifty->web->response;
282 }
283  
284 0 $self->call_trigger('after_request', $req);
285 0 return $response->finalize;
286 }
287  
288 =head2 cleanup_request
289  
290 Dispatchers should call this at the end of each request, as a class method.
291 It flushes the session to disk, as well as flushing L<Jifty::DBI>'s cache.
292  
293 =cut
294  
295 0 ▶sub cleanup_request {
Subroutine cleanup_request: not called CC=3 SCAR=24.8
Pod documented
296 0 my $self = shift;
297  
298 # Clean out the cache. the performance impact should be marginal.
299 # Consistency is improved, too.
300  
301 0 Jifty->web->session->unload();
302 0 ▶ Jifty::Record->flush_cache if Jifty::Record->can('flush_cache');
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty::Record"->can("flush_cache") 0 0 0
303 0 $self->stash(undef);
304 0 $self->buffer->pop for 1 .. $self->buffer->depth;
305 0 $self->buffer->clear;
306 }
307  
308 1 1;
« Jifty/CAS.pm ↑ Summary Jifty/Plugin/SetupWizard/Action/TestDatabaseConnectivity.pm »