blib/lib/Text/Xslate.pm

filter: Statementstmt 95.4% 316 / 331 Branchbran 86.6% 104 / 120 Conditioncond 70.7% 29 / 41 MC/DCmcdc 40.0% 10 / 25 Subroutinesub 90.2% 37 / 41 Podpod 50.0% 1 / 2 totaltotal 88.7% 497 / 560 CC 71 SCAR 19.8
CC
71
Cov
91%
_load_compiled
15.1
_load_source
14.3
new
10.0
CRAP 7.2
« blib/lib/Text/Xslate/PP.pm ↑ Summary blib/lib/Text/Xslate/Util.pm »
1 package Text::Xslate;
2 # The Xslate engine class
3 378 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 378 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 378 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 378 ▶use version; our $VERSION = version->declare('v3.5.9');
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 378 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 378 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 378 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 378 ▶use Data::MessagePack ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 378 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 378 ▶use Text::Xslate::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 378 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # all the exportable functions are defined in ::Util
18 378 our @EXPORT_OK = qw(
19 mark_raw
20 unmark_raw
21 escaped_string
22 html_escape
23 uri_escape
24 html_builder
25 );
26 378 Text::Xslate::Util->import(@EXPORT_OK);
27 }
28  
29 378 our @ISA = qw(Text::Xslate::Engine);
30  
31 378 my $BYTECODE_VERSION = '1.6';
32  
33 # $bytecode_version + $fullpath + $compiler_and_parser_options
34 378 my $XSLATE_MAGIC = qq{xslate;$BYTECODE_VERSION;%s;%s;};
35  
36 378 our $DEFAULT_CACHE_DIR;
37  
38 # load backend (XS or PP)
39 378 my $use_xs = 0;
40 378 ▶if(!exists $INC{'Text/Xslate/PP.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{"Text/Xslate/PP.pm"}) 376 2 378
41 376 ▶ my $pp = ($Text::Xslate::Util::DEBUG =~ /\b pp \b/xms or $ENV{PERL_ONLY});
Condition100%3/3
$Text::Xslate::Util::DEBUG =~ /\b pp \b/msx || $ENV{'PERL_ONLY'}
l!l&&r!l&&!r
3185188
MC/DC100%2/2
$Text::Xslate::Util::DEBUG =~ /\b pp \b/msx || $ENV{'PERL_ONLY'}
$Text::Xslate::Util::DEBUG =~ /\b pp \b/msx $ENV{'PERL_ONLY'}
Truth table100%3/3
A || B
A= $Text::Xslate::Util::DEBUG =~ /\b pp \b/msxB= $ENV{'PERL_ONLY'}
ABresult
1X1
011
000
42 376 ▶ if(!$pp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pp) 188 188 376
43 188 eval {
44 188 require XSLoader;
45 188 XSLoader::load(__PACKAGE__, $VERSION);
46 188 $use_xs = 1;
47 };
48 188 ▶ die $@ if $@ && $Text::Xslate::Util::DEBUG =~ /\b xs \b/xms; # force XS
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $Text::Xslate::Util::DEBUG =~ /\b xs \b/msx 0 188 188
Condition33%1/3
$@ and $Text::Xslate::Util::DEBUG =~ /\b xs \b/msx
!ll&&!rl&&r
18800
MC/DC0%0/2
$@ and $Text::Xslate::Util::DEBUG =~ /\b xs \b/msx
$@ $Text::Xslate::Util::DEBUG =~ /\b xs \b/msx
Truth table33%1/3
A and B
A= $@B= $Text::Xslate::Util::DEBUG =~ /\b xs \b/msx
ABresult
0X0
100
111
49 }
50 376 ▶ if(!__PACKAGE__->can('render')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__PACKAGE__->can("render")) 188 188 376
51 188 require 'Text/Xslate/PP.pm';
52 }
53 }
54 0 ▶sub USE_XS() { $use_xs }
Subroutine USE_XS: not called CC=1 SCAR=6.9
Pod undocumented
55  
56 # for error messages (see T::X::Util)
57 6017 ▶sub input_layer { ref($_[0]) ? $_[0]->{input_layer} : ':utf8' }
Subroutine input_layer: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5420 597 6017
58  
59 package Text::Xslate::Engine; # XS/PP common base class
60  
61 378 ▶use version; our $VERSION = version->declare('v3.5.9');
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 378 ▶use Text::Xslate::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
64 make_error
65 dump
66 378 );
67  
68 # constants
69 378 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.2
70 378 our @ISA = qw(Exporter);
71  
72 378 my $dump_load = scalar($Text::Xslate::Util::DEBUG =~ /\b dump=load \b/xms);
73 0 ▶ *_DUMP_LOAD = sub(){ $dump_load };
Subroutine __ANON__: not called CC=1 SCAR=1.2
74  
75 378 my $save_src = scalar($Text::Xslate::Util::DEBUG =~ /\b save_src \b/xms);
76 0 ▶ *_SAVE_SRC = sub() { $save_src };
Subroutine __ANON__: not called CC=1 SCAR=1.2
77  
78 378 *_ST_MTIME = sub() { 9 }; # see perldoc -f stat
79 }
80  
81 378 ▶unless(defined $DEFAULT_CACHE_DIR) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Text::Xslate::DEFAULT_CACHE_DIR) 378 0 378
82 378 my $cache_dir = '.xslate_cache';
83 378 foreach my $d($ENV{HOME}, File::Spec->tmpdir) {
84 378 ▶ if(defined($d) and -d $d and -w _) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d and -d $d and -w _) 378 0 378
Condition33%2/6
defined $d and -d $d and -w _
!ll&&!rl&&r
00378
defined $d and -d $d
!ll&&!rl&&r
00378
MC/DC0%0/3
defined $d and -d $d and -w _
defined $d -d $d -w _
Truth table25%1/4
A and B and C
A= defined $dB= -d $dC= -w _
ABCresult
0XX0
10X0
1100
1111
85 378 $cache_dir = File::Spec->catfile($d, '.xslate_cache');
86 378 last;
87 }
88 }
89  
90 378 $DEFAULT_CACHE_DIR = $cache_dir;
91 }
92  
93 # the real defaults are defined in the parser
94 378 my %parser_option = (
95 line_start => undef,
96 tag_start => undef,
97 tag_end => undef,
98 );
99  
100 # the real defaults are defined in the compiler
101 378 my %compiler_option = (
102 syntax => undef,
103 type => undef,
104 header => undef, # template augment
105 footer => undef, # template augment
106 macro => undef, # template augment
107 );
108  
109 378 my %builtin = (
110 html_escape => \&Text::Xslate::Util::html_escape,
111 mark_raw => \&Text::Xslate::Util::mark_raw,
112 unmark_raw => \&Text::Xslate::Util::unmark_raw,
113 uri_escape => \&Text::Xslate::Util::uri_escape,
114  
115 is_array_ref => \&Text::Xslate::Util::is_array_ref,
116 is_hash_ref => \&Text::Xslate::Util::is_hash_ref,
117  
118 dump => \&Text::Xslate::Util::dump,
119  
120 # aliases
121 raw => 'mark_raw',
122 html => 'html_escape',
123 uri => 'uri_escape',
124 );
125  
126 593 ▶sub default_functions { +{} } # overridable
Subroutine default_functions: called CC=1 SCAR=0.0
127  
128 1297 ▶sub parser_option { # overridable
Subroutine parser_option: called CC=1 SCAR=0.0
129 1297 return \%parser_option;
130 }
131  
132 1297 ▶sub compiler_option { # overridable
Subroutine compiler_option: called CC=1 SCAR=0.0
133 1297 return \%compiler_option;
134 }
135  
136 50 ▶sub replace_option_value_for_magic_token { # overridable
Subroutine replace_option_value_for_magic_token: called CC=1 SCAR=0.0
137 #my($self, $name, $value) = @_;
138 #$value;
139 50 return $_[2];
140 }
141  
142 599 ▶sub options { # overridable
Subroutine options: called CC=1 SCAR=0.0
143 599 my($self) = @_;
144 return {
145 # name => default
146 suffix => '.tx',
147 path => ['.'],
148 input_layer => $self->input_layer,
149 cache => 1, # 0: not cached, 1: checks mtime, 2: always cached
150 cache_dir => $DEFAULT_CACHE_DIR,
151 module => undef,
152 function => undef,
153 html_builder_module => undef,
154 compiler => 'Text::Xslate::Compiler',
155  
156 verbose => 1,
157 warn_handler => undef,
158 die_handler => undef,
159 pre_process_handler => undef,
160  
161 599 %{ $self->parser_option },
162 599 %{ $self->compiler_option },
163 };
164 }
165  
166 599 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
167 599 my $class = shift;
168 599 ▶ my %args = (@_ == 1 ? %{$_[0]} : @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 28 571 599
169  
170 599 my $options = $class->options;
171 599 my $used = 0;
172 599 my $nargs = scalar keys %args;
173 599 foreach my $key(keys %{$options}) {
174 12587 ▶ if(exists $args{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{$key}) { } 1491 11096 12587
elsif (defined $options->{$key}) { } 3186 7910 11096
175 1491 $used++;
176 }
177 elsif(defined($options->{$key})) {
178 3186 $args{$key} = $options->{$key};
179 }
180 }
181  
182 599 ▶ if($used != $nargs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used != $nargs) 4 595 599
183 4 my @unknowns = sort grep { !exists $options->{$_} } keys %args;
184 4 warnings::warnif(misc
185 => "$class: Unknown option(s): " . join ' ', @unknowns);
186 }
187  
188 $args{path} = [
189 625 ▶ map { ref($_) ? $_ : File::Spec->rel2abs($_) }
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 114 511 625
ref $args{'path'} eq 'ARRAY' ? : 505 92 597
190 505 ref($args{path}) eq 'ARRAY' ? @{$args{path}} : $args{path}
191 597 ];
192  
193 597 my $arg_function= $args{function};
194  
195 597 my %funcs;
196 597 $args{function} = \%funcs;
197  
198 597 $args{template} = {}; # template structures
199  
200 597 my $self = bless \%args, $class;
201  
202 # definition of functions and methods
203  
204 # builtin functions
205 595 %funcs = %builtin;
206 595 $self->_register_builtin_methods(\%funcs);
207  
208 # per-class functions
209 595 $self->_merge_hash(\%funcs, $class->default_functions());
210  
211 # user-defined functions
212 595 ▶ if(defined $args{module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"module"}) 22 573 595
213 $self->_merge_hash(\%funcs,
214 22 Text::Xslate::Util::import_from(@{$args{module}}));
215 }
216  
217 # user-defined html builder functions
218 591 ▶ if(defined $args{html_builder_module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"html_builder_module"}) 2 589 591
219 2 my $raw = Text::Xslate::Util::import_from(@{$args{html_builder_module}});
220 my $html_builders = +{
221 map {
222 2 ($_ => &Text::Xslate::Util::html_builder($raw->{$_}))
223 } keys %$raw
224 };
225 2 $self->_merge_hash(\%funcs, $html_builders);
226 }
227  
228 591 $self->_merge_hash(\%funcs, $arg_function);
229  
230 591 $self->_resolve_function_aliases(\%funcs);
231  
232 591 return $self;
233 }
234  
235 1206 ▶sub _merge_hash {
Subroutine _merge_hash: called CC=2 SCAR=6.9
236 1206 my($self, $base, $add) = @_;
237 1206 foreach my $name(keys %{$add}) {
238 200 $base->{$name} = $add->{$name};
239 }
240 1206 return;
241 }
242  
243 591 ▶sub _resolve_function_aliases {
Subroutine _resolve_function_aliases: called CC=8 SCAR=22.4
244 591 my($self, $funcs) = @_;
245  
246 591 foreach my $f(values %{$funcs}) {
247 14370 my %seen; # to avoid infinite loops
248 14370 ▶ while(!( ref($f) or Scalar::Util::looks_like_number($f) )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $f or &Scalar::Util::looks_like_number($f)) 1771 12599 14370
Condition100%3/3
ref $f or &Scalar::Util::looks_like_number($f)
l!l&&r!l&&!r
847241271771
MC/DC100%2/2
ref $f or &Scalar::Util::looks_like_number($f)
ref $f &Scalar::Util::looks_like_number($f)
Truth table100%3/3
A or B
A= ref $fB= &Scalar::Util::looks_like_number($f)
ABresult
1X1
011
000
249 1771 ▶ my $v = $funcs->{$f} or $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
my $v = $funcs->{$f} or $self->_error("Cannot resolve a function alias '${f}'," . ' which refers nothing') 1771 0 1771
250 "Cannot resolve a function alias '$f',"
251 . " which refers nothing",
252 );
253  
254 1771 ▶ if( ref($v) or Scalar::Util::looks_like_number($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v or &Scalar::Util::looks_like_number($v)) { } 1771 0 1771
Condition33%1/3
ref $v or &Scalar::Util::looks_like_number($v)
l!l&&r!l&&!r
177100
MC/DC0%0/2
ref $v or &Scalar::Util::looks_like_number($v)
ref $v &Scalar::Util::looks_like_number($v)
Truth table33%1/3
A or B
A= ref $vB= &Scalar::Util::looks_like_number($v)
ABresult
1X1
011
000
255 1771 $f = $v;
256 1771 last;
257 }
258 else {
259 0 ▶ $seen{$v}++ and $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
$seen{$v}++ and $self->_error("Cannot resolve a function alias '${f}'," . ' which makes circular references') 0 0 0
260 "Cannot resolve a function alias '$f',"
261 . " which makes circular references",
262 );
263 }
264 }
265 }
266  
267 591 return;
268 }
269  
270 2787 ▶sub load_string { # called in render_string()
Subroutine load_string: called CC=2 SCAR=6.9
271 2787 my($self, $string) = @_;
272 2787 ▶ if(not defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 2 2785 2787
273 2 $self->_error("LoadError: Template string is not given");
274 }
275 2785 $self->note(' _load_string: %s', join '\n', split /\n/, $string)
276 if _DUMP_LOAD;
277 2785 $self->{source}{'<string>'} = $string if _SAVE_SRC;
278 2785 $self->{string_buffer} = $string;
279 2785 my $asm = $self->compile($string);
280 2657 $self->_assemble($asm, '<string>', \$string, undef, undef);
281 2657 return $asm;
282 }
283  
284 378 my $updir = File::Spec->updir;
285 4476 ▶sub find_file {
Subroutine find_file: called CC=9 SCAR=22.0
286 4476 my($self, $file) = @_;
287  
288 4476 ▶ if($file =~ /\Q$updir\E/xmso) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\Q$updir\E/mosx) 8 4468 4476
289 8 $self->_error("LoadError: Forbidden component (updir: '$updir') found in file name '$file'");
290 }
291  
292 4468 my $fullpath;
293 my $cachepath;
294 4468 my $orig_mtime;
295 4468 my $cache_mtime;
296 4468 foreach my $p(@{$self->{path}}) {
297 4500 $self->note(" find_file: %s in %s ...\n", $file, $p) if _DUMP_LOAD;
298  
299 4500 my $cache_prefix;
300 4500 ▶ if(ref $p eq 'HASH') { # virtual path
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq 'HASH') { } 202 4298 4500
301 202 ▶ defined(my $content = $p->{$file}) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $content = $p->{$file}) or next 192 10 202
302 192 $fullpath = \$content;
303  
304 # NOTE:
305 # Because contents of virtual paths include their digest,
306 # time-dependent cache verifier makes no sense.
307 192 $orig_mtime = 0;
308 192 $cache_mtime = 0;
309 192 $cache_prefix = 'HASH';
310 }
311 else {
312 4298 $fullpath = File::Spec->catfile($p, $file);
313 4298 ▶ defined($orig_mtime = (stat($fullpath))[_ST_MTIME])
Branch: 1 branch
BranchTrueFalseTotal
defined($orig_mtime = (stat $fullpath)[9]) or next 4266 32 4298
314 or next;
315 4266 $cache_prefix = Text::Xslate::uri_escape($p);
316 4266 ▶ if (length $cache_prefix > 127) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $cache_prefix > 127) 0 4266 4266
317 # some filesystems refuse a path part with length > 127
318 0 $cache_prefix = $self->_digest($cache_prefix);
319 }
320 }
321  
322 # $file is found
323 $cachepath = File::Spec->catfile(
324 $self->{cache_dir},
325 4458 $cache_prefix,
326 $file . 'c',
327 );
328 # stat() will be failed if the cache doesn't exist
329 4458 $cache_mtime = (stat($cachepath))[_ST_MTIME];
330 4458 last;
331 }
332  
333 4468 ▶ if(not defined $orig_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $orig_mtime) 10 4458 4468
334 10 $self->_error("LoadError: Cannot find '$file' (path: @{$self->{path}})");
335 }
336  
337 4458 $self->note(" find_file: %s (mtime=%d)\n",
338 $fullpath, $cache_mtime || 0) if _DUMP_LOAD;
339  
340 return {
341 4458 ▶ name => ref($fullpath) ? $file : $fullpath,
Branch: 1 branch
BranchTrueFalseTotal
ref $fullpath ? : 192 4266 4458
342 fullpath => $fullpath,
343 cachepath => $cachepath,
344  
345 orig_mtime => $orig_mtime,
346 cache_mtime => $cache_mtime,
347 };
348 }
349  
350  
351 4230 ▶sub load_file {
Subroutine load_file: called CC=5 SCAR=16.1
352 4230 my($self, $file, $mtime, $omit_augment) = @_;
353  
354 4230 local $self->{omit_augment} = $omit_augment;
355  
356 4230 $self->note("%s->load_file(%s)\n", $self, $file) if _DUMP_LOAD;
357  
358 4230 ▶ if($file eq '<string>') { # simply reload it
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "<string>") 2 4228 4230
359 2 return $self->load_string($self->{string_buffer});
360 }
361  
362 4228 my $fi = $self->find_file($file);
363  
364 4216 ▶ my $asm = $self->_load_compiled($fi, $mtime) || $self->_load_source($fi, $mtime);
Condition100%3/3
$self->_load_compiled($fi, $mtime) || $self->_load_source($fi, $mtime)
l!l&&r!l&&!r
14140714
MC/DC0%0/2
$self->_load_compiled($fi, $mtime) || $self->_load_source($fi, $mtime)
$self->_load_compiled($fi, $mtime) $self->_load_source($fi, $mtime)
Truth table66%2/3
A || B
A= $self->_load_compiled($fi, $mtime)B= $self->_load_source($fi, $mtime)
ABresult
1X1
011
000
365  
366 # $cache_mtime is undef : uses caches without any checks
367 # $cache_mtime > 0 : uses caches with mtime checks
368 # $cache_mtime == 0 : doesn't use caches
369 4206 my $cache_mtime;
370 4206 ▶ if($self->{cache} < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"} < 2) 4188 18 4206
371 4188 ▶ $cache_mtime = $fi->{cache_mtime} || 0;
Condition100%2/2
$fi->{'cache_mtime'} || 0
l!l
3163872
MC/DC100%1/1
$fi->{'cache_mtime'} || 0
$fi->{'cache_mtime'}
Truth table100%2/2
A
A= $fi->{'cache_mtime'}
Aresult
11
00
372 }
373  
374 4206 $self->_assemble($asm, $file, $fi->{fullpath}, $fi->{cachepath}, $cache_mtime);
375 4206 return $asm;
376 }
377  
378 4127 ▶sub slurp_template {
Subroutine slurp_template: called CC=3 SCAR=11.0
379 4127 my($self, $input_layer, $fullpath) = @_;
380  
381 4127 ▶ if (ref $fullpath eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fullpath eq 'SCALAR') { } 146 3981 4127
382 146 return $$fullpath;
383 } else {
384 3981 ▶ open my($source), '<' . $input_layer, $fullpath
Branch: 1 branch
BranchTrueFalseTotal
open my $source, '<' . $input_layer, $fullpath or $self->_error("LoadError: Cannot open $fullpath for reading: $!") 3981 0 3981
385 or $self->_error("LoadError: Cannot open $fullpath for reading: $!");
386 3981 local $/;
387 3981 return scalar <$source>;
388 }
389 }
390  
391 4075 ▶sub _load_source {
Subroutine _load_source: called CC=13 SCAR=26.6
392 4075 my($self, $fi) = @_;
393 4075 my $fullpath = $fi->{fullpath};
394 4075 my $cachepath = $fi->{cachepath};
395  
396 4075 $self->note(" _load_source: try %s ...\n", $fullpath) if _DUMP_LOAD;
397  
398 # This routine is called when the cache is no longer valid (or not created yet)
399 # so it should be ensured that the cache, if exists, does not exist
400 4075 ▶ if(-e $cachepath) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cachepath) 67 4008 4075
401 67 ▶ unlink $cachepath
Branch: 1 branch
BranchTrueFalseTotal
unlink $cachepath or Carp::carp("Xslate: cannot unlink $cachepath (ignored): $!") 67 0 67
402 or Carp::carp("Xslate: cannot unlink $cachepath (ignored): $!");
403 }
404  
405 4075 my $source = $self->slurp_template($self->input_layer, $fullpath);
406 4075 ▶ $source = $self->{pre_process_handler}->($source) if $self->{pre_process_handler};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pre_process_handler"} 2 4073 4075
407 4075 $self->{source}{$fi->{name}} = $source if _SAVE_SRC;
408  
409 my $asm = $self->compile($source,
410 file => $fullpath,
411 name => $fi->{name},
412 4075 );
413  
414 4065 ▶ if($self->{cache} >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"} >= 1) 193 3872 4065
415 193 my($volume, $dir) = File::Spec->splitpath($fi->{cachepath});
416 193 my $cachedir = File::Spec->catpath($volume, $dir, '');
417 193 ▶ if(not -e $cachedir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $cachedir) 84 109 193
418 84 require File::Path;
419 84 File::Path::mkpath($cachedir);
420 84 ▶ if (! -e $cachedir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $cachedir) 0 84 84
421 0 Carp::croak("Xslate: Cannot prepare cache directory $cachepath (ignored): $@");
422 }
423 }
424  
425 193 my $tmpfile = sprintf('%s.%d.%d', $cachepath, $$, Scalar::Util::refaddr($self));
426  
427 193 ▶ if (open my($out), ">:raw", $tmpfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $out, '>:raw', $tmpfile) { } 193 0 193
428 193 my $mtime = $self->_save_compiled($out, $asm, $fullpath, utf8::is_utf8($source));
429  
430 193 ▶ if(!close $out) {
Branch: 2 branches
BranchTrueFalseTotal
if (not close $out) { } 0 193 193
elsif (rename $tmpfile, $cachepath) { } 193 0 193
431 0 Carp::carp("Xslate: Cannot close $cachepath (ignored): $!");
432 0 unlink $tmpfile;
433 }
434 elsif (rename($tmpfile => $cachepath)) {
435 # set the newest mtime of all the related files to cache mtime
436 193 ▶ if (not ref $fullpath) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $fullpath) { } 149 44 193
437 149 my $main_mtime = (stat $fullpath)[_ST_MTIME];
438 149 ▶ if (defined($main_mtime) && $main_mtime > $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main_mtime and $main_mtime > $mtime) 121 28 149
Condition66%2/3
defined $main_mtime and $main_mtime > $mtime
!ll&&!rl&&r
028121
MC/DC50%1/2
defined $main_mtime and $main_mtime > $mtime
defined $main_mtime $main_mtime > $mtime
Truth table66%2/3
A and B
A= defined $main_mtimeB= $main_mtime > $mtime
ABresult
0X0
100
111
439 121 $mtime = $main_mtime;
440 }
441 149 utime $mtime, $mtime, $cachepath;
442 149 $fi->{cache_mtime} = $mtime;
443 }
444 else {
445 44 $fi->{cache_mtime} = (stat $cachepath)[_ST_MTIME];
446 }
447 }
448 else {
449 0 Carp::carp("Xslate: Cannot rename cache file $cachepath (ignored): $!");
450 0 unlink $tmpfile;
451 }
452 }
453 else {
454 0 Carp::carp("Xslate: Cannot open $cachepath for writing (ignored): $!");
455 }
456 }
457 4065 if(_DUMP_LOAD) {
458 $self->note(" _load_source: cache(mtime=%s)\n",
459 defined $fi->{cache_mtime} ? $fi->{cache_mtime} : 'undef');
460 }
461  
462 4065 return $asm;
463 }
464  
465 # load compiled templates if they are fresh enough
466 4216 ▶sub _load_compiled {
Subroutine _load_compiled: called CC=15 SCAR=27.1
467 4216 my($self, $fi, $threshold) = @_;
468  
469 4216 ▶ if($self->{cache} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cache'} >= 2) { } 18 4198 4216
470 # threshold is the most latest modified time of all the related caches,
471 # so if the cache level >= 2, they seems always fresh.
472 18 $threshold = 9**9**9; # force to purge the cache
473 }
474 else {
475 4198 ▶ $threshold ||= $fi->{cache_mtime};
Condition100%3/3
$threshold ||= $fi->{"cache_mtime"}
l!l&&r!l&&!r
101884000
MC/DC100%2/2
$threshold ||= $fi->{"cache_mtime"}
$threshold $fi->{"cache_mtime"}
Truth table100%3/3
A ||= B
A= $thresholdB= $fi->{"cache_mtime"}
ABresult
1X1
011
000
476 }
477 # see also tx_load_template() in xs/Text-Xslate.xs
478 4216 ▶ if(!( defined($fi->{cache_mtime}) and $self->{cache} >= 1
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fi->{"cache_mtime"} and $self->{"cache"} >= 1 and $threshold >= $fi->{"orig_mtime"}) 4041 175 4216
Condition83%5/6
defined $fi->{"cache_mtime"} and $self->{"cache"} >= 1 and $threshold >= $fi->{"orig_mtime"}
!ll&&!rl&&r
400833175
defined $fi->{"cache_mtime"} and $self->{"cache"} >= 1
!ll&&!rl&&r
40080208
MC/DC66%2/3
defined $fi->{"cache_mtime"} and $self->{"cache"} >= 1 and $threshold >= $fi->{"orig_mtime"}
defined $fi->{"cache_mtime"} $self->{"cache"} >= 1 $threshold >= $fi->{"orig_mtime"}
Truth table75%3/4
A and B and C
A= defined $fi->{"cache_mtime"}B= $self->{"cache"} >= 1C= $threshold >= $fi->{"orig_mtime"}
ABCresult
0XX0
10X0
1100
1111
479 and $threshold >= $fi->{orig_mtime} )) {
480 4041 $self->note( " _load_compiled: no fresh cache: %s, %s",
481 $threshold || 0, Text::Xslate::Util::p($fi) ) if _DUMP_LOAD;
482 4041 $fi->{cache_mtime} = undef;
483 4041 return undef;
484 }
485  
486 175 my $cachepath = $fi->{cachepath};
487 175 ▶ open my($in), '<:raw', $cachepath
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:raw", $cachepath or $self->_error("LoadError: Cannot open $cachepath for reading: $!") 175 0 175
488 or $self->_error("LoadError: Cannot open $cachepath for reading: $!");
489  
490 175 my $magic = $self->_magic_token($fi->{fullpath});
491 175 my $data;
492 175 read $in, $data, length($magic);
493 175 ▶ if($data ne $magic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne $magic) { } 32 143 175
494 32 return undef;
495 }
496 else {
497 143 local $/;
498 143 $data = <$in>;
499 143 close $in;
500 }
501 143 my $unpacker = Data::MessagePack::Unpacker->new();
502 143 my $offset = $unpacker->execute($data);
503 143 my $is_utf8 = $unpacker->data();
504 143 $unpacker->reset();
505  
506 143 $unpacker->utf8($is_utf8);
507  
508 143 my @asm;
509 143 ▶ if($is_utf8) { # TODO: move to XS?
Branch: 1 branch
BranchTrueFalseTotal
if ($is_utf8) 121 22 143
510 121 my $seed = "";
511 121 utf8::upgrade($seed);
512 121 push @asm, ['print_raw_s', $seed, __LINE__, __FILE__];
513 }
514 143 ▶ while($offset < length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $data) 2604 141 2745
515 2604 $offset = $unpacker->execute($data, $offset);
516 2604 my $c = $unpacker->data();
517 2604 $unpacker->reset();
518  
519 # my($name, $arg, $line, $file, $symbol) = @{$c};
520 2604 ▶ if($c->[0] eq 'depend') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->[0] eq 'depend') { } 12 2592 2604
elsif ($c->[0] eq 'literal') { } 10 2582 2592
521 12 my $dep_mtime = (stat $c->[1])[_ST_MTIME];
522 12 ▶ if(!defined $dep_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dep_mtime) 0 12 12
523 0 Carp::carp("Xslate: Failed to stat $c->[1] (ignored): $!");
524 0 return undef; # purge the cache
525 }
526 12 ▶ if($dep_mtime > $threshold){
Branch: 1 branch
BranchTrueFalseTotal
if ($dep_mtime > $threshold) 2 10 12
527 2 $self->note(" _load_compiled: %s(%s) is newer than %s(%s)\n",
528 $c->[1], scalar localtime($dep_mtime),
529 $cachepath, scalar localtime($threshold) )
530 if _DUMP_LOAD;
531 2 return undef; # purge the cache
532 }
533 }
534 elsif($c->[0] eq 'literal') {
535 # force upgrade to avoid UTF-8 key issues
536 10 ▶ utf8::upgrade($c->[1]) if($is_utf8);
Branch: 1 branch
BranchTrueFalseTotal
if $is_utf8 6 4 10
537 }
538 2602 push @asm, $c;
539 }
540  
541 141 if(_DUMP_LOAD) {
542 $self->note(" _load_compiled: cache(mtime=%s)\n",
543 defined $fi->{cache_mtime} ? $fi->{cache_mtime} : 'undef');
544 }
545  
546 141 return \@asm;
547 }
548  
549 193 ▶sub _save_compiled {
Subroutine _save_compiled: called CC=5 SCAR=16.1
550 193 my($self, $out, $asm, $fullpath, $is_utf8) = @_;
551 193 my $mp = Data::MessagePack->new();
552 193 local $\;
553 193 print $out $self->_magic_token($fullpath);
554 193 ▶ print $out $mp->pack($is_utf8 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$is_utf8 ? : 149 44 193
555  
556 193 my $newest_mtime = 0;
557 193 foreach my $c(@{$asm}) {
558 3171 print $out $mp->pack($c);
559  
560 3171 ▶ if ($c->[0] eq 'depend') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[0] eq "depend") 42 3129 3171
561 42 my $dep_mtime = (stat $c->[1])[_ST_MTIME];
562 42 ▶ if ($newest_mtime < $dep_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newest_mtime < $dep_mtime) 34 8 42
563 34 $newest_mtime = $dep_mtime;
564 }
565 }
566 }
567 193 return $newest_mtime;
568 }
569  
570 368 ▶sub _magic_token {
Subroutine _magic_token: called CC=4 SCAR=14.0
571 368 my($self, $fullpath) = @_;
572  
573 $self->{serial_opt} ||= Data::MessagePack->pack([
574 ref($self->{compiler}) || $self->{compiler},
575 $self->_filter_options_for_magic_token($self->_extract_options($self->parser_option)),
576 $self->_filter_options_for_magic_token($self->_extract_options($self->compiler_option)),
577 $self->input_layer,
578 368 ▶ [sort keys %{ $self->{function} }],
Condition66%4/6
$self->{"serial_opt"} ||= "Data::MessagePack"->pack([ref $self->{"compiler"} || $self->{"compiler"}, $self->_filter_options_for_magic_token($self->_extract_options($self->parser_option)), $self->_filter_options_for_magic_token($self->_extract_options($self->compiler_option)), $self->input_layer, [sort(keys %{$$self{"function"};})]])
l!l&&r!l&&!r
1552130
ref $self->{'compiler'} || $self->{'compiler'}
l!l&&r!l&&!r
971160
MC/DC0%0/2
$self->{"serial_opt"} ||= "Data::MessagePack"->pack([ref $self->{"compiler"} || $self->{"compiler"}, $self->_filter_options_for_magic_token($self->_extract_options($self->parser_option)), $self->_filter_options_for_magic_token($self->_extract_options($self->compiler_option)), $self->input_layer, [sort(keys %{$$self{"function"};})]])
$self->{"serial_opt"} "Data::MessagePack"->pack([ref $self->{"compiler"} || $self->{"compiler"}, $self->_filter_options_for_magic_token($self->_extract_options($self->parser_option)), $self->_filter_options_for_magic_token($self->_extract_options($self->compiler_option)), $self->input_layer, [sort(keys %{$$self{"function"};})]])
MC/DC0%0/2
ref $self->{'compiler'} || $self->{'compiler'}
ref $self->{'compiler'} $self->{'compiler'}
Truth table66%2/3
A ||= B
A= $self->{"serial_opt"}B= "Data::MessagePack"->pack([ref $self->{"compiler"} || $self->{"compiler"}, $self->_filter_options_for_magic_token($self->_extract_options($self->parser_option)), $self->_filter_options_for_magic_token($self->_extract_options($self->compiler_option)), $self->input_layer, [sort(keys %{$$self{"function"};})]])
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $self->{'compiler'}B= $self->{'compiler'}
ABresult
1X1
011
000
579 ]);
580  
581 368 ▶ if(ref $fullpath) { # ref to content string
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fullpath) 72 296 368
582 $fullpath = join ':', ref($fullpath),
583 72 $self->_digest(${$fullpath});
584 }
585 368 return sprintf $XSLATE_MAGIC, $fullpath, $self->{serial_opt};
586 }
587  
588 72 ▶sub _digest {
Subroutine _digest: called CC=1 SCAR=0.0
589 72 my($self, $content) = @_;
590 72 require 'Digest/MD5.pm'; # we don't want to create its namespace
591 72 my $md5 = Digest::MD5->new();
592 72 utf8::encode($content);
593 72 $md5->add($content);
594 72 return $md5->hexdigest();
595 }
596  
597 1396 ▶sub _extract_options {
Subroutine _extract_options: called CC=3 SCAR=11.0
598 1396 my($self, $opt_ref) = @_;
599 1396 my @options;
600 1396 foreach my $name(sort keys %{$opt_ref}) {
601 5596 ▶ if(exists $self->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$name}) 275 5321 5596
602 275 push @options, $name => $self->{$name};
603 }
604 }
605 1396 return @options;
606 }
607  
608 426 ▶sub _filter_options_for_magic_token {
Subroutine _filter_options_for_magic_token: called CC=2 SCAR=6.9
609 426 my($self, @options) = @_;
610 426 my @filterd_options;
611 426 ▶ while (@options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@options) 56 426 482
612 56 my $name = shift @options;
613 56 my $value = $self->replace_option_value_for_magic_token($name, shift @options);
614 56 push(@filterd_options, $name => $value);
615 }
616 426 @filterd_options;
617 }
618  
619  
620  
621 7000 ▶sub _compiler {
Subroutine _compiler: called CC=2 SCAR=6.9
622 7000 my($self) = @_;
623 7000 my $compiler = $self->{compiler};
624  
625 7000 ▶ if(!ref $compiler){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $compiler) 485 6515 7000
626 485 require Mouse;
627 485 Mouse::load_class($compiler);
628  
629 485 my $input_layer = $self->input_layer;
630 485 $compiler = $compiler->new(
631 engine => $self,
632 input_layer => $input_layer,
633 $self->_extract_options($self->compiler_option),
634 parser_option => {
635 input_layer => $input_layer,
636 $self->_extract_options($self->parser_option),
637 },
638 );
639  
640 485 $compiler->define_function(keys %{ $self->{function} });
641  
642 485 $self->{compiler} = $compiler;
643 }
644  
645 7000 return $compiler;
646 }
647  
648 6986 ▶sub compile {
Subroutine compile: called CC=1 SCAR=0.0
649 6986 my $self = shift;
650 return $self->_compiler->compile(@_,
651 6986 omit_augment => $self->{omit_augment});
652 }
653  
654 20 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
655 20 die make_error(@_);
656 }
657  
658 0 ▶sub note {
Subroutine note: not called CC=1 SCAR=6.9
659 0 my($self, @args) = @_;
660 0 printf STDERR @args;
661 }
662  
663 package Text::Xslate;
664 378 1;
665 __END__
« blib/lib/Text/Xslate/PP.pm ↑ Summary blib/lib/Text/Xslate/Util.pm »