Pickles/Context.pm

filter: Statementstmt 84.9% 158 / 186 Branchbran 58.8% 20 / 34 Conditioncond 55.0% 11 / 20 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 81.4% 35 / 43 Podpod 46.1% 12 / 26 totaltotal 73.9% 238 / 322 CC 26 SCAR 10.7
CC
26
Cov
79%
redirect
12.0
register
6.0
__ANON__
6.0
CRAP 2.9
« Pickles/Dispatcher.pm ↑ Summary Pickles/Container.pm »
1 package Pickles::Context;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use base qw(Class::Data::Inheritable);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Plack::Util::Accessor qw(env stash finished controller config dispatcher container);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Class::Trigger qw(init pre_dispatch post_dispatch pre_render post_render pre_finalize post_finalize);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use String::CamelCase qw(camelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use Pickles::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Pickles::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 __PACKAGE__->mk_classdata(__components => {});
16 10 __PACKAGE__->mk_classdata(__plugins => {});
17  
18 10 __PACKAGE__->mk_classdata(request_class => '+Pickles::Request');
19 10 __PACKAGE__->mk_classdata(response_class => '+Pickles::Response');
20 10 __PACKAGE__->mk_classdata(view_class => 'View');
21  
22 # shortcut for container.
23 0 ▶sub register {
Subroutine register: not called CC=2 SCAR=17.9
Pod documented
24 0 my $self = shift;
25 0 ▶ Carp::croak( $self. '->register is deprecated. Use container profile file instead. See Pickles::Container for detail.' ) unless blessed( $self );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 0 0
26 0 $self->container->register( @_ );
27 }
28  
29 6 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
30 6 my $self = shift;
31 6 $self->container->get( @_ );
32 }
33  
34 2 ▶sub load_plugins {
Subroutine load_plugins: called CC=2 SCAR=6.9
Pod documented
35 2 my( $class, @plugins ) = @_;
36 2 for my $plugin( @plugins ) {
37 2 my $plugin_class = Plack::Util::load_class( $plugin, 'Pickles::Plugin' );
38 2 $plugin_class->install( $class );
39 2 $class->__plugins->{$plugin} = $plugin_class;
40 }
41 }
42  
43 0 ▶sub plugins {
Subroutine plugins: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my $class = shift;
45 0 values %{$class->__plugins};
46 }
47  
48 0 ▶sub has_plugin {
Subroutine has_plugin: not called CC=1 SCAR=6.9
Pod undocumented
49 0 my( $pkg, $name ) = @_;
50 0 $pkg->__plugins->{$name};
51 }
52  
53 1 ▶sub add_method {
Subroutine add_method: called CC=1 SCAR=0.0
Pod undocumented
54 1 my( $pkg, $method, $code ) = @_;
55 {
56 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 *{"$pkg\::$method"} = $code;
58 }
59 }
60  
61 14 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod undocumented
62 14 my( $self, $component ) = @_;
63 14 my $loaded =
64 Plack::Util::load_class( $self->$component(), $self->appname );
65 14 $loaded;
66 }
67  
68 8 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
69 8 my $class = shift;
70 8 my %args;
71 8 ▶ if ( @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 3 5 8
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
503
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
72 3 $args{env} = $_[0];
73 }
74 else {
75 5 %args = @_;
76 }
77 8 ▶ Carp::croak(q{$env is required}) unless $args{env};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"env"} 0 8 8
78 8 my $self = bless {
79 controller => undef,
80 stash => +{},
81 finished => 0,
82 %args,
83 }, $class;
84 8 $self->call_trigger('init');
85 8 $self;
86 }
87  
88 21 ▶sub appname {
Subroutine appname: called CC=2 SCAR=7.1
Pod documented
89 21 my $self = shift;
90 21 ▶ my $class = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 21 0 21
91 21 Pickles::Util::appname( $class );
92 }
93  
94 36 ▶sub request {
Subroutine request: called CC=2 SCAR=7.0
Pod documented
95 36 my $self = shift;
96 36 ▶ $self->{_request} ||= do {
Condition66%2/3
$self->{"_request"} ||= do { my $class = $self->load("request_class"); $class->new($self->env) }
l!l&&r!l&&!r
2880
MC/DC0%0/2
$self->{"_request"} ||= do { my $class = $self->load("request_class"); $class->new($self->env) }
$self->{"_request"} do { my $class = $self->load("request_class"); $class->new($self->env) }
Truth table66%2/3
A ||= B
A= $self->{"_request"}B= do { my $class = $self->load("request_class"); $class->new($self->env) }
ABresult
1X1
011
000
97 8 my $class = $self->load('request_class');
98 8 $class->new( $self->env );
99 };
100 }
101 36 ▶sub req { shift->request(@_); }
Subroutine req: called CC=1 SCAR=0.0
Pod documented
102  
103 29 ▶sub response {
Subroutine response: called CC=2 SCAR=7.0
Pod documented
104 29 my $self = shift;
105 29 ▶ $self->{_response} ||= do {
Condition66%2/3
$self->{"_response"} ||= do { my $class = $self->load("response_class"); $class->new(200) }
l!l&&r!l&&!r
2540
MC/DC0%0/2
$self->{"_response"} ||= do { my $class = $self->load("response_class"); $class->new(200) }
$self->{"_response"} do { my $class = $self->load("response_class"); $class->new(200) }
Truth table66%2/3
A ||= B
A= $self->{"_response"}B= do { my $class = $self->load("response_class"); $class->new(200) }
ABresult
1X1
011
000
106 4 my $class = $self->load('response_class');
107 4 $class->new( 200 );
108 };
109 }
110 29 ▶sub res { shift->response(@_); }
Subroutine res: called CC=1 SCAR=0.0
Pod documented
111  
112 16 ▶sub match {
Subroutine match: called CC=2 SCAR=7.0
Pod undocumented
113 16 my $self = shift;
114 16 my $dispatcher = $self->dispatcher;
115 16 ▶ $self->{_match} ||= $dispatcher->match( $self->req );
Condition66%2/3
$self->{"_match"} ||= $dispatcher->match($self->req)
l!l&&r!l&&!r
1240
MC/DC0%0/2
$self->{"_match"} ||= $dispatcher->match($self->req)
$self->{"_match"} $dispatcher->match($self->req)
Truth table66%2/3
A ||= B
A= $self->{"_match"}B= $dispatcher->match($self->req)
ABresult
1X1
011
000
116 }
117  
118 2 ▶sub render {
Subroutine render: called CC=3 SCAR=11.1
Pod documented
119 2 my( $self, $view_class ) = @_;
120 2 ▶ if ( $view_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($view_class) { } 0 2 2
121 0 $view_class = Plack::Util::load_class( $view_class, $self->appname );
122 }
123 else {
124 2 $view_class = $self->load('view_class');
125 }
126 2 my $view;
127 2 ▶ try { $view = $self->__components->{"$view_class"} };
Subroutine __ANON__: called CC=1 SCAR=0.0
128 2 ▶ if (! $view) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($view) 2 0 2
129 2 $self->__components->{"$view_class"} = ($view = $view_class->new);
130 }
131 2 $self->res->content_type( $view->content_type );
132 2 my $body = $view->render( $self );
133 2 $self->res->body( $body );
134 2 $self->finished(1);
135 }
136  
137 4 ▶sub dispatch {
Subroutine dispatch: called CC=5 SCAR=17.0
Pod undocumented
138 4 my $self = shift;
139  
140 4 my $guard = $self->container->new_scope;
141  
142 4 $self->_prepare;
143 4 my $controller_class = $self->controller_class;
144 4 my $action = $self->action;
145 4 ▶ unless ( $controller_class && $self->validate_action( $action ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($controller_class and $self->validate_action($action)) 0 4 4
Condition33%1/3
$controller_class and $self->validate_action($action)
!ll&&!rl&&r
004
MC/DC0%0/2
$controller_class and $self->validate_action($action)
$controller_class $self->validate_action($action)
Truth table33%1/3
A and B
A= $controller_classB= $self->validate_action($action)
ABresult
0X0
100
111
146 0 $self->handle_not_found;
147 0 return $self->finalize;
148 }
149 4 my $controller;
150 4 ▶ try { $controller = $self->__components->{"$controller_class"} };
Subroutine __ANON__: called CC=1 SCAR=0.0
151 4 ▶ if (! $controller) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($controller) 3 1 4
152 3 $self->__components->{"$controller_class"} = ($controller = $controller_class->new);
153 }
154 4 $controller->init( $self );
155 4 $self->controller( $controller );
156  
157 4 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 4 $self->call_trigger('pre_dispatch');
159 4 $controller->execute( $action, $self );
160 4 $self->call_trigger('post_dispatch');
161 }
162 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
163 0 ▶ unless (/^PICKLES_EXCEPTION_ABORT/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^PICKLES_EXCEPTION_ABORT/) 0 0 0
164 0 local $SIG{__DIE__} = 'DEFAULT';
165 0 die $_;
166 }
167 4 };
168 4 ▶ unless ( $self->finished ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->finished) 2 2 4
169 2 $self->call_trigger('pre_render');
170 2 $self->render;
171 2 $self->call_trigger('post_render');
172 }
173 4 return $self->finalize;
174 }
175  
176 0 ▶sub abort { die 'PICKLES_EXCEPTION_ABORT'; }
Subroutine abort: not called CC=1 SCAR=6.9
Pod documented
177  
178 4 ▶sub _prepare {
Subroutine _prepare: called CC=2 SCAR=6.9
179 4 my $self = shift;
180 4 my $path = $self->req->path_info;
181 4 ▶ $path .= 'index' if $path =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[/$] 2 2 4
182 4 $path =~ s{^/}{};
183 4 $self->stash->{'VIEW_TEMPLATE'} = $path;
184 }
185  
186 4 ▶sub finalize {
Subroutine finalize: called CC=1 SCAR=0.0
Pod undocumented
187 4 my $self = shift;
188 4 $self->call_trigger('pre_finalize');
189 4 my $result = $self->res->finalize;
190 4 $self->call_trigger('post_finalize');
191 4 $result;
192 }
193  
194 5 ▶sub uri_for {
Subroutine uri_for: called CC=4 SCAR=13.9
Pod documented
195 5 my( $self, @args ) = @_;
196 # Plack::App::URLMap
197 5 my $req = $self->req;
198 5 my $uri = $req->base;
199 5 ▶ my $params =
Branch: 1 branch
BranchTrueFalseTotal
scalar @args && ref $args[$#args] eq 'HASH' ? : 1 4 5
Condition66%2/3
scalar @args and ref $args[$#args] eq "HASH"
!ll&&!rl&&r
041
MC/DC50%1/2
scalar @args and ref $args[$#args] eq "HASH"
scalar @args ref $args[$#args] eq "HASH"
Truth table66%2/3
A and B
A= scalar @argsB= ref $args[$#args] eq "HASH"
ABresult
0X0
100
111
200 ( scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {} );
201 5 my @path = split '/', $uri->path;
202 5 ▶ unless ( $args[0] =~ m{^/} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args[0] =~ m[^/]) 2 3 5
203 2 push @path, split( '/', $self->req->path_info );
204 }
205 5 push @path, @args;
206 5 my $path = join '/', @path;
207 5 $path =~ s|/{2,}|/|g; # xx////xx -> xx/xx
208 5 $uri->path( $path );
209 5 $uri->query_form( $params );
210 5 $uri;
211 }
212  
213 0 ▶sub handle_not_found {
Subroutine handle_not_found: not called CC=1 SCAR=6.9
Pod undocumented
214 0 my $self = shift;
215 0 $self->res->status( 404 );
216 0 $self->not_found;
217 0 $self->finished(1);
218 }
219  
220 0 ▶sub not_found {
Subroutine not_found: not called CC=1 SCAR=6.9
Pod undocumented
221 0 my $self = shift;
222 0 $self->res->content_type('text/html');
223 0 $self->res->body(<<'HTML');
224 <html>
225 <head>
226 <title>404</title>
227 </head>
228 <body>
229 <h1>404 File Not Found</h1>
230 </body>
231 </html>
232 HTML
233 }
234  
235 0 ▶sub redirect {
Subroutine redirect: not called CC=3 SCAR=24.8
Pod documented
236 0 my( $self, $url, $code ) = @_;
237 0 ▶ $code ||= 302;
Condition0%0/2
$code ||= 302
l!l
00
MC/DC0%0/1
$code ||= 302
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
238 0 $self->res->status( $code );
239 0 ▶ $url = ($url =~ m{^https?://}) ? $url : $self->uri_for( $url );
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[^https?://] ? : 0 0 0
240 0 $self->res->headers->header(Location => $url);
241 0 $self->finished(1);
242 }
243  
244 4 ▶sub controller_class {
Subroutine controller_class: called CC=2 SCAR=7.0
Pod undocumented
245 4 my $self = shift;
246 4 my $match = $self->match;
247 4 my $controller = $match->{controller};
248 4 ▶ return unless $controller;
Branch: 1 branch
BranchTrueFalseTotal
unless $controller 0 4 4
249 4 my $class = Plack::Util::load_class(
250 'Controller::'. camelize( $controller ),
251 $self->appname
252 );
253 4 $class;
254 }
255  
256 my %_reserved_actions =
257 100 map { $_ => 1 }
258 10 grep { defined &{"Pickles::Controller::$_"} }
259 keys %{Pickles::Controller::};
260  
261 4 ▶sub validate_action {
Subroutine validate_action: called CC=3 SCAR=11.4
Pod undocumented
262 4 my( $self, $action ) = @_;
263 4 ▶ return unless defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 0 4 4
264 4 ▶ return if $_reserved_actions{$action};
Branch: 1 branch
BranchTrueFalseTotal
if $_reserved_actions{$action} 0 4 4
265 4 return $action =~ m{^[a-z][a-zA-Z0-9_]*$};
266 }
267  
268 4 ▶sub action {
Subroutine action: called CC=1 SCAR=0.0
Pod undocumented
269 4 my $self = shift;
270 4 my $match = $self->match;
271 4 $match->{action};
272 }
273  
274 8 ▶sub args {
Subroutine args: called CC=1 SCAR=0.0
Pod undocumented
275 8 my $self = shift;
276 8 my $match = $self->match;
277 8 $match->{args};
278 }
279  
280 10 1;
281  
282 __END__
« Pickles/Dispatcher.pm ↑ Summary Pickles/Container.pm »