blib/lib/Minima/App.pm

filter: Statementstmt 100% 103 / 103 Branchbran 100% 38 / 38 Conditioncond 100% 18 / 18 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 24 / 24 Podpod 100% 5 / 5 totaltotal 100% 198 / 198 CC 27 SCAR 16.4
CC
27
Cov
100%
run
13.0
_setup_controller
5.0
_load_routes
4.0
CRAP 5.2
« blib/lib/Minima/Controller.pm ↑ Summary blib/lib/Minima/Project.pm »
1 10 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use experimental 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 class Minima::App;
5  
6 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Minima::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use constant DEFAULT_VERSION => 'prototype';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 field $env :param(environment) :reader = undef;
16 10 field $config :param(configuration) :reader = {};
17  
18 field $router = Minima::Router->new;
19  
20 43 ▶ADJUST {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 43 $self->_read_config;
22 }
23  
24 8 ▶method set_env ($e) { $env = $e }
Subroutine set_env: called CC=1 SCAR=0.0
Pod documented
25 5 ▶method set_config ($c) { $config = $c; $self->_read_config }
Subroutine set_config: called CC=1 SCAR=0.0
Pod documented
26  
27 method development
28 14 ▶{
Subroutine development: called CC=2 SCAR=6.9
Pod documented
29 14 ▶ return 1 if not defined $ENV{PLACK_ENV};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"PLACK_ENV"} 6 8 14
30  
31 8 $ENV{PLACK_ENV} eq 'development'
32 }
33  
34 100 ▶method path ($p)
Subroutine path: called CC=1 SCAR=0.0
Pod documented
35 {
36 100 Path::Tiny::path($p)->absolute($config->{base_dir})->stringify;
37 }
38  
39 method run
40 26 ▶{
Subroutine run: called CC=13 SCAR=25.6
Pod documented
41 26 ▶ croak "Can't run without an environment.\n" unless defined $env;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env 1 25 26
42  
43 25 my $match = $router->match($env);
44 25 my $response;
45 my $controller;
46 25 my $action;
47  
48 25 ▶ return $self->_not_found unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 2 23 25
49 23 ▶ return $self->_redirect($match) if $match->{redirect};
Branch: 1 branch
BranchTrueFalseTotal
if $match->{"redirect"} 2 21 23
50  
51 21 try {
52 21 ($controller, $action) = $self->_setup_controller($match);
53  
54 # before_action
55 14 ▶ if ($controller->can('before_action')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($controller->can("before_action")) 9 5 14
56 9 my $res = $controller->before_action($action);
57 9 ▶ return $res if $res; # halt
Branch: 1 branch
BranchTrueFalseTotal
if $res 1 8 9
58 }
59 # actual action
60 13 $response = $controller->$action;
61 # after action
62 11 ▶ if ($controller->can('after_action')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($controller->can("after_action")) 8 3 11
63 8 $controller->after_action($response);
64 }
65 } catch ($e) {
66 9 my $err = $router->error_route;
67 # Something failed. If we're in production
68 # and there is a server_error route, try it.
69 9 ▶ if (!$self->development && $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->development and $err) { } 1 8 9
Condition100%3/3
not $self->development and $err
!ll&&!rl&&r
531
MC/DC100%2/2
not $self->development and $err
not $self->development $err
Truth table100%3/3
not(A) and B
A= not $self->developmentB= $err
ABresult
0X0
100
111
70 1 ($controller, $action) = $self->_setup_controller($err);
71 1 $response = $controller->$action($e);
72 } else {
73 # Nothing can be done, re-throw
74 8 die $e;
75 }
76 }
77  
78 # Delete body on HEAD requests
79 12 ▶ my $auto_head = $config->{automatic_head} // 1;
Condition100%2/2
$config->{"automatic_head"} // 1
l!l
111
MC/DC100%1/1
$config->{"automatic_head"} // 1
$config->{"automatic_head"}
Truth table100%2/2
A
A= $config->{"automatic_head"}
Aresult
11
00
80 12 ▶ if ( $auto_head
Branch: 1 branch
BranchTrueFalseTotal
if ($auto_head and length $env->{"REQUEST_METHOD"} and $env->{"REQUEST_METHOD"} eq "HEAD") 2 10 12
Condition100%6/6
$auto_head and length $env->{"REQUEST_METHOD"} and $env->{"REQUEST_METHOD"} eq "HEAD"
!ll&&!rl&&r
642
$auto_head and length $env->{"REQUEST_METHOD"}
!ll&&!rl&&r
156
MC/DC100%3/3
$auto_head and length $env->{"REQUEST_METHOD"} and $env->{"REQUEST_METHOD"} eq "HEAD"
$auto_head length $env->{"REQUEST_METHOD"} $env->{"REQUEST_METHOD"} eq "HEAD"
Truth table100%4/4
A and B and C
A= $auto_headB= length $env->{"REQUEST_METHOD"}C= $env->{"REQUEST_METHOD"} eq "HEAD"
ABCresult
0XX0
10X0
1100
1111
81 && length $env->{REQUEST_METHOD}
82 && $env->{REQUEST_METHOD} eq 'HEAD'
83 ) {
84 2 ▶ return Plack::Util::response_cb($response, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
85 2 my $res = shift;
86 2 ▶ if ($res->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[2]) { } 1 1 2
87 1 $res->[2] = [];
88 } else {
89 1 ▶ return sub { defined $_[0] ? '' : undef };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 1 2
90 }
91 2 });
92 }
93  
94 10 return $response;
95 }
96  
97 23 ▶method _setup_controller ($match)
Subroutine _setup_controller: called CC=5 SCAR=16.1
98 {
99 23 my $class = $match->{controller};
100 23 my $method = $match->{action};
101  
102 23 ▶ die "Route dispatch failed: "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 1 22 23
103 . "no controller defined for `$env->{PATH_INFO}`.\n"
104 unless defined $class;
105  
106 22 ▶ die "Route dispatch failed: no action configured for `$class`.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method and length $method 2 20 22
Condition100%3/3
defined $method and length $method
!ll&&!rl&&r
1120
MC/DC100%2/2
defined $method and length $method
defined $method length $method
Truth table100%3/3
A and B
A= defined $methodB= length $method
ABresult
0X0
100
111
107 unless defined $method && length $method;
108  
109 20 $self->_load_class($class);
110  
111 16 my $controller = $class->new(
112 app => $self,
113 route => $match,
114 );
115  
116 16 ▶ die "Route dispatch failed: `$class` does not implement action `$method`.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $controller->can($method) 1 15 16
117 unless $controller->can($method);
118  
119 15 ( $controller, $method );
120 }
121  
122 method _not_found
123 2 ▶{
Subroutine _not_found: called CC=1 SCAR=0.0
124 [
125 2 404,
126 [ 'Content-Type' => 'text/plain' ],
127 [ "not found\n" ]
128 ]
129 }
130  
131 26 ▶method _load_class ($class)
Subroutine _load_class: called CC=1 SCAR=0.0
132 {
133 26 try {
134 26 my $file = $class;
135 26 $file =~ s|::|/|g;
136 26 require "$file.pm";
137 } catch ($e) {
138 6 croak "Could not load `$class`: $e\n";
139 }
140 }
141  
142 method _load_routes
143 50 ▶{
Subroutine _load_routes: called CC=4 SCAR=13.9
144 50 $router->clear_routes;
145  
146 50 my $file = $config->{routes};
147 50 ▶ unless (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 48 2 50
148 # No file passed. Attempt the default route.
149 48 $file = $self->path('etc/routes.map');
150 # If it does not exist, setup a basic route
151 # for the default controller only.
152 48 ▶ unless (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 21 27 48
153 21 $router->_connect(
154 '/',
155 {
156 controller => 'Minima::Controller',
157 action => 'hello',
158 },
159 );
160 21 return;
161 }
162 }
163  
164 # Controller prefix
165 29 my $prefix = $config->{controller_prefix};
166 29 ▶ $router->set_prefix($prefix) if defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix 1 28 29
167  
168 # Read routes
169 29 $file = $self->path($file);
170 29 $router->read_file($file);
171 }
172  
173 2 ▶method _redirect ($m)
Subroutine _redirect: called CC=1 SCAR=0.0
174 {
175 2 my $res = Plack::Response->new;
176  
177 2 $res->redirect($m->{redirect}, $m->{redirect_status});
178 2 $res->finalize;
179 }
180  
181 method _read_config
182 48 ▶{
Subroutine _read_config: called CC=2 SCAR=6.9
183 # Ensure base_dir is set and absolute
184 48 ▶ my $base = $config->{base_dir} // '.';
Condition100%2/2
$config->{"base_dir"} // "."
l!l
1830
MC/DC100%1/1
$config->{"base_dir"} // "."
$config->{"base_dir"}
Truth table100%2/2
A
A= $config->{"base_dir"}
Aresult
11
00
185 48 $config->{base_dir} = Path::Tiny::path($base)->absolute;
186  
187 48 $self->_load_routes;
188 47 $self->_set_version;
189 }
190  
191 method _set_version
192 47 ▶{
Subroutine _set_version: called CC=4 SCAR=13.9
193 47 ▶ return if defined $config->{VERSION};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"VERSION"} 4 43 47
194  
195 43 ▶ if (defined $config->{version_from}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{'version_from'}) { } 3 40 43
196 3 my $class = $config->{version_from};
197 3 try {
198 3 $self->_load_class($class);
199 } catch ($e) {
200 1 croak "Failed to load version from class.\n$e\n";
201 }
202 2 ▶ $config->{VERSION} = $class->VERSION // DEFAULT_VERSION;
Condition100%2/2
$class->VERSION // "prototype"
l!l
11
MC/DC100%1/1
$class->VERSION // "prototype"
$class->VERSION
Truth table100%2/2
A
A= $class->VERSION
Aresult
11
00
203 } else {
204 40 $config->{VERSION} = DEFAULT_VERSION;
205 }
206 }
207  
208 __END__
« blib/lib/Minima/Controller.pm ↑ Summary blib/lib/Minima/Project.pm »