Plift.pm

filter: Statementstmt 95.8% 206 / 215 Branchbran 71.7% 56 / 78 Conditioncond 66.6% 28 / 42 MC/DCmcdc 46.1% 12 / 26 Subroutinesub 97.0% 33 / 34 Podpod 50.0% 5 / 10 totaltotal 83.9% 340 / 405 CC 61 SCAR 21.7
CC
61
Cov
87%
_load_template
20.8
_after_load_template
11.4
add_handler
11.2
CRAP 8.7
« Plift/Handler/Render.pm ↑ Summary Plift/Handler/Snippet.pm »
1 package Plift;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use XML::LibXML::jQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Plift::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Digest::MD5 qw/ md5_hex /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 our $VERSION = "0.15";
15  
16 10 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 XML_DOCUMENT_NODE => 9,
18 XML_DOCUMENT_FRAG_NODE => 11,
19 XML_DTD_NODE => 14
20 10 };
21  
22 10 has 'helper', is => 'ro';
23 10 has 'wrapper', is => 'ro';
24 0 ▶has 'paths', is => 'rw', default => sub { ['.'] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 9 ▶has 'snippet_namespaces', is => 'ro', default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 10 ▶has 'plugins', is => 'ro', default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
27 10 has 'encoding', is => 'rw', default => 'UTF-8';
28 10 ▶has 'debug', is => 'rw', default => sub { $ENV{PLIFT_DEBUG} };
Subroutine __ANON__: called CC=1 SCAR=0.0
29 10 has 'max_file_size', is => 'rw', default => 1024 * 1024;
30 10 has 'enable_cache', is => 'rw', default => 1;
31 10 has 'max_cached_files', is => 'rw', default => 50;
32  
33 10 ▶has '_cache', is => 'ro', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35  
36  
37 10 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.6
Pod undocumented
38 10 my $self = shift;
39  
40 # builtin handlers
41 10 my @components = qw/
42 Handler::Include
43 Handler::Wrap
44 Handler::Render
45 Handler::Snippet
46 Handler::Meta
47 /;
48  
49 # plugins
50 0 ▶ push @components, map { /^\+/ ? $_ : 'Plugin::'.$_ } @{ $self->plugins };
Branch: 1 branch
BranchTrueFalseTotal
/^\+/ ? : 0 0 0
51  
52 10 $self->load_components(@components);
53  
54 ## remove-if / remove-unless
55 10 $self->hook('after_load_template', \&_after_load_template);
56 }
57  
58 59 ▶sub _after_load_template {
Subroutine _after_load_template: called CC=10 SCAR=24.4
59 59 my ($c, $dom) = @_;
60  
61 # remove-if/unless
62 236 my $xpath = join ' | ', map { ("./*[\@$_]", ".//*[\@$_]") }
63 59 map { ($_, "data-$_") }
64 qw/ remove-if remove-unless /;
65  
66 59 foreach my $node ( @{ $dom->xfilter($xpath)->{nodes} }, @{ $dom->xfind($xpath)->{nodes} } ) {
67  
68 # remove-if
69 3 ▶ if (exists $node->{'remove-if'} || exists $node->{'data-remove-if'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"remove-if"} or exists $node->{"data-remove-if"}) 2 1 3
Condition100%3/3
exists $node->{"remove-if"} or exists $node->{"data-remove-if"}
l!l&&r!l&&!r
111
MC/DC100%2/2
exists $node->{"remove-if"} or exists $node->{"data-remove-if"}
exists $node->{"remove-if"} exists $node->{"data-remove-if"}
Truth table100%3/3
A or B
A= exists $node->{"remove-if"}B= exists $node->{"data-remove-if"}
ABresult
1X1
011
000
70  
71 2 ▶ if ($c->get($node->{'remove-if'} || $node->{'data-remove-if'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->get($node->{'remove-if'} || $node->{'data-remove-if'})) { } 2 0 2
Condition66%2/3
$node->{'remove-if'} || $node->{'data-remove-if'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$node->{'remove-if'} || $node->{'data-remove-if'}
$node->{'remove-if'} $node->{'data-remove-if'}
Truth table66%2/3
A || B
A= $node->{'remove-if'}B= $node->{'data-remove-if'}
ABresult
1X1
011
000
72  
73 2 $node->unbindNode;
74 } else {
75  
76 0 delete $node->{'remove-if'};
77 0 delete $node->{'data-remove-if'};
78 }
79 }
80  
81 # remove-unless
82 3 ▶ if (exists $node->{'remove-unless'} || exists $node->{'data-remove-unless'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"remove-unless"} or exists $node->{"data-remove-unless"}) 1 2 3
Condition66%2/3
exists $node->{"remove-unless"} or exists $node->{"data-remove-unless"}
l!l&&r!l&&!r
012
MC/DC50%1/2
exists $node->{"remove-unless"} or exists $node->{"data-remove-unless"}
exists $node->{"remove-unless"} exists $node->{"data-remove-unless"}
Truth table66%2/3
A or B
A= exists $node->{"remove-unless"}B= exists $node->{"data-remove-unless"}
ABresult
1X1
011
000
83  
84 1 ▶ unless ($c->get($node->{'remove-unless'} || $node->{'data-remove-unless'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->get($node->{'remove-unless'} || $node->{'data-remove-unless'})) { } 1 0 1
Condition33%1/3
$node->{'remove-unless'} || $node->{'data-remove-unless'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$node->{'remove-unless'} || $node->{'data-remove-unless'}
$node->{'remove-unless'} $node->{'data-remove-unless'}
Truth table33%1/3
A || B
A= $node->{'remove-unless'}B= $node->{'data-remove-unless'}
ABresult
1X1
011
000
85  
86 0 $node->unbindNode;
87 } else {
88  
89 1 delete $node->{'remove-unless'};
90 1 delete $node->{'data-remove-unless'};
91 }
92 }
93 }
94 }
95  
96  
97 10 ▶sub load_components {
Subroutine load_components: called CC=3 SCAR=11.1
Pod documented
98 10 my $self = shift;
99  
100 # instantiate and init
101 10 foreach my $name (@_) {
102  
103 50 ▶ my $class = $name =~ /^\+/ ? substr($name, 1)
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^\+/ ? : 0 50 50
104 : __PACKAGE__.'::'.$name;
105  
106 50 my $plugin = Class::Load::load_class($class)->new;
107 50 $plugin->register($self);
108 }
109 }
110  
111 2 ▶sub has_template {
Subroutine has_template: called CC=1 SCAR=0.0
Pod undocumented
112 2 my ($self, $name) = @_;
113 2 return !! $self->_find_template_file($name, $self->paths);
114 }
115  
116  
117  
118 32 ▶sub template {
Subroutine template: called CC=7 SCAR=19.9
Pod documented
119 32 my ($self, $name, $options) = @_;
120 32 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
329
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
121  
122 # path copy for the load_template closure
123 # this way we do not expose the engine nor the path to the context object
124 32 ▶ my @paths = @{ delete $options->{paths} || $self->paths };
Condition33%1/3
delete $options->{'paths'} || $self->paths
l!l&&r!l&&!r
0320
MC/DC0%0/2
delete $options->{'paths'} || $self->paths
delete $options->{'paths'} $self->paths
Truth table33%1/3
A || B
A= delete $options->{'paths'}B= $self->paths
ABresult
1X1
011
000
125 32 ▶ my @ns = @{ delete $options->{snippet_namespaces} || $self->snippet_namespaces };
Condition33%1/3
delete $options->{'snippet_namespaces'} || $self->snippet_namespaces
l!l&&r!l&&!r
0320
MC/DC0%0/2
delete $options->{'snippet_namespaces'} || $self->snippet_namespaces
delete $options->{'snippet_namespaces'} $self->snippet_namespaces
Truth table33%1/3
A || B
A= delete $options->{'snippet_namespaces'}B= $self->snippet_namespaces
ABresult
1X1
011
000
126  
127 32 ▶ $options->{$_} ||= $self->$_ for qw/ helper wrapper encoding /;
Condition100%3/3
$options->{$_} ||= $self->$_
l!l&&r!l&&!r
13362
MC/DC100%2/2
$options->{$_} ||= $self->$_
$options->{$_} $self->$_
Truth table100%3/3
A ||= B
A= $options->{$_}B= $self->$_
ABresult
1X1
011
000
128  
129 $options->{data_stack} = [delete $options->{data}]
130 32 ▶ if defined $options->{data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"data"} 0 32 32
131  
132 Plift::Context->new(
133 %$options,
134 template => $name,
135 32 handlers => [@{ $self->{handlers}}],
136 57 ▶ load_template => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
137 57 my ($ctx, $name) = @_;
138 57 $self->_load_template($name, \@paths, $ctx)
139 },
140 3 ▶ load_snippet => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 3 $self->_load_snippet(\@ns, @_);
142 },
143 220 ▶ run_hooks => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 220 $self->run_hooks(@_);
145 },
146 32 );
147 }
148  
149 6 ▶sub process {
Subroutine process: called CC=2 SCAR=7.0
Pod documented
150 6 my ($self, $template, $data, $schema) = @_;
151  
152 6 my $ctx = $self->template($template);
153  
154 6 ▶ $ctx->at($schema)
Branch: 1 branch
BranchTrueFalseTotal
if $schema 0 6 6
155 if $schema;
156  
157 6 $ctx->render($data);
158 }
159  
160 1 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
161 1 my $self = shift;
162 1 $self->process(@_)->as_html;
163 }
164  
165  
166 53 ▶sub add_handler {
Subroutine add_handler: called CC=11 SCAR=24.2
Pod documented
167 53 my ($self, $config) = @_;
168  
169 confess "missing handler callback"
170 53 ▶ unless $config->{handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"handler"} 0 53 53
171  
172 confess "missing handler name"
173 53 ▶ unless $config->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"name"} 0 53 53
174  
175 53 my @match;
176  
177 53 for my $key (qw/ tag attribute /) {
178 $config->{$key} = [$config->{$key}]
179 106 ▶ if defined $config->{$key} && !ref $config->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{$key} and not ref $config->{$key} 63 43 106
Condition100%3/3
defined $config->{$key} and not ref $config->{$key}
!ll&&!rl&&r
212263
MC/DC100%2/2
defined $config->{$key} and not ref $config->{$key}
defined $config->{$key} not ref $config->{$key}
Truth table100%3/3
A and not(B)
A= defined $config->{$key}B= not ref $config->{$key}
ABresult
0X0
100
111
180 }
181  
182 44 push(@match, map { ".//$_" } @{$config->{tag}})
183 53 ▶ if $config->{tag};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"tag"} 43 10 53
184  
185 63 push(@match, map { ".//*[\@$_]" } @{$config->{attribute}})
186 53 ▶ if $config->{attribute};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"attribute"} 42 11 53
187  
188 push @match, $config->{xpath}
189 53 ▶ if $config->{xpath};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"xpath"} 0 53 53
190  
191 53 my $match = join ' | ', @match;
192  
193 53 ▶ printf STDERR "[Plift] Adding handler: $match\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 53 53
194 if $self->debug;
195  
196 # check config has one of tag/attribute/xpath
197 53 ▶ confess "Invalid handler. Missing at least one binding criteria (tag, attribute or xpath)."
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 53 53
198 unless $match;
199  
200 my $handler = {
201 tag => $config->{tag},
202 attribute => $config->{attribute},
203 name => $config->{name},
204 xpath => $match,
205 sub => $config->{handler}
206 53 };
207  
208 53 push @{$self->{handlers}}, $handler;
209 53 $self->{handlers_by_name}->{$handler->{name}} = $handler;
210  
211 53 $self;
212 }
213  
214 5 ▶sub get_handler {
Subroutine get_handler: called CC=1 SCAR=0.0
Pod undocumented
215 5 my ($self, $name) = @_;
216 5 $self->{handlers_by_name}->{$name};
217 }
218  
219  
220  
221 10 my %hooks;
222 14 ▶sub hook {
Subroutine hook: called CC=4 SCAR=15.9
Pod undocumented
223 14 my ($self, $step, $cb) = @_;
224  
225 14 ▶ croak "Usage: plift->hook(<step>, <callback>)"
Branch: 1 branch
BranchTrueFalseTotal
unless $step and $cb and ref $cb eq "CODE" 0 14 14
Condition33%2/6
$step and $cb and ref $cb eq "CODE"
!ll&&!rl&&r
0014
$step and $cb
!ll&&!rl&&r
0014
MC/DC0%0/3
$step and $cb and ref $cb eq "CODE"
$step $cb ref $cb eq "CODE"
Truth table25%1/4
A and B and C
A= $stepB= $cbC= ref $cb eq "CODE"
ABCresult
0XX0
10X0
1100
1111
226 unless $step && $cb && ref $cb eq 'CODE';
227  
228 14 push @{$hooks{$step}}, $cb;
229 14 $self;
230 }
231  
232 220 ▶sub run_hooks {
Subroutine run_hooks: called CC=4 SCAR=13.9
Pod undocumented
233 220 my ($self, $step, $args) = @_;
234  
235 220 ▶ foreach my $cb (@{ $hooks{$step} || [] }) {
Condition100%2/2
$hooks{$step} || []
l!l
59161
MC/DC100%1/1
$hooks{$step} || []
$hooks{$step}
Truth table100%2/2
A
A= $hooks{$step}
Aresult
11
00
236 65 ▶ $cb->(@{ $args || [] });
Condition50%1/2
$args || []
l!l
650
MC/DC0%0/1
$args || []
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
237 }
238 }
239  
240  
241  
242 61 ▶sub _load_template {
Subroutine _load_template: called CC=20 SCAR=30.3
243 61 my ($self, $template, $paths, $ctx) = @_;
244  
245 61 my ($tpl_source, $tpl_etag, $tpl_file, $tpl_path, $cache_key);
246  
247 # inline template
248 61 ▶ if (ref $template) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template) { } 3 58 61
249  
250 3 $tpl_source = $$template;
251 3 $tpl_etag = md5_hex($tpl_source);
252 3 $cache_key = 'inline:'.$tpl_etag;
253 }
254  
255 # file template
256 else {
257  
258 # add relative prefix
259 # (only './foo' and '../foo' are considered relative, not plain 'foo')
260 58 ▶ if (defined $ctx->{current_file} && $template =~ /^\.\.?\//) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctx->{"current_file"} and $template =~ m[^\.\.?/]) 20 38 58
Condition100%3/3
defined $ctx->{"current_file"} and $template =~ m[^\.\.?/]
!ll&&!rl&&r
271120
MC/DC100%2/2
defined $ctx->{"current_file"} and $template =~ m[^\.\.?/]
defined $ctx->{"current_file"} $template =~ m[^\.\.?/]
Truth table100%3/3
A and B
A= defined $ctx->{"current_file"}B= $template =~ m[^\.\.?/]
ABresult
0X0
100
111
261 20 my $current_file = $ctx->{current_file};
262 20 my $current_path = $ctx->{current_path};
263 20 my $prefix = $current_file->parent->relative($current_path);
264 20 ▶ $template = "$prefix/$template"
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix eq "." 19 1 20
265 unless $prefix eq '.';
266 }
267  
268 # resolve template file
269 58 ($tpl_file, $tpl_path) = $self->_find_template_file($template, $paths);
270 58 ▶ die sprintf "Can't find a template file for template '%s'. Tried:\n%s\n", $template, join(",\n", @$paths)
Branch: 1 branch
BranchTrueFalseTotal
unless $tpl_file 0 58 58
271 unless $tpl_file;
272  
273 # update contex current file/path
274 58 $ctx->{current_file} = $tpl_file;
275 58 $ctx->{current_path} = $tpl_path;
276  
277 58 $tpl_etag = $tpl_file->stat->mtime;
278 58 $cache_key = $tpl_file->stringify;
279 }
280  
281 # cached template
282 61 my $cache = $self->_cache;
283 61 my $dom;
284  
285 # get from cache
286 61 ▶ if ($self->enable_cache && (my $entry = $cache->{$cache_key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_cache and my $entry = $cache->{$cache_key}) 23 38 61
Condition66%2/3
$self->enable_cache and my $entry = $cache->{$cache_key}
!ll&&!rl&&r
03823
MC/DC50%1/2
$self->enable_cache and my $entry = $cache->{$cache_key}
$self->enable_cache my $entry = $cache->{$cache_key}
Truth table66%2/3
A and B
A= $self->enable_cacheB= my $entry = $cache->{$cache_key}
ABresult
0X0
100
111
287  
288 # cache hit
289 23 ▶ if ($entry->{etag} eq $tpl_etag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{'etag'} eq $tpl_etag) { } 23 0 23
290  
291 23 $dom = $entry->{dom}->clone->contents;
292 23 $entry->{hits} += 1;
293 23 $entry->{last_hit} = time;
294 # printf STDERR "# Plift cache hit: '$tpl_file' => %d hits\n", $entry->{hits};
295 }
296  
297 # invalidade cache entry
298 else {
299 0 delete $cache->{$cache_key};
300 }
301 }
302  
303 61 ▶ unless ($dom) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dom) 38 23 61
304  
305 # max file size
306 38 ▶ my $tpl_size = defined $tpl_file ? $tpl_file->stat->size : length $tpl_source;
Branch: 1 branch
BranchTrueFalseTotal
defined $tpl_file ? : 36 2 38
307 38 ▶ die sprintf("Template '%s' exceeds the max_file_size option! (%d > %d)\n", $cache_key, $tpl_size, $self->max_file_size)
Branch: 1 branch
BranchTrueFalseTotal
if $tpl_size > $self->max_file_size 0 38 38
308 if $tpl_size > $self->max_file_size;
309  
310 # parse source
311 38 ▶ if (defined $tpl_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tpl_file) 36 2 38
312 36 ▶ $tpl_source = $ctx->encoding eq 'UTF-8' ? $tpl_file->slurp_utf8
Branch: 1 branch
BranchTrueFalseTotal
$ctx->encoding eq 'UTF-8' ? : 36 0 36
313 : $tpl_file->slurp( binmode => ":unix:encoding(".$self->encoding.")")
314 }
315  
316 38 $dom = XML::LibXML::jQuery->new($tpl_source);
317  
318 # check for data-plift-template attr, and use that element
319 38 my $body = $dom->xfind('//body[@data-plift-template]');
320  
321 38 ▶ if ($body->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body->size) 3 35 38
322  
323 3 my $selector = $body->attr('data-plift-template');
324 3 my $template_element = $dom->find($selector);
325 3 ▶ confess "Can't find template via selector '$selector' (referenced at <body data-plift-template=\"$selector\">)."
Branch: 1 branch
BranchTrueFalseTotal
unless $template_element->size 0 3 3
326 unless $template_element->size;
327  
328 # create new document for the template elment
329 3 $dom = j()->document->append($template_element)->contents;
330 }
331  
332 # cache it
333 38 ▶ if ($self->enable_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_cache) 38 0 38
334  
335 # control cache size
336 38 ▶ if (scalar keys(%$cache) == $self->max_cached_files) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$cache == $self->max_cached_files) 0 38 38
337  
338 0 my @least_used = sort { $cache->{$b}{last_hit} <=> $cache->{$a}{last_hit} }
339 keys %$cache;
340  
341 0 delete $cache->{$least_used[0]};
342 }
343  
344 38 $cache->{$cache_key} = {
345 dom => $dom->document->clone,
346 etag => $tpl_etag,
347 hits => 0,
348 last_hit => 0,
349 };
350 }
351 }
352  
353 # adopt into document
354 61 ▶ if (my $existing_document = $ctx->document) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing_document = $ctx->document) { } 34 27 61
355  
356 34 $existing_document = $existing_document->get(0);
357  
358 # replace DTD
359 34 ▶ if (my $dtd = $dom->{document}->internalSubset) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dtd = $dom->{"document"}->internalSubset) 7 27 34
360 7 $existing_document->removeInternalSubset;
361 7 $existing_document->createInternalSubset( $dtd->getName, $dtd->publicId, $dtd->systemId );
362 }
363  
364 # adopt nodes
365 34 my @nodes = map { $existing_document->adoptNode($_); $_ }
366 38 grep { $_->nodeType != XML_DTD_NODE }
367 # grep { $_->getOwner->nodeType == XML_DOCUMENT_NODE }
368 34 @{ $dom->{nodes} };
369  
370 # reinstantitate on new document
371 34 $dom = $dom->_new_nodes(\@nodes, undef, $existing_document);
372 }
373  
374 # 1st tempalte loaded, set contex document
375 else {
376 27 $ctx->document($dom->document);
377 }
378  
379 61 $dom;
380 }
381  
382 60 ▶sub _find_template_file {
Subroutine _find_template_file: called CC=6 SCAR=17.9
383 60 my ($self, $template_name, $paths) = @_;
384  
385 # clean \x00 char that can be used to truncate our string
386 60 $template_name =~ tr/\x00//d;
387  
388 60 foreach my $path (@$paths) {
389  
390 61 ▶ if (-e (my $file = "$path/$template_name.html")) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e (my $file = "$path/$template_name.html")) 59 2 61
391  
392 59 $file = Path::Tiny->new($file);
393 59 $path = Path::Tiny->new($path);
394  
395 # if suspicious template name, check file is really child of path
396 59 ▶ if ($template_name =~ /\.\.\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template_name =~ m[\.\./]) 13 46 59
397 13 $file = $file->realpath;
398 13 $path = $path->realpath;
399  
400 13 ▶ die "[Plift] attempt to traverse out of path via '$template_name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $path->subsumes($file) 0 13 13
401 unless $path->subsumes($file)
402 }
403  
404 59 ▶ return wantarray ? ($file, $path) : $file;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 58 1 59
405 }
406 }
407  
408 1 return;
409 }
410  
411 3 ▶sub _load_snippet {
Subroutine _load_snippet: called CC=1 SCAR=0.0
412 3 my ($self, $ns, $name, $params) = @_;
413  
414 3 my $class_sufixx = _camelize($name);
415 3 my @try_classes = map { join '::', $_, $class_sufixx } @$ns;
416 3 my $snippet_class = Class::Load::load_first_existing_class @try_classes;
417  
418  
419 3 $snippet_class->new($params);
420 }
421  
422 # borrowed from Mojo::Util :)
423 3 ▶sub _camelize {
Subroutine _camelize: called CC=2 SCAR=7.0
424 3 my $str = shift;
425 3 ▶ return $str if $str =~ /^[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^[A-Z]/ 0 3 3
426  
427 # CamelCase words
428 return join '::', map {
429 3 join( '', map { ucfirst lc } split '_' )
430 } split '-', $str;
431 }
432  
433  
434 10 1;
435 __END__
« Plift/Handler/Render.pm ↑ Summary Plift/Handler/Snippet.pm »