Yeb/Application.pm

filter: Statementstmt 69.0% 165 / 239 Branchbran 50.0% 28 / 56 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 63.5% 47 / 74 Podpod 0.0% 0 / 18 totaltotal 61.7% 244 / 395 CC 35 SCAR 14.8
CC
35
Cov
65%
__ANON__
30.0
call
12.0
__ANON__
12.0
CRAP 4.4
↑ Summary Yeb/Plugin/Static.pm »
1 package Yeb::Application;
2 2 our $AUTHORITY = 'cpan:GETTY';
3 # ABSTRACT: Main Meta Class for a Yeb Application
4 2 $Yeb::Application::VERSION = '0.104';
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Yeb::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Yeb::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Plack::Middleware::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use List::Util qw( reduce );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Hash::Merge qw( merge );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use URL::Encode qw( url_encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use Web::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 my $first_yep_application;
24  
25 2 has class => (
26 is => 'ro',
27 required => 1,
28 );
29  
30 2 has app => (
31 is => 'rw',
32 );
33  
34 has first => (
35 is => 'ro',
36 lazy => 1,
37 3 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
38 3 my ( $self ) = @_;
39 3 ▶ $first_yep_application->class eq $self->class ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$first_yep_application->class eq $self->class ? : 2 1 3
40 },
41 2 );
42  
43 2 has args => (
44 is => 'ro',
45 predicate => 1,
46 );
47  
48 has config => (
49 is => 'ro',
50 lazy => 1,
51 0 ▶ builder => sub {{}},
Subroutine _build_config: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 2 );
53  
54 has root => (
55 is => 'ro',
56 lazy => 1,
57 3 ▶ builder => sub {
Subroutine _build_root: called CC=2 SCAR=6.9
58 defined $ENV{YEB_ROOT}
59 ? path($ENV{YEB_ROOT})
60 3 ▶ : path(".")
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'YEB_ROOT'} ? : 2 1 3
61 },
62 2 );
63  
64 has current_dir => (
65 is => 'ro',
66 lazy => 1,
67 3 ▶ builder => sub { path(".") },
Subroutine _build_current_dir: called CC=1 SCAR=0.0
68 2 );
69  
70 has debug => (
71 is => 'ro',
72 lazy => 1,
73 3 ▶ builder => sub {
Subroutine __ANON__: called CC=4 SCAR=14.9
74 3 my ( $self ) = @_;
75 3 ▶ return 0 unless $self->first;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->first 1 2 3
76 2 ▶ return $ENV{YEB_TRACE} || $ENV{YEB_DEBUG} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'YEB_TRACE'} || $ENV{'YEB_DEBUG'} ? : 0 2 2
Condition33%1/3
$ENV{"YEB_TRACE"} or $ENV{"YEB_DEBUG"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$ENV{"YEB_TRACE"} or $ENV{"YEB_DEBUG"}
$ENV{"YEB_TRACE"} $ENV{"YEB_DEBUG"}
Truth table33%1/3
A or B
A= $ENV{"YEB_TRACE"}B= $ENV{"YEB_DEBUG"}
ABresult
1X1
011
000
77 },
78 2 );
79  
80 has package_stash => (
81 is => 'ro',
82 lazy => 1,
83 3 ▶ builder => sub { Package::Stash->new(shift->class) },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 2 );
85  
86 has yebs => (
87 is => 'ro',
88 lazy => 1,
89 3 ▶ builder => sub {{}},
Subroutine _build_yebs: called CC=1 SCAR=0.0
90 2 );
91 14 ▶sub y {
Subroutine y: called CC=1 SCAR=0.0
Pod undocumented
92 14 my ( $self, $yeb ) = @_;
93 14 $self->yebs->{$yeb};
94 }
95 17 ▶sub y_main {
Subroutine y_main: called CC=1 SCAR=0.0
Pod undocumented
96 17 my ( $self ) = @_;
97 17 $self->yebs->{$self->class};
98 }
99  
100 has functions => (
101 is => 'ro',
102 lazy => 1,
103 3 ▶ builder => sub {{}},
Subroutine _build_functions: called CC=1 SCAR=0.0
104 2 );
105  
106 has plugins => (
107 is => 'ro',
108 lazy => 1,
109 1 ▶ builder => sub {[]},
Subroutine _build_plugins: called CC=1 SCAR=0.0
110 2 );
111  
112 has yeb_functions => (
113 is => 'ro',
114 lazy => 1,
115 3 ▶ builder => sub {
Subroutine _build_yeb_functions: called CC=1 SCAR=3.9
116 3 my ( $self ) = @_;
117 {
118 5 ▶ yeb => sub { $self },
Subroutine __ANON__: called CC=1 SCAR=0.0
119  
120 0 ▶ app => sub { $self->app },
Subroutine __ANON__: not called CC=1 SCAR=6.9
121  
122 5 ▶ chain => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 5 my $class = $self->class_loader(shift);
124 5 return $class->yeb->y($class)->chain;
125 },
126 1 ▶ load => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
127 1 my $class = $self->class_loader(shift);
128 1 return;
129 },
130  
131 0 ▶ cfg => sub { $self->config },
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 2 ▶ root => sub { path($self->root,@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
133 0 ▶ cur => sub { path($self->current_dir,@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 ▶ hostname => sub { hostname() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135  
136 0 ▶ cc => sub { $self->cc },
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 0 ▶ env => sub { $self->hash_accessor($self->cc->env,@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 ▶ req => sub { $self->cc->request },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 ▶ uri_for => sub { $self->cc->uri_for(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 21 ▶ st => sub { $self->hash_accessor_empty($self->cc->stash,@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
141 0 ▶ st_has => sub { $self->hash_accessor_has($self->cc->stash,@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 8 ▶ ex => sub { $self->hash_accessor_empty($self->cc->export,@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
143 0 ▶ ex_has => sub { $self->hash_accessor_has($self->cc->export,@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 0 ▶ pa => sub { $self->hash_accessor_empty($self->cc->request->parameters,@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 ▶ pa_has => sub { $self->hash_accessor_has($self->cc->request->parameters,@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
146  
147 0 ▶ url => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
148 0 my @parts = $self->flat([@_]);
149 0 my ( @path_parts, @hashs );
150 0 for (@parts) {
151 0 ▶ if (ref $_ eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 0 0 0
152 0 push @hashs, $_;
153 } else {
154 0 push @path_parts, $_;
155 }
156 }
157 0 my $url = $self->cc->uri_base;
158 0 ▶ if (@path_parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path_parts) 0 0 0
159 0 $url .= join("/",map { url_encode_utf8($_) } @path_parts);
160 }
161 0 ▶ if (@hashs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hashs) 0 0 0
162 0 $url .= '?';
163 0 my $gets = $self->merge_hashs(reverse @hashs);
164 0 $url .= join("&",map { $_.'='.url_encode_utf8($gets->{$_}) } keys %{$gets});
165 }
166 0 return $url;
167 },
168  
169 9 ▶ text => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
170 9 $self->cc->content_type('text/plain');
171 9 $self->cc->body(join("\n",@_));
172 9 $self->cc->response;
173 },
174  
175 0 ▶ redirect => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
176 0 my ( $target, $code ) = shift;
177 0 ▶ $code = 307 unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 0 0
178 0 $self->cc->content_type('text/html');
179 0 $self->cc->header->{location} = $target;
180 0 $self->cc->body(<<"__REDIRECT__");
181 <html><head><title>Moved</title><meta http-equiv="refresh" content="0; url=$target"></head>
182 <body><h1>Moved</h1><p>This page has moved to <a href="$target">$target</a>.</p></body></html>
183 __REDIRECT__
184 0 $self->cc->response;
185 },
186  
187 0 ▶ html_body => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 $self->cc->content_type('text/html');
189 0 $self->cc->body('<html><body>'.join(" ",@_).'</body></html>');
190 0 $self->cc->response;
191 },
192 }
193 3 },
194 2 );
195  
196 0 ▶sub call {
Subroutine call: not called CC=3 SCAR=24.8
Pod undocumented
197 0 my ( $self, $func, @args ) = @_;
198 0 ▶ return $self->functions->{$func}->(@_) if defined $self->functions->{$func};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->functions->{$func} 0 0 0
199 0 ▶ return $self->yeb_functions->{$func}->(@_) if defined $self->yeb_functions->{$func};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->yeb_functions->{$func} 0 0 0
200 0 croak "Unknown function ".$func." inside ".(ref $self)." application";
201 }
202  
203 6 ▶sub class_loader {
Subroutine class_loader: called CC=2 SCAR=6.9
Pod undocumented
204 6 my ( $self, $class ) = @_;
205 6 ▶ if ($class =~ m/^\+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^\+/) { } 2 4 6
206 2 $class =~ s/^(\+)//;
207 } else {
208 4 $class = $self->class.'::'.$class;
209 }
210 6 use_module($class);
211 6 return $class;
212 }
213  
214 29 ▶sub hash_accessor_empty {
Subroutine hash_accessor_empty: called CC=2 SCAR=6.9
Pod undocumented
215 29 my ( $self, @hash_and_args ) = @_;
216 29 my $value = $self->hash_accessor(@hash_and_args);
217 29 ▶ return defined $value ? $value : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 27 2 29
218 }
219  
220 0 ▶sub hash_accessor_has {
Subroutine hash_accessor_has: not called CC=2 SCAR=17.9
Pod undocumented
221 0 my ( $self, @hash_and_args ) = @_;
222 0 my $value = $self->hash_accessor(@hash_and_args);
223 0 ▶ return defined $value ? 1 : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
224 }
225  
226 29 ▶sub hash_accessor {
Subroutine hash_accessor: called CC=5 SCAR=16.1
Pod undocumented
227 29 my ( $self, $hash, $key, $value ) = @_;
228 29 ▶ return $hash unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 29 29
229 29 ▶ my @args = ref $key eq 'ARRAY' ? @{$key} : ($key);
Branch: 1 branch
BranchTrueFalseTotal
ref $key eq 'ARRAY' ? : 15 14 29
230 29 my $last_key = shift @args;
231 29 my $last;
232 29 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 10 19 29
233 10 ▶ $last = reduce { $a->{$b}||={} } ($hash, @args);
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$Yeb::Application::a->{$b} ||= {}
l!l
46
MC/DC100%1/1
$Yeb::Application::a->{$b} ||= {}
$Yeb::Application::a->{$b}
Truth table100%2/2
A
A= $Yeb::Application::a->{$b}
Aresult
11
00
234 } else {
235 19 $last = $hash;
236 }
237 29 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 16 13 29
238 16 return $last->{$last_key} = $value;
239 } else {
240 13 return $last->{$last_key};
241 }
242 }
243  
244 1 ▶sub add_plugin {
Subroutine add_plugin: called CC=2 SCAR=7.1
Pod undocumented
245 1 my ( $self, $source, $plugin, %args ) = @_;
246 1 my $class;
247 1 ▶ if ($plugin =~ m/^\+(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin =~ /^\+(.+)/) { } 0 1 1
248 0 $class = $1;
249 } else {
250 1 $class = 'Yeb::Plugin::'.$plugin;
251 }
252 1 use_module($class);
253 1 my $obj = $class->new( app => $self, class => $self->y($source) , %args );
254 1 push @{$self->plugins}, $obj;
255 }
256  
257 0 ▶sub add_middleware {
Subroutine add_middleware: not called CC=1 SCAR=6.9
Pod undocumented
258 0 my ( $self, $middleware ) = @_;
259 0 ▶ $self->y_main->prepend_to_chain( "" => sub { $middleware } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 }
261  
262 3 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=18.6
Pod undocumented
263 3 my ( $self ) = @_;
264  
265 3 ▶ $first_yep_application = $self unless defined $first_yep_application;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_yep_application 2 1 3
266  
267 3 $self->root;
268 3 $self->current_dir;
269  
270 3 $self->package_stash->add_symbol('$yeb',\$self);
271
272 3 Web::Simple->import::into($self->class);
273  
274 0 ▶ $self->package_stash->add_symbol('&register_has',sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
275 0 my ( $attr, @args ) = @_;
276 0 ▶ my @attrs = ref $attr eq 'ARRAY' ? @{$attr} : ($attr);
Branch: 1 branch
BranchTrueFalseTotal
ref $attr eq 'ARRAY' ? : 0 0 0
277 0 $self->register_function($_, $self->class->can($_)) for @attrs;
278 0 $self->class->can('has')->($attr, @args);
279 3 });
280  
281 0 ▶ $self->package_stash->add_symbol('&register_function',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
282 0 $self->register_function(@_);
283 3 });
284
285 16 ▶ $self->package_stash->add_symbol('&dispatch_request',sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
286 16 my ( $app, $env ) = @_;
287 16 $self->app($app);
288 16 $self->reset_context;
289 16 $self->set_cc(Yeb::Context->new( env => $env ));
290 return $self->y_main->chain,
291 2 ▶ '/...' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
292 2 $self->cc->status(500);
293 2 $self->cc->response;
294 16 };
295 3 });
296  
297 3 $self->yeb_import($self->class);
298  
299 3 ▶ $self->package_stash->add_symbol('&import',sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
300 3 my ( $class ) = @_;
301 3 my $target = caller;
302 3 $self->yeb_import($target);
303 3 });
304  
305 3 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 3 3
306 0 $self->add_middleware(Plack::Middleware::Debug->new);
307 }
308  
309 3 ▶ my @args = $self->has_args ? ( @{$self->args} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->has_args ? : 1 2 3
310  
311 3 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 1 3 4
312 1 my $plugin = shift @args;
313 1 ▶ my $plugin_args = ref $args[0] eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'HASH' ? : 0 1 1
314 ? shift @args : {};
315 1 $self->add_plugin($self->class,$plugin,%{$plugin_args});
316 }
317 }
318  
319 2 my $cc;
320 16 ▶sub set_cc { shift; $cc = shift; }
Subroutine set_cc: called CC=1 SCAR=0.0
Pod undocumented
321 60 ▶sub cc { $cc }
Subroutine cc: called CC=1 SCAR=0.0
Pod undocumented
322 16 ▶sub reset_context { $cc = undef }
Subroutine reset_context: called CC=1 SCAR=0.0
Pod undocumented
323 0 ▶sub current_context { shift->cc }
Subroutine current_context: not called CC=1 SCAR=6.9
Pod undocumented
324  
325 6 ▶sub yeb_import {
Subroutine yeb_import: called CC=2 SCAR=6.9
Pod undocumented
326 6 my ( $self, $target ) = @_;
327 6 $self->yebs->{$target} = Yeb::Class->new(
328 app => $self,
329 class => $target,
330 );
331 6 for (keys %{$self->functions}) {
332 6 $self->y($target)->add_function($_,$self->functions->{$_});
333 }
334 }
335  
336 2 ▶sub register_function {
Subroutine register_function: called CC=3 SCAR=11.0
Pod undocumented
337 2 my ( $self, $func, $coderef ) = @_;
338 2 ▶ die "Function ".$func." already defined" if defined $self->functions->{$func};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->functions->{$func} 0 2 2
339 2 $self->functions->{$func} = $coderef;
340 2 for (keys %{$self->yebs}) {
341 2 $self->y($_)->add_function($func,$coderef);
342 }
343 }
344  
345 0 ▶sub flat {
Subroutine flat: not called CC=3 SCAR=24.8
Pod undocumented
346 0 my ( $self, $list, @seen_lists ) = @_;
347 0 ▶ if (ref $list ne 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $list ne 'ARRAY') { } 0 0 0
elsif (&any(sub { $_ == $list; } , @seen_lists)) { } 0 0 0
348 0 return $list;
349 0 ▶ } elsif (any { $_ == $list } @seen_lists) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
350 0 return;
351 } else {
352 0 push @seen_lists, $list;
353 0 return map { $self->flat($_, @seen_lists) } @{$list};
354 }
355 }
356  
357 0 ▶sub merge_hashs {
Subroutine merge_hashs: not called CC=2 SCAR=17.9
Pod undocumented
358 0 my ( $self, @hashs ) = @_;
359 0 my $first = pop @hashs;
360 0 ▶ while (@hashs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hashs) 0 0 0
361 0 my $next = pop @hashs;
362 0 $first = merge($first,$next);
363 }
364 0 return $first;
365 }
366  
367 2 1;
368  
369 __END__
↑ Summary Yeb/Plugin/Static.pm »