lib/Template/Provider.pm

filter: Statementstmt 92.3% 363 / 393 Branchbran 70.3% 173 / 246 Conditioncond 62.0% 62 / 100 MC/DCmcdc 39.6% 23 / 58 Subroutinesub 97.4% 38 / 39 Podpod 100% 5 / 5 totaltotal 78.9% 664 / 841 CC 162 SCAR 27.0
CC
162
Cov
81%
_compile
30.6
_init
28.1
_store
23.8
CRAP 14.9
« lib/Template/Plugins.pm ↑ Summary lib/Template/Namespace/Constants.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Provider
4 #
5 # DESCRIPTION
6 # This module implements a class which handles the loading, compiling
7 # and caching of templates. Multiple Template::Provider objects can
8 # be stacked and queried in turn to effect a Chain-of-Command between
9 # them. A provider will attempt to return the requested template,
10 # an error (STATUS_ERROR) or decline to provide the template
11 # (STATUS_DECLINE), allowing subsequent providers to attempt to
12 # deliver it. See 'Design Patterns' for further details.
13 #
14 # AUTHORS
15 # Andy Wardley <abw@wardley.org>
16 #
17 # Refactored by Bill Moseley for v2.19 to add negative caching (i.e.
18 # tracking templates that are NOTFOUND so that we can decline quickly)
19 # and to provide better support for subclassing the provider.
20 #
21 # COPYRIGHT
22 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
23 #
24 # This module is free software; you can redistribute it and/or
25 # modify it under the same terms as Perl itself.
26 #
27 # WARNING:
28 # This code is ugly and contorted and is being totally re-written for TT3.
29 # In particular, we'll be throwing errors rather than messing around
30 # returning (value, status) pairs. With the benefit of hindsight, that
31 # was a really bad design decision on my part. I deserve to be knocked
32 # to the ground and kicked around a bit by hoards of angry TT developers
33 # for that one. Bill's refactoring has made the module easier to subclass,
34 # (so you can ease off the kicking now), but it really needs to be totally
35 # redesigned and rebuilt from the ground up along with the bits of TT that
36 # use it. -- abw 2007/04/27
37 #============================================================================
38  
39 package Template::Provider;
40  
41 117 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 117 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 117 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 117 ▶use Template::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 117 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 117 ▶use Template::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 117 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 117 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 117 ▶use constant PREV => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 117 ▶use constant NAME => 1; # template name -- indexed by this name in LOOKUP
Subroutine BEGIN: called CC=1 SCAR=0.0
52 117 ▶use constant DATA => 2; # Compiled template
Subroutine BEGIN: called CC=1 SCAR=0.0
53 117 ▶use constant LOAD => 3; # mtime of template
Subroutine BEGIN: called CC=1 SCAR=0.0
54 117 ▶use constant NEXT => 4; # link to next item in cache linked list
Subroutine BEGIN: called CC=1 SCAR=0.0
55 117 ▶use constant STAT => 5; # Time last stat()ed
Subroutine BEGIN: called CC=1 SCAR=0.0
56 117 ▶use constant MSWin32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 117 our $VERSION = '3.106';
59 117 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Provider::DEBUG 117 0 117
60 117 our $ERROR = '';
61  
62 # name of document class
63 117 ▶our $DOCUMENT = 'Template::Document' unless defined $DOCUMENT;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Provider::DOCUMENT 117 0 117
64  
65 # maximum time between performing stat() on file to check staleness
66 117 ▶our $STAT_TTL = 1 unless defined $STAT_TTL;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Provider::STAT_TTL 117 0 117
67  
68 # maximum number of directories in an INCLUDE_PATH, to prevent runaways
69 117 ▶our $MAX_DIRS = 64 unless defined $MAX_DIRS;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Provider::MAX_DIRS 117 0 117
70  
71 # UNICODE is supported in versions of Perl from 5.007 onwards
72 117 ▶our $UNICODE = $] > 5.007 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$] > 5.007 ? : 117 0 117
73  
74 117 my $boms = [
75 'UTF-8' => "\x{ef}\x{bb}\x{bf}",
76 'UTF-32BE' => "\x{0}\x{0}\x{fe}\x{ff}",
77 'UTF-32LE' => "\x{ff}\x{fe}\x{0}\x{0}",
78 'UTF-16BE' => "\x{fe}\x{ff}",
79 'UTF-16LE' => "\x{ff}\x{fe}",
80 ];
81  
82 # regex to match relative paths
83 117 our $RELATIVE_PATH = qr[(?:^|/)\.+/];
84  
85 #========================================================================
86 # -- PUBLIC METHODS --
87 #========================================================================
88  
89 #------------------------------------------------------------------------
90 # fetch($name)
91 #
92 # Returns a compiled template for the name specified by parameter.
93 # The template is returned from the internal cache if it exists, or
94 # loaded and then subsequently cached. The ABSOLUTE and RELATIVE
95 # configuration flags determine if absolute (e.g. '/something...')
96 # and/or relative (e.g. './something') paths should be honoured. The
97 # INCLUDE_PATH is otherwise used to find the named file. $name may
98 # also be a reference to a text string containing the template text,
99 # or a file handle from which the content is read. The compiled
100 # template is not cached in these latter cases given that there is no
101 # filename to cache under. A subsequent call to store($name,
102 # $compiled) can be made to cache the compiled template for future
103 # fetch() calls, if necessary.
104 #
105 # Returns a compiled template or (undef, STATUS_DECLINED) if the
106 # template could not be found. On error (e.g. the file was found
107 # but couldn't be read or parsed), the pair ($error, STATUS_ERROR)
108 # is returned. The TOLERANT configuration option can be set to
109 # downgrade any errors to STATUS_DECLINE.
110 #------------------------------------------------------------------------
111  
112 1631 ▶sub fetch {
Subroutine fetch: called CC=11 SCAR=24.0
Pod documented
113 1631 my ($self, $name) = @_;
114 1631 my ($data, $error);
115  
116  
117 1631 ▶ if (ref $name) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $name) { } 1383 248 1631
elsif ('File::Spec'->file_name_is_absolute($name)) { } 33 215 248
elsif ($name =~ /$Template::Provider::RELATIVE_PATH/o) { } 11 204 215
118 # $name can be a reference to a scalar, GLOB or file handle
119 1383 ($data, $error) = $self->_load($name);
120 1383 ▶ ($data, $error) = $self->_compile($data)
Branch: 1 branch
BranchTrueFalseTotal
unless $error 1383 0 1383
121 unless $error;
122 $data = $data->{ data }
123 1383 ▶ unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 1382 1 1383
124 }
125 elsif (File::Spec->file_name_is_absolute($name)) {
126 # absolute paths (starting '/') allowed if ABSOLUTE set
127 ($data, $error) = $self->{ ABSOLUTE }
128 ? $self->_fetch($name)
129 : $self->{ TOLERANT }
130 33 ▶ ? (undef, Template::Constants::STATUS_DECLINED)
Branch: 2 branches
BranchTrueFalseTotal
$self->{'ABSOLUTE'} ? : 29 4 33
$self->{'TOLERANT'} ? : 2 2 4
131 : ("$name: absolute paths are not allowed (set ABSOLUTE option)",
132 Template::Constants::STATUS_ERROR);
133 }
134 elsif ($name =~ m/$RELATIVE_PATH/o) {
135 # anything starting "./" is relative to cwd, allowed if RELATIVE set
136 ($data, $error) = $self->{ RELATIVE }
137 ? $self->_fetch($name)
138 : $self->{ TOLERANT }
139 11 ▶ ? (undef, Template::Constants::STATUS_DECLINED)
Branch: 2 branches
BranchTrueFalseTotal
$self->{'RELATIVE'} ? : 5 6 11
$self->{'TOLERANT'} ? : 2 4 6
140 : ("$name: relative paths are not allowed (set RELATIVE option)",
141 Template::Constants::STATUS_ERROR);
142 }
143 else {
144 # otherwise, it's a file name relative to INCLUDE_PATH
145 ($data, $error) = $self->{ INCLUDE_PATH }
146 204 ▶ ? $self->_fetch_path($name)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'INCLUDE_PATH'} ? : 204 0 204
147 : (undef, Template::Constants::STATUS_DECLINED);
148 }
149  
150 1631 return ($data, $error);
151 }
152  
153  
154 #------------------------------------------------------------------------
155 # store($name, $data)
156 #
157 # Store a compiled template ($data) in the cached as $name.
158 # Returns compiled template
159 #------------------------------------------------------------------------
160  
161 116 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod documented
162 116 my ($self, $name, $data, $mtime) = @_;
163 116 $self->_store($name, {
164 data => $data,
165 load => 0,
166 mtime => $mtime
167 });
168 }
169  
170  
171 #------------------------------------------------------------------------
172 # load($name)
173 #
174 # Load a template without parsing/compiling it, suitable for use with
175 # the INSERT directive. There's some duplication with fetch() and at
176 # some point this could be reworked to integrate them a little closer.
177 #------------------------------------------------------------------------
178  
179 17 ▶sub load {
Subroutine load: called CC=13 SCAR=25.7
Pod documented
180 17 my ($self, $name) = @_;
181 17 my ($data, $error);
182 17 my $path = $name;
183  
184 17 ▶ if (File::Spec->file_name_is_absolute($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($name)) { } 3 14 17
elsif ($name =~ /$Template::Provider::RELATIVE_PATH/o) { } 2 12 14
185 # absolute paths (starting '/') allowed if ABSOLUTE set
186 $error = "$name: absolute paths are not allowed (set ABSOLUTE option)"
187 3 ▶ unless $self->{ ABSOLUTE };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ABSOLUTE"} 2 1 3
188 }
189 elsif ($name =~ m[$RELATIVE_PATH]o) {
190 # anything starting "./" is relative to cwd, allowed if RELATIVE set
191 $error = "$name: relative paths are not allowed (set RELATIVE option)"
192 2 ▶ unless $self->{ RELATIVE };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"RELATIVE"} 1 1 2
193 }
194 else {
195 INCPATH: {
196 # otherwise, it's a file name relative to INCLUDE_PATH
197 12 ▶ my $paths = $self->paths()
Condition50%1/2
$self->paths || (return $self->error, 255)
l!l
120
MC/DC0%0/1
$self->paths || (return $self->error, 255)
$self->paths
Truth table50%1/2
A
A= $self->paths
Aresult
11
00
198 || return ($self->error(), Template::Constants::STATUS_ERROR);
199  
200 12 foreach my $dir (@$paths) {
201 13 $path = File::Spec->catfile($dir, $name);
202 last INCPATH
203 13 ▶ if defined $self->_template_modified($path);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_template_modified($path) 10 3 13
204 }
205 2 undef $path; # not found
206 }
207 }
208  
209 # Now fetch the content
210 17 ▶ ($data, $error) = $self->_template_content($path)
Branch: 1 branch
BranchTrueFalseTotal
if defined $path and not $error 12 5 17
Condition100%3/3
defined $path and not $error
!ll&&!rl&&r
2312
MC/DC100%2/2
defined $path and not $error
defined $path not $error
Truth table100%3/3
A and not(B)
A= defined $pathB= not $error
ABresult
0X0
100
111
211 if defined $path && !$error;
212  
213 17 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 3 14 17
elsif (not defined $path) { } 2 12 14
214 return $self->{ TOLERANT }
215 3 ▶ ? (undef, Template::Constants::STATUS_DECLINED)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'TOLERANT'} ? : 1 2 3
216 : ($error, Template::Constants::STATUS_ERROR);
217 }
218 elsif (! defined $path) {
219 2 return (undef, Template::Constants::STATUS_DECLINED);
220 }
221 else {
222 12 return ($data, Template::Constants::STATUS_OK);
223 }
224 }
225  
226  
227  
228 #------------------------------------------------------------------------
229 # include_path(\@newpath)
230 #
231 # Accessor method for the INCLUDE_PATH setting. If called with an
232 # argument, this method will replace the existing INCLUDE_PATH with
233 # the new value.
234 #------------------------------------------------------------------------
235  
236 0 ▶sub include_path {
Subroutine include_path: not called CC=2 SCAR=17.9
Pod documented
237 0 my ($self, $path) = @_;
238 0 ▶ $self->{ INCLUDE_PATH } = $path if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 0 0 0
239 0 return $self->{ INCLUDE_PATH };
240 }
241  
242  
243 #------------------------------------------------------------------------
244 # paths()
245 #
246 # Evaluates the INCLUDE_PATH list, ignoring any blank entries, and
247 # calling and subroutine or object references to return dynamically
248 # generated path lists. Returns a reference to a new list of paths
249 # or undef on error.
250 #------------------------------------------------------------------------
251  
252 219 ▶sub paths {
Subroutine paths: called CC=10 SCAR=23.3
Pod documented
253 219 my $self = shift;
254 219 my @ipaths = @{ $self->{ INCLUDE_PATH } };
255 219 my (@opaths, $dpaths, $dir);
256 219 my $count = $MAX_DIRS;
257  
258 219 ▶ while (@ipaths && --$count) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ipaths and --$count) 417 219 636
Condition100%3/3
@ipaths and --$count
!ll&&!rl&&r
2181417
MC/DC100%2/2
@ipaths and --$count
@ipaths --$count
Truth table100%3/3
A and B
A= @ipathsB= --$count
ABresult
0X0
100
111
259 417 ▶ $dir = shift @ipaths || next;
Condition50%1/2
shift @ipaths || (next)
l!l
4170
MC/DC0%0/1
shift @ipaths || (next)
shift @ipaths
Truth table50%1/2
A
A= shift @ipaths
Aresult
11
00
260  
261 # $dir can be a sub or object ref which returns a reference
262 # to a dynamically generated list of search paths.
263  
264 417 ▶ if (ref $dir eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dir eq 'CODE') { } 46 371 417
elsif (ref $dir and UNIVERSAL::can($dir, 'paths')) { } 5 366 371
265 46 eval { $dpaths = &$dir() };
266 46 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 46 46
267 0 chomp $@;
268 0 return $self->error($@);
269 }
270 46 unshift(@ipaths, @$dpaths);
271 46 next;
272 }
273 ▶ elsif (ref($dir) && UNIVERSAL::can($dir, 'paths')) {
Condition66%2/3
ref $dir and UNIVERSAL::can($dir, "paths")
!ll&&!rl&&r
36605
MC/DC50%1/2
ref $dir and UNIVERSAL::can($dir, "paths")
ref $dir UNIVERSAL::can($dir, "paths")
Truth table66%2/3
A and B
A= ref $dirB= UNIVERSAL::can($dir, "paths")
ABresult
0X0
100
111
274 5 ▶ $dpaths = $dir->paths()
Condition50%1/2
$dir->paths || (return $self->error($dir->error))
l!l
50
MC/DC0%0/1
$dir->paths || (return $self->error($dir->error))
$dir->paths
Truth table50%1/2
A
A= $dir->paths
Aresult
11
00
275 || return $self->error($dir->error());
276 5 unshift(@ipaths, @$dpaths);
277 5 next;
278 }
279 else {
280 366 push(@opaths, $dir);
281 }
282 }
283 219 ▶ return $self->error("INCLUDE_PATH exceeds $MAX_DIRS directories")
Branch: 1 branch
BranchTrueFalseTotal
if @ipaths 1 218 219
284 if @ipaths;
285  
286 218 return \@opaths;
287 }
288  
289  
290 #------------------------------------------------------------------------
291 # DESTROY
292 #
293 # The provider cache is implemented as a doubly linked list which Perl
294 # cannot free by itself due to the circular references between NEXT <=>
295 # PREV items. This cleanup method walks the list deleting all the NEXT/PREV
296 # references, allowing the proper cleanup to occur and memory to be
297 # repooled.
298 #------------------------------------------------------------------------
299  
300 266 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
301 266 my $self = shift;
302 266 my ($slot, $next);
303  
304 266 $slot = $self->{ HEAD };
305 266 ▶ while ($slot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot) 115 266 381
306 115 $next = $slot->[ NEXT ];
307 115 undef $slot->[ PREV ];
308 115 undef $slot->[ NEXT ];
309 115 $slot = $next;
310 }
311 266 undef $self->{ HEAD };
312 266 undef $self->{ TAIL };
313 }
314  
315  
316  
317  
318 #========================================================================
319 # -- PRIVATE METHODS --
320 #========================================================================
321  
322 #------------------------------------------------------------------------
323 # _init()
324 #
325 # Initialise the cache.
326 #------------------------------------------------------------------------
327  
328 270 ▶sub _init {
Subroutine _init: called CC=23 SCAR=33.4
329 270 my ($self, $params) = @_;
330 270 my $size = $params->{ CACHE_SIZE };
331 270 ▶ my $path = $params->{ INCLUDE_PATH } || '.';
Condition100%2/2
$params->{'INCLUDE_PATH'} || '.'
l!l
75195
MC/DC100%1/1
$params->{'INCLUDE_PATH'} || '.'
$params->{'INCLUDE_PATH'}
Truth table100%2/2
A
A= $params->{'INCLUDE_PATH'}
Aresult
11
00
332 270 ▶ my $cdir = $params->{ COMPILE_DIR } || '';
Condition100%2/2
$params->{'COMPILE_DIR'} || ''
l!l
8262
MC/DC100%1/1
$params->{'COMPILE_DIR'} || ''
$params->{'COMPILE_DIR'}
Truth table100%2/2
A
A= $params->{'COMPILE_DIR'}
Aresult
11
00
333 270 my $dlim = $params->{ DELIMITER };
334 270 my $debug;
335  
336 # tweak delim to ignore C:/
337 270 ▶ $dlim //= MSWin32 ? qr/:(?!\\|\/)/ : qr/:/;
Condition50%1/2
$dlim //= qr/:/
l!l
0270
MC/DC0%0/1
$dlim //= qr/:/
$dlim
Truth table50%1/2
A
A= $dlim
Aresult
11
00
338  
339 # coerce INCLUDE_PATH to an array ref, if not already so
340 270 ▶ $path = [ split(/$dlim/, $path) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq "ARRAY" 262 8 270
341 unless ref $path eq 'ARRAY';
342  
343 # don't allow a CACHE_SIZE 1 because it breaks things and the
344 # additional checking isn't worth it
345 270 ▶ $size = 2
Branch: 1 branch
BranchTrueFalseTotal
if defined $size and $size == 1 || $size < 0 0 270 270
Condition16%1/6
defined $size and $size == 1 || $size < 0
!ll&&!rl&&r
27000
$size == 1 or $size < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $size and $size == 1 || $size < 0
defined $size $size == 1 $size < 0
Truth table25%1/4
A and B or C
A= defined $sizeB= $size == 1C= $size < 0
ABCresult
0XX0
1000
11X1
1011
346 if defined $size && ($size == 1 || $size < 0);
347  
348 270 ▶ if (defined ($debug = $params->{ DEBUG })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($debug = $params->{'DEBUG'})) { } 21 249 270
349 21 $self->{ DEBUG } = $debug & ( Template::Constants::DEBUG_PROVIDER
350 | Template::Constants::DEBUG_FLAGS );
351 }
352 else {
353 249 $self->{ DEBUG } = $DEBUG;
354 }
355  
356 270 ▶ if ($self->{ DEBUG }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DEBUG"}) 0 270 270
357 0 local $" = ', ';
358 0 ▶ $self->debug(
Branch: 1 branch
BranchTrueFalseTotal
defined $size ? : 0 0 0
359 "creating cache of ",
360 defined $size ? $size : 'unlimited',
361 " slots for [ @$path ]"
362 );
363 }
364  
365 # create COMPILE_DIR and sub-directories representing each INCLUDE_PATH
366 # element in which to store compiled files
367 270 ▶ if ($cdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cdir) 8 262 270
368 8 require File::Path;
369 8 foreach my $dir (@$path) {
370 8 ▶ next if ref $dir;
Branch: 1 branch
BranchTrueFalseTotal
if ref $dir 0 8 8
371 8 my $wdir = $dir;
372 8 $wdir =~ tr[:][]d if MSWin32;
373 {
374 117 ▶ no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
375 0 ▶ $wdir = each %{ { $wdir => undef } } if ${^TAINT}; #untaint
Branch: 1 branch
BranchTrueFalseTotal
if ${^TAINT} 0 8 8
376 }
377 8 $wdir = File::Spec->catfile($cdir, $wdir);
378 8 ▶ File::Path::mkpath($wdir) unless -d $wdir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $wdir 3 5 8
379 }
380 }
381  
382 270 $self->{ LOOKUP } = { };
383 270 $self->{ NOTFOUND } = { }; # Tracks templates *not* found.
384 270 $self->{ SLOTS } = 0;
385 270 $self->{ SIZE } = $size;
386 270 $self->{ INCLUDE_PATH } = $path;
387 270 $self->{ DELIMITER } = $dlim;
388 270 $self->{ COMPILE_DIR } = $cdir;
389 270 ▶ $self->{ COMPILE_EXT } = $params->{ COMPILE_EXT } || '';
Condition100%2/2
$params->{'COMPILE_EXT'} || ''
l!l
12258
MC/DC100%1/1
$params->{'COMPILE_EXT'} || ''
$params->{'COMPILE_EXT'}
Truth table100%2/2
A
A= $params->{'COMPILE_EXT'}
Aresult
11
00
390 270 ▶ $self->{ ABSOLUTE } = $params->{ ABSOLUTE } || 0;
Condition100%2/2
$params->{'ABSOLUTE'} || 0
l!l
9261
MC/DC100%1/1
$params->{'ABSOLUTE'} || 0
$params->{'ABSOLUTE'}
Truth table100%2/2
A
A= $params->{'ABSOLUTE'}
Aresult
11
00
391 270 ▶ $self->{ RELATIVE } = $params->{ RELATIVE } || 0;
Condition100%2/2
$params->{'RELATIVE'} || 0
l!l
5265
MC/DC100%1/1
$params->{'RELATIVE'} || 0
$params->{'RELATIVE'}
Truth table100%2/2
A
A= $params->{'RELATIVE'}
Aresult
11
00
392 270 ▶ $self->{ TOLERANT } = $params->{ TOLERANT } || 0;
Condition100%2/2
$params->{'TOLERANT'} || 0
l!l
1269
MC/DC100%1/1
$params->{'TOLERANT'} || 0
$params->{'TOLERANT'}
Truth table100%2/2
A
A= $params->{'TOLERANT'}
Aresult
11
00
393 270 ▶ $self->{ DOCUMENT } = $params->{ DOCUMENT } || $DOCUMENT;
Condition33%1/3
$params->{'DOCUMENT'} || $Template::Provider::DOCUMENT
l!l&&r!l&&!r
02700
MC/DC0%0/2
$params->{'DOCUMENT'} || $Template::Provider::DOCUMENT
$params->{'DOCUMENT'} $Template::Provider::DOCUMENT
Truth table33%1/3
A || B
A= $params->{'DOCUMENT'}B= $Template::Provider::DOCUMENT
ABresult
1X1
011
000
394 270 $self->{ PARSER } = $params->{ PARSER };
395 270 $self->{ DEFAULT } = $params->{ DEFAULT };
396 270 $self->{ ENCODING } = $params->{ ENCODING };
397 # $self->{ PREFIX } = $params->{ PREFIX };
398 270 ▶ $self->{ STAT_TTL } = $params->{ STAT_TTL } || $STAT_TTL;
Condition33%1/3
$params->{'STAT_TTL'} || $Template::Provider::STAT_TTL
l!l&&r!l&&!r
02700
MC/DC0%0/2
$params->{'STAT_TTL'} || $Template::Provider::STAT_TTL
$params->{'STAT_TTL'} $Template::Provider::STAT_TTL
Truth table33%1/3
A || B
A= $params->{'STAT_TTL'}B= $Template::Provider::STAT_TTL
ABresult
1X1
011
000
399 270 $self->{ PARAMS } = $params;
400  
401 # look for user-provided UNICODE parameter or use default from package var
402 $self->{ UNICODE } = defined $params->{ UNICODE }
403 270 ▶ ? $params->{ UNICODE } : $UNICODE;
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'UNICODE'} ? : 0 270 270
404  
405 270 return $self;
406 }
407  
408  
409 #------------------------------------------------------------------------
410 # _fetch($name, $t_name)
411 #
412 # Fetch a file from cache or disk by specification of an absolute or
413 # relative filename. No search of the INCLUDE_PATH is made. If the
414 # file is found and loaded, it is compiled and cached.
415 # Call with:
416 # $name = path to search (possible prefixed by INCLUDE_PATH)
417 # $t_name = template name
418 #------------------------------------------------------------------------
419  
420 339 ▶sub _fetch {
Subroutine _fetch: called CC=12 SCAR=25.1
421 339 my ($self, $name, $t_name) = @_;
422 339 my $stat_ttl = $self->{ STAT_TTL };
423  
424 339 ▶ $self->debug("_fetch($name)") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 339 339
425  
426 # First see if the named template is in the memory cache
427 339 ▶ if ((my $slot = $self->{ LOOKUP }->{ $name })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $slot = $self->{"LOOKUP"}{$name}) 79 260 339
428 # Test if cache is fresh, and reload/compile if not.
429 79 my ($data, $error) = $self->_refresh($slot);
430  
431 79 ▶ return $error
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 1 78 79
432 ? ( $data, $error ) # $data may contain error text
433 : $slot->[ DATA ]; # returned document object
434 }
435  
436 # Otherwise, see if we already know the template is not found
437 260 ▶ if (my $last_stat_time = $self->{ NOTFOUND }->{ $name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last_stat_time = $self->{"NOTFOUND"}{$name}) 56 204 260
438 56 my $expires_in = $last_stat_time + $stat_ttl - time;
439 56 ▶ if ($expires_in > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires_in > 0) { } 56 0 56
440 $self->debug(" file [$name] in negative cache. Expires in $expires_in seconds")
441 56 ▶ if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 56 56
442 56 return (undef, Template::Constants::STATUS_DECLINED);
443 }
444 else {
445 0 delete $self->{ NOTFOUND }->{ $name };
446 }
447 }
448  
449 204 my($template,$error);
450 204 my $uncompiled_template_mtime = $self->_template_modified( $name ); # does template exist?
451  
452 # some templates like Provider::FromDATA does not provide mtime information
453 204 ▶ $uncompiled_template_mtime //= 0;
Condition100%2/2
$uncompiled_template_mtime //= 0
l!l
11886
MC/DC100%1/1
$uncompiled_template_mtime //= 0
$uncompiled_template_mtime
Truth table100%2/2
A
A= $uncompiled_template_mtime
Aresult
11
00
454  
455 # Is there an up-to-date compiled version on disk?
456 204 ▶ if (my $template_mtime = $self->_compiled_is_current($name, $uncompiled_template_mtime)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $template_mtime = $self->_compiled_is_current($name, $uncompiled_template_mtime)) 15 189 204
457 # require() the compiled template.
458 15 my $compiled_template = $self->_load_compiled( $self->_compiled_filename($name) );
459  
460 # Store and return the compiled template
461 15 ▶ return $self->store( $name, $compiled_template, $template_mtime ) if $compiled_template;
Branch: 1 branch
BranchTrueFalseTotal
if $compiled_template 15 0 15
462  
463 # Problem loading compiled template:
464 # warn and continue to fetch source template
465 0 warn($self->error(), "\n");
466 }
467  
468 # load template from source
469 189 ($template, $error) = $self->_load($name, $t_name);
470  
471 189 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 87 102 189
472 # Template could not be fetched. Add to the negative/notfound cache.
473 87 $self->{ NOTFOUND }->{ $name } = time;
474 87 return ( $template, $error );
475 }
476  
477 # compile template source
478 102 ($template, $error) = $self->_compile($template, $self->_compiled_filename($name) );
479  
480 102 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 1 101 102
481 # return any compile time error
482 1 return ($template, $error);
483 }
484 else {
485 # Store compiled template and return it
486 101 return $self->store($name, $template->{data}) ;
487 }
488 }
489  
490  
491 #------------------------------------------------------------------------
492 # _fetch_path($name)
493 #
494 # Fetch a file from cache or disk by specification of an absolute cache
495 # name (e.g. 'header') or filename relative to one of the INCLUDE_PATH
496 # directories. If the file isn't already cached and can be found and
497 # loaded, it is compiled and cached under the full filename.
498 #------------------------------------------------------------------------
499  
500 207 ▶sub _fetch_path {
Subroutine _fetch_path: called CC=11 SCAR=25.0
501 207 my ($self, $name) = @_;
502  
503 207 ▶ $self->debug("_fetch_path($name)") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 207 207
504  
505 # the template may have been stored using a non-filename name
506 # so look for the plain name in the cache first
507 207 ▶ if ((my $slot = $self->{ LOOKUP }->{ $name })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $slot = $self->{"LOOKUP"}{$name}) 0 207 207
508 # cached entry exists, so refresh slot and extract data
509 0 my ($data, $error) = $self->_refresh($slot);
510  
511 0 ▶ return $error
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
512 ? ($data, $error)
513 : ($slot->[ DATA ], $error );
514 }
515  
516 207 ▶ my $paths = $self->paths
Condition100%2/2
$self->paths || (return $self->error, 255)
l!l
2061
MC/DC100%1/1
$self->paths || (return $self->error, 255)
$self->paths
Truth table100%2/2
A
A= $self->paths
Aresult
11
00
517 || return ( $self->error, Template::Constants::STATUS_ERROR );
518  
519 # search the INCLUDE_PATH for the file, in cache or on disk
520 206 foreach my $dir (@$paths) {
521 305 my $path = File::Spec->catfile($dir, $name);
522  
523 305 ▶ $self->debug("searching path: $path\n") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 305 305
524  
525 305 my ($data, $error) = $self->_fetch( $path, $name );
526  
527 # Return if no error or if a serious error.
528 305 ▶ return ( $data, $error )
Branch: 1 branch
BranchTrueFalseTotal
if not $error or $error == 255 164 141 305
Condition100%3/3
not $error or $error == 255
l!l&&r!l&&!r
1613141
MC/DC100%2/2
not $error or $error == 255
not $error $error == 255
Truth table100%3/3
not(A) or B
A= not $errorB= $error == 255
ABresult
1X1
011
000
529 if !$error || $error == Template::Constants::STATUS_ERROR;
530  
531 }
532  
533 # not found in INCLUDE_PATH, now try DEFAULT
534 return $self->_fetch_path( $self->{DEFAULT} )
535 42 ▶ if defined $self->{DEFAULT} && $name ne $self->{DEFAULT};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"DEFAULT"} and $name ne $self->{"DEFAULT"} 3 39 42
Condition100%3/3
defined $self->{"DEFAULT"} and $name ne $self->{"DEFAULT"}
!ll&&!rl&&r
3723
MC/DC100%2/2
defined $self->{"DEFAULT"} and $name ne $self->{"DEFAULT"}
defined $self->{"DEFAULT"} $name ne $self->{"DEFAULT"}
Truth table100%3/3
A and B
A= defined $self->{"DEFAULT"}B= $name ne $self->{"DEFAULT"}
ABresult
0X0
100
111
536  
537 # We could not handle this template name
538 39 return (undef, Template::Constants::STATUS_DECLINED);
539 }
540  
541 324 ▶sub _compiled_filename {
Subroutine _compiled_filename: called CC=6 SCAR=17.9
542 324 my ($self, $file) = @_;
543  
544 324 ▶ return $self->{ COMPILEDPATH }{$file} if $self->{ COMPILEDPATH }{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"COMPILEDPATH"}{$file} 22 302 324
545  
546 302 my ($compext, $compdir) = @$self{ qw( COMPILE_EXT COMPILE_DIR ) };
547 302 my ($path, $compiled);
548  
549 return undef
550 302 ▶ unless $compext || $compdir;
Branch: 1 branch
BranchTrueFalseTotal
unless $compext or $compdir 254 48 302
Condition100%3/3
$compext or $compdir
l!l&&r!l&&!r
453254
MC/DC100%2/2
$compext or $compdir
$compext $compdir
Truth table100%3/3
A or B
A= $compextB= $compdir
ABresult
1X1
011
000
551  
552 48 $path = $file;
553 48 ▶ $path or die "invalid filename: $path";
Branch: 1 branch
BranchTrueFalseTotal
$path or die "invalid filename: $path" 48 0 48
554 48 $path =~ tr[:][]d if MSWin32;
555  
556  
557 48 $compiled = "$path$compext";
558 48 ▶ $self->{ COMPILEDPATH }{$file} = $compiled = File::Spec->catfile($compdir, $compiled) if length $compdir;
Branch: 1 branch
BranchTrueFalseTotal
if length $compdir 23 25 48
559  
560 48 return $compiled;
561 }
562  
563 16 ▶sub _load_compiled {
Subroutine _load_compiled: called CC=3 SCAR=11.0
564 16 my ($self, $file) = @_;
565  
566 # Implicitly Relative paths are not supported
567 # by "require" and invoke @INC traversal, where relative
568 # paths only traditionally worked prior to Perl 5.26
569 # due to the presence of '.' in @INC
570 #
571 # Given load_compiled never wants to traverse @INC, forcing
572 # an absolute path for the loaded file and the INC key is
573 # sensible.
574 #
575 # NB: %INC Keys are always identical to their respective
576 # "require" invocations regardless of OS, and the only time
577 # one needs to care about slash direction is when dealing
578 # with Module::Name -> Module/Name.pm translation.
579 16 my $fpath = File::Spec->rel2abs( $file );
580  
581 16 ▶ return $self->error("compiled template missing path") unless defined $fpath;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fpath 0 16 16
582  
583 16 ($fpath) = $fpath =~ /^(.*)$/s;
584  
585 # load compiled template via require(); we zap any
586 # %INC entry to ensure it is reloaded (we don't
587 # want 1 returned by require() to say it's in memory)
588 16 delete $INC{ $fpath };
589 16 my $compiled;
590 16 eval { $compiled = require $fpath; };
591 16 ▶ return $@
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 1 15 16
592 ? $self->error("compiled template $fpath: $@")
593 : $compiled;
594 }
595  
596 #------------------------------------------------------------------------
597 # _load($name, $alias)
598 #
599 # Load template text from a string ($name = scalar ref), GLOB or file
600 # handle ($name = ref), or from an absolute filename ($name = scalar).
601 # Returns a hash array containing the following items:
602 # name filename or $alias, if provided, or 'input text', etc.
603 # text template text
604 # time modification time of file, or current time for handles/strings
605 # load time file was loaded (now!)
606 #
607 # On error, returns ($error, STATUS_ERROR), or (undef, STATUS_DECLINED)
608 # if TOLERANT is set.
609 #------------------------------------------------------------------------
610  
611 1575 ▶sub _load {
Subroutine _load: called CC=16 SCAR=28.9
612 1575 my ($self, $name, $alias) = @_;
613 1575 my ($data, $error);
614 1575 my $tolerant = $self->{ TOLERANT };
615 1575 my $now = time;
616  
617 1575 ▶ $alias = $name unless defined $alias or ref $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alias or ref $name 14 1561 1575
Condition100%3/3
defined $alias or ref $name
l!l&&r!l&&!r
178138314
MC/DC100%2/2
defined $alias or ref $name
defined $alias ref $name
Truth table100%3/3
A or B
A= defined $aliasB= ref $name
ABresult
1X1
011
000
618  
619 $self->debug("_load($name, ", $alias // '<no alias>',
620 1575 ▶ ')') if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1575 1575
Condition0%0/2
$alias // "<no alias>"
l!l
00
MC/DC0%0/1
$alias // "<no alias>"
$alias
Truth table0%0/2
A
A= $alias
Aresult
11
00
621  
622 # SCALAR ref is the template text
623 1575 ▶ if (ref $name eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name eq "SCALAR") 1380 195 1575
624 # $name can be a SCALAR reference to the input text...
625 return {
626 1380 ▶ name => $alias // 'input text',
Condition50%2/4
$alias // "input text"
l!l
01380
$alias // "input text"
l!l
01380
MC/DC0%0/1
$alias // "input text"
$alias
MC/DC0%0/1
$alias // "input text"
$alias
Truth table50%1/2
A
A= $alias
Aresult
11
00
Truth table50%1/2
A
A= $alias
Aresult
11
00
627 path => $alias // 'input text',
628 text => $$name,
629 time => $now,
630 load => 0,
631 };
632 }
633  
634 # Otherwise, assume GLOB as a file handle
635 195 ▶ if (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) 3 192 195
636 3 local $/;
637 3 my $text = <$name>;
638 3 ▶ $text = $self->_decode_unicode($text) if $self->{ UNICODE };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"UNICODE"} 3 0 3
639 return {
640 3 ▶ name => $alias // 'input file handle',
Condition50%2/4
$alias // "input file handle"
l!l
03
$alias // "input file handle"
l!l
03
MC/DC0%0/1
$alias // "input file handle"
$alias
MC/DC0%0/1
$alias // "input file handle"
$alias
Truth table50%1/2
A
A= $alias
Aresult
11
00
Truth table50%1/2
A
A= $alias
Aresult
11
00
641 path => $alias // 'input file handle',
642 text => $text,
643 time => $now,
644 load => 0,
645 };
646 }
647  
648 # Otherwise, it's the name of the template
649 192 ▶ if ( defined $self->_template_modified( $name ) ) { # does template exist?
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_template_modified($name)) 106 86 192
650 106 my ($text, $error, $mtime ) = $self->_template_content( $name );
651 106 ▶ unless ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 105 1 106
652 105 ▶ $text = $self->_decode_unicode($text) if $self->{ UNICODE };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"UNICODE"} 105 0 105
653 return {
654 105 name => $alias,
655 path => $name,
656 text => $text,
657 time => $mtime,
658 load => $now,
659 };
660 }
661  
662 1 ▶ return ( $error, Template::Constants::STATUS_ERROR )
Branch: 1 branch
BranchTrueFalseTotal
unless $tolerant 1 0 1
663 unless $tolerant;
664 }
665  
666 # Unable to process template, pass onto the next Provider.
667 86 return (undef, Template::Constants::STATUS_DECLINED);
668 }
669  
670  
671 #------------------------------------------------------------------------
672 # _refresh(\@slot)
673 #
674 # Private method called to mark a cache slot as most recently used.
675 # A reference to the slot array should be passed by parameter. The
676 # slot is relocated to the head of the linked list. If the file from
677 # which the data was loaded has been updated since it was compiled, then
678 # it is re-loaded from disk and re-compiled.
679 #------------------------------------------------------------------------
680  
681 79 ▶sub _refresh {
Subroutine _refresh: called CC=14 SCAR=28.1
682 79 my ($self, $slot) = @_;
683 79 my $stat_ttl = $self->{ STAT_TTL };
684 79 my ($head, $file, $data, $error);
685  
686 $self->debug(
687 "_refresh([ ",
688 0 ▶ join(', ', map { $_ // '<undef>' } @$slot),
Condition0%0/2
$_ // "<undef>"
l!l
00
MC/DC0%0/1
$_ // "<undef>"
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
689 '])'
690 79 ▶ ) if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 79 79
691  
692 # if it's more than $STAT_TTL seconds since we last performed a
693 # stat() on the file then we need to do it again and see if the file
694 # time has changed
695 79 my $now = time;
696 79 my $expires_in_sec = $slot->[ STAT ] + $stat_ttl - $now;
697  
698 79 ▶ if ( $expires_in_sec <= 0 ) { # Time to check!
Branch: 2 branches
BranchTrueFalseTotal
if ($expires_in_sec <= 0) { } 3 76 79
elsif ($self->{'DEBUG'}) { } 0 76 76
699 3 $slot->[ STAT ] = $now;
700  
701 # Grab mtime of template.
702 # Seems like this should be abstracted to compare to
703 # just ask for a newer compiled template (if it's newer)
704 # and let that check for a newer template source.
705 3 my $template_mtime = $self->_template_modified( $slot->[ NAME ] );
706 3 ▶ if ( ! defined $template_mtime || ( $template_mtime != $slot->[ LOAD ] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $template_mtime or $template_mtime != $slot->[3]) 3 0 3
Condition33%1/3
not defined $template_mtime or $template_mtime != $slot->[3]
l!l&&r!l&&!r
030
MC/DC0%0/2
not defined $template_mtime or $template_mtime != $slot->[3]
not defined $template_mtime $template_mtime != $slot->[3]
Truth table33%1/3
not(A) or B
A= not defined $template_mtimeB= $template_mtime != $slot->[3]
ABresult
1X1
011
000
707 $self->debug("refreshing cache file ", $slot->[ NAME ])
708 3 ▶ if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 3 3
709  
710 3 ($data, $error) = $self->_load($slot->[ NAME ], $slot->[ DATA ]->{ name });
711 3 ▶ ($data, $error) = $self->_compile($data, $self->_compiled_filename($slot->[ NAME ]))
Branch: 1 branch
BranchTrueFalseTotal
unless $error 3 0 3
712 unless $error;
713  
714 3 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 1 2 3
715 # if the template failed to load/compile then we wipe out the
716 # STAT entry. This forces the provider to try and reload it
717 # each time instead of using the previously cached version
718 # until $STAT_TTL is next up
719 1 $slot->[ STAT ] = 0;
720 }
721 else {
722 2 $slot->[ DATA ] = $data->{ data };
723 2 $slot->[ LOAD ] = $data->{ time };
724 }
725 }
726  
727 } elsif ( $self->{ DEBUG } ) {
728 0 $self->debug( sprintf('STAT_TTL not met for file [%s]. Expires in %d seconds',
729 $slot->[ NAME ], $expires_in_sec ) );
730 }
731  
732 # Move this slot to the head of the list
733 79 ▶ unless( $self->{ HEAD } == $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"HEAD"} == $slot) 47 32 79
734 # remove existing slot from usage chain...
735 47 ▶ if ($slot->[ PREV ]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot->[0]) { } 47 0 47
736 47 $slot->[ PREV ]->[ NEXT ] = $slot->[ NEXT ];
737 }
738 else {
739 0 $self->{ HEAD } = $slot->[ NEXT ];
740 }
741 47 ▶ if ($slot->[ NEXT ]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot->[4]) { } 18 29 47
742 18 $slot->[ NEXT ]->[ PREV ] = $slot->[ PREV ];
743 }
744 else {
745 29 $self->{ TAIL } = $slot->[ PREV ];
746 }
747  
748 # ..and add to start of list
749 47 $head = $self->{ HEAD };
750 47 ▶ $head->[ PREV ] = $slot if $head;
Branch: 1 branch
BranchTrueFalseTotal
if $head 47 0 47
751 47 $slot->[ PREV ] = undef;
752 47 $slot->[ NEXT ] = $head;
753 47 $self->{ HEAD } = $slot;
754 }
755  
756 79 return ($data, $error);
757 }
758  
759  
760  
761 #------------------------------------------------------------------------
762 # _store($name, $data)
763 #
764 # Private method called to add a data item to the cache. If the cache
765 # size limit has been reached then the oldest entry at the tail of the
766 # list is removed and its slot relocated to the head of the list and
767 # reused for the new data item. If the cache is under the size limit,
768 # or if no size limit is defined, then the item is added to the head
769 # of the list.
770 # Returns compiled template
771 #------------------------------------------------------------------------
772  
773 116 ▶sub _store {
Subroutine _store: called CC=12 SCAR=31.7
774 116 my ($self, $name, $data, $compfile) = @_;
775 116 my $size = $self->{ SIZE };
776 116 my ($slot, $head);
777  
778 # Return if memory cache disabled. (overriding code should also check)
779 # $$$ What's the expected behaviour of store()? Can't tell from the
780 # docs if you can call store() when SIZE = 0.
781 116 ▶ return $data->{data} if defined $size and !$size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $size and not $size 0 116 116
Condition33%1/3
defined $size and not $size
!ll&&!rl&&r
11600
MC/DC0%0/2
defined $size and not $size
defined $size not $size
Truth table33%1/3
A and not(B)
A= defined $sizeB= not $size
ABresult
0X0
100
111
782  
783 # check the modification time -- extra stat here
784 116 ▶ my $load = $data->{ mtime } || $self->_modified($name);
Condition100%3/3
$data->{'mtime'} || $self->_modified($name)
l!l&&r!l&&!r
151001
MC/DC100%2/2
$data->{'mtime'} || $self->_modified($name)
$data->{'mtime'} $self->_modified($name)
Truth table100%3/3
A || B
A= $data->{'mtime'}B= $self->_modified($name)
ABresult
1X1
011
000
785  
786 # extract the compiled template from the data hash
787 116 $data = $data->{ data };
788 116 ▶ $self->debug("_store($name, $data)") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 116 116
789  
790 116 ▶ if (defined $size && $self->{ SLOTS } >= $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and $self->{'SLOTS'} >= $size) { } 0 116 116
Condition33%1/3
defined $size and $self->{"SLOTS"} >= $size
!ll&&!rl&&r
11600
MC/DC0%0/2
defined $size and $self->{"SLOTS"} >= $size
defined $size $self->{"SLOTS"} >= $size
Truth table33%1/3
A and B
A= defined $sizeB= $self->{"SLOTS"} >= $size
ABresult
0X0
100
111
791 # cache has reached size limit, so reuse oldest entry
792 0 ▶ $self->debug("reusing oldest cache entry (size limit reached: $size)\nslots: $self->{ SLOTS }") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 0 0
793  
794 # remove entry from tail of list
795 0 $slot = $self->{ TAIL };
796 0 $slot->[ PREV ]->[ NEXT ] = undef;
797 0 $self->{ TAIL } = $slot->[ PREV ];
798  
799 # remove name lookup for old node
800 0 delete $self->{ LOOKUP }->{ $slot->[ NAME ] };
801  
802 # add modified node to head of list
803 0 $head = $self->{ HEAD };
804 0 ▶ $head->[ PREV ] = $slot if $head;
Branch: 1 branch
BranchTrueFalseTotal
if $head 0 0 0
805 0 @$slot = ( undef, $name, $data, $load, $head, time );
806 0 $self->{ HEAD } = $slot;
807  
808 # add name lookup for new node
809 0 $self->{ LOOKUP }->{ $name } = $slot;
810 }
811 else {
812 # cache is under size limit, or none is defined
813  
814 116 ▶ $self->debug("adding new cache entry") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 116 116
815  
816 # add new node to head of list
817 116 $head = $self->{ HEAD };
818 116 $slot = [ undef, $name, $data, $load, $head, time ];
819 116 ▶ $head->[ PREV ] = $slot if $head;
Branch: 1 branch
BranchTrueFalseTotal
if $head 60 56 116
820 116 $self->{ HEAD } = $slot;
821 116 ▶ $self->{ TAIL } ||= $slot;
Condition66%2/3
$self->{"TAIL"} ||= $slot
l!l&&r!l&&!r
60560
MC/DC0%0/2
$self->{"TAIL"} ||= $slot
$self->{"TAIL"} $slot
Truth table66%2/3
A ||= B
A= $self->{"TAIL"}B= $slot
ABresult
1X1
011
000
822  
823 # add lookup from name to slot and increment nslots
824 116 $self->{ LOOKUP }->{ $name } = $slot;
825 116 $self->{ SLOTS }++;
826 }
827  
828 116 return $data;
829 }
830  
831  
832 #------------------------------------------------------------------------
833 # _compile($data)
834 #
835 # Private method called to parse the template text and compile it into
836 # a runtime form. Creates and delegates a Template::Parser object to
837 # handle the compilation, or uses a reference passed in PARSER. On
838 # success, the compiled template is stored in the 'data' item of the
839 # $data hash and returned. On error, ($error, STATUS_ERROR) is returned,
840 # or (undef, STATUS_DECLINED) if the TOLERANT flag is set.
841 # The optional $compiled parameter may be passed to specify
842 # the name of a compiled template file to which the generated Perl
843 # code should be written. Errors are (for now...) silently
844 # ignored, assuming that failures to open a file for writing are
845 # intentional (e.g directory write permission).
846 #------------------------------------------------------------------------
847  
848 1488 ▶sub _compile {
Subroutine _compile: called CC=21 SCAR=34.2
849 1488 my ($self, $data, $compfile) = @_;
850 1488 my $text = $data->{ text };
851 1488 my ($parsedoc, $error);
852  
853 $self->debug("_compile($data, ",
854 defined $compfile ? $compfile : '<no compfile>', ')')
855 1488 ▶ if $self->{ DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1488 1488
defined $compfile ? : 0 0 0
856  
857 my $parser = $self->{ PARSER }
858 ||= Template::Config->parser($self->{ PARAMS })
859 1488 ▶ || return (Template::Config->error(), Template::Constants::STATUS_ERROR);
Condition60%3/5
$self->{"PARSER"} ||= "Template::Config"->parser($self->{"PARAMS"}) || (return "Template::Config"->error, 255)
l!l&&r!l&&!r
12622260
'Template::Config'->parser($self->{'PARAMS'}) || (return 'Template::Config'->error, 255)
l!l
2260
MC/DC0%0/2
$self->{"PARSER"} ||= "Template::Config"->parser($self->{"PARAMS"}) || (return "Template::Config"->error, 255)
$self->{"PARSER"} 'Template::Config'->parser($self->{'PARAMS'})
Truth table66%2/3
A ||= B
A= $self->{"PARSER"}B= 'Template::Config'->parser($self->{'PARAMS'})
ABresult
1X1
011
000
860  
861 # discard the template text - we don't need it any more
862 1488 delete $data->{ text };
863  
864 # call parser to compile template into Perl code
865 1488 ▶ if ($parsedoc = $parser->parse($text, $data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsedoc = $parser->parse($text, $data)) { } 1485 3 1488
866  
867 $parsedoc->{ METADATA } = {
868 'name' => $data->{ name },
869 'modtime' => $data->{ 'time' },
870 1485 %{ $parsedoc->{ METADATA } },
871 };
872  
873 # write the Perl code to the file $compfile, if defined
874 1485 ▶ if ($compfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compfile) 19 1466 1485
875 19 my $basedir = &File::Basename::dirname($compfile);
876 {
877 117 ▶ no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
878 0 ▶ $basedir = each %{ { $basedir => undef } } if ${^TAINT}; #untaint
Branch: 1 branch
BranchTrueFalseTotal
if ${^TAINT} 0 19 19
879 }
880  
881 19 ▶ unless (-d $basedir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $basedir) 6 13 19
882 6 eval { File::Path::mkpath($basedir) };
883 6 ▶ $error = "failed to create compiled templates directory: $basedir ($@)"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
884 if ($@);
885 }
886  
887 19 ▶ unless ($error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 19 0 19
888 19 my $docclass = $self->{ DOCUMENT };
889 19 ▶ $error = 'cache failed to write '
Branch: 1 branch
BranchTrueFalseTotal
unless $docclass->write_perl_file($compfile, $parsedoc) 0 19 19
890 . &File::Basename::basename($compfile)
891 . ': ' . $docclass->error()
892 unless $docclass->write_perl_file($compfile, $parsedoc);
893 }
894  
895 # set atime and mtime of newly compiled file, don't bother
896 # if time is undef
897 19 ▶ if (!defined($error) && defined $data->{ 'time' }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $error and defined $data->{"time"}) 19 0 19
Condition33%1/3
not defined $error and defined $data->{"time"}
!ll&&!rl&&r
0019
MC/DC0%0/2
not defined $error and defined $data->{"time"}
not defined $error defined $data->{"time"}
Truth table33%1/3
not(A) and B
A= not defined $errorB= defined $data->{"time"}
ABresult
0X0
100
111
898 19 my $cfile = do {
899 117 ▶ no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
900 19 each %{ { $compfile => undef } };
901 };
902 19 ▶ if (!length $cfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $cfile) 0 19 19
903 return(
904 0 "invalid filename: $compfile",
905 Template::Constants::STATUS_ERROR
906 );
907 };
908  
909 19 my $ctime = $data->{ time };
910 19 ▶ if (!length $ctime || $ctime =~ tr{0-9}{}c) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $ctime or $ctime =~ tr/0-9//c) 0 19 19
Condition33%1/3
not length $ctime or $ctime =~ tr/0-9//c
l!l&&r!l&&!r
0019
MC/DC0%0/2
not length $ctime or $ctime =~ tr/0-9//c
not length $ctime $ctime =~ tr/0-9//c
Truth table33%1/3
not(A) or B
A= not length $ctimeB= $ctime =~ tr/0-9//c
ABresult
1X1
011
000
911 return(
912 0 "invalid time: $ctime",
913 Template::Constants::STATUS_ERROR
914 );
915 }
916 19 utime($ctime, $ctime, $cfile);
917  
918 $self->debug(" cached compiled template to file [$compfile]")
919 19 ▶ if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 19 19
920 }
921 }
922  
923 1485 ▶ unless ($error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 1485 0 1485
924 return $data ## RETURN ##
925 1485 ▶ if $data->{ data } = $DOCUMENT->new($parsedoc);
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"data"} = $Template::Provider::DOCUMENT->new($parsedoc) 1485 0 1485
926 0 $error = $Template::Document::ERROR;
927 }
928 }
929 else {
930 3 $error = Template::Exception->new(
931 'parse', "$data->{ name } " .
932 $parser->error()
933 );
934 }
935  
936 # return STATUS_ERROR, or STATUS_DECLINED if we're being tolerant
937 return $self->{ TOLERANT }
938 3 ▶ ? (undef, Template::Constants::STATUS_DECLINED)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'TOLERANT'} ? : 0 3 3
939 : ($error, Template::Constants::STATUS_ERROR)
940 }
941  
942 #------------------------------------------------------------------------
943 # _compiled_is_current( $template_name )
944 #
945 # Returns true if $template_name and its compiled name
946 # exist and they have the same mtime.
947 #------------------------------------------------------------------------
948  
949 204 ▶sub _compiled_is_current {
Subroutine _compiled_is_current: called CC=7 SCAR=19.9
950 204 my ( $self, $template_name, $uncompiled_template_mtime ) = @_;
951  
952 204 my $compiled_name = $self->_compiled_filename($template_name);
953 204 ▶ return unless defined $compiled_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $compiled_name 169 35 204
954  
955 35 my $compiled_mtime = (stat($compiled_name))[9];
956 35 ▶ return unless defined $compiled_mtime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $compiled_mtime 19 16 35
957  
958 16 ▶ my $template_mtime = $uncompiled_template_mtime || $self->_template_modified( $template_name ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $template_mtime = $uncompiled_template_mtime || $self->_template_modified($template_name) or return 16 0 16
Condition33%1/3
$uncompiled_template_mtime || $self->_template_modified($template_name)
l!l&&r!l&&!r
1600
MC/DC0%0/2
$uncompiled_template_mtime || $self->_template_modified($template_name)
$uncompiled_template_mtime $self->_template_modified($template_name)
Truth table33%1/3
A || B
A= $uncompiled_template_mtimeB= $self->_template_modified($template_name)
ABresult
1X1
011
000
959 16 ▶ return unless defined $template_mtime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template_mtime 0 16 16
960  
961 # This was >= in the 2.15, but meant that downgrading
962 # a source template would not get picked up.
963 16 ▶ return $compiled_mtime == $template_mtime ? $template_mtime : 0;
Branch: 1 branch
BranchTrueFalseTotal
$compiled_mtime == $template_mtime ? : 15 1 16
964 }
965  
966  
967 #------------------------------------------------------------------------
968 # _template_modified($path)
969 #
970 # Returns the last modified time of the $path.
971 # Returns undef if the path does not exist.
972 # Override if templates are not on disk, for example
973 #------------------------------------------------------------------------
974  
975 513 ▶sub _template_modified {
Subroutine _template_modified: called CC=2 SCAR=7.1
976 513 my $self = shift;
977 513 ▶ my $template = shift || return;
Condition50%1/2
shift() || (return)
l!l
5130
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
978 513 return (stat( $template ))[9];
979 }
980  
981 #------------------------------------------------------------------------
982 # _template_content($path)
983 #
984 # Fetches content pointed to by $path.
985 # Returns the content in scalar context.
986 # Returns ($data, $error, $mtime) in list context where
987 # $data - content
988 # $error - error string if there was an error, otherwise undef
989 # $mtime - last modified time from calling stat() on the path
990 #------------------------------------------------------------------------
991  
992 118 ▶sub _template_content {
Subroutine _template_content: called CC=5 SCAR=16.4
993 118 my ($self, $path) = @_;
994  
995 118 ▶ return (undef, "No path specified to fetch content from ")
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 118 118
996 unless $path;
997  
998 118 my $data;
999 my $mod_date;
1000 118 my $error;
1001  
1002 118 local *FH;
1003 118 ▶ if(-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 1 117 118
elsif (open FH, '<', $path) { } 117 0 117
1004 1 $error = "$path: not a file";
1005 }
1006 elsif (open(FH, "<", $path)) {
1007 117 local $/;
1008 117 binmode(FH);
1009 117 $data = <FH>;
1010 117 $mod_date = (stat($path))[9];
1011 117 close(FH);
1012 }
1013 else {
1014 0 $error = "$path: $!";
1015 }
1016  
1017 return wantarray
1018 118 ▶ ? ( $data, $error, $mod_date )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 118 0 118
1019 : $data;
1020 }
1021  
1022  
1023 #------------------------------------------------------------------------
1024 # _modified($name)
1025 # _modified($name, $time)
1026 #
1027 # When called with a single argument, it returns the modification time
1028 # of the named template. When called with a second argument it returns
1029 # true if $name has been modified since $time.
1030 #------------------------------------------------------------------------
1031  
1032 101 ▶sub _modified {
Subroutine _modified: called CC=4 SCAR=16.1
1033 101 my ($self, $name, $time) = @_;
1034 101 my $load = $self->_template_modified($name);
1035 101 ▶ return $time ? 1 : 0 unless defined $load;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $load 0 101 101
$time ? : 0 0 0
1036  
1037 101 ▶ return $time
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 0 101 101
1038 ? $load > $time
1039 : $load;
1040 }
1041  
1042 #------------------------------------------------------------------------
1043 # _decode_unicode
1044 #
1045 # Decodes encoded unicode text that starts with a BOM and
1046 # turns it into perl's internal representation
1047 #------------------------------------------------------------------------
1048  
1049 108 ▶sub _decode_unicode {
Subroutine _decode_unicode: called CC=6 SCAR=18.0
1050 108 my $self = shift;
1051 108 my $string = shift;
1052 108 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 108 108
1053  
1054 117 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
1055 108 require Encode;
1056  
1057 108 ▶ return $string if Encode::is_utf8( $string );
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($string) 0 108 108
1058  
1059 # try all the BOMs in order looking for one (order is important
1060 # 32bit BOMs look like 16bit BOMs)
1061  
1062 108 my $count = 0;
1063  
1064 108 ▶ while ($count < @{ $boms }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < @{$boms;}) 530 103 633
1065 530 my $enc = $boms->[$count++];
1066 530 my $bom = $boms->[$count++];
1067  
1068 # does the string start with the bom?
1069 530 ▶ if ($bom eq substr($string, 0, length($bom))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bom eq substr($string, 0, length $bom)) 5 525 530
1070 # decode it and hand it back
1071 5 return Encode::decode($enc, substr($string, length($bom)), 1);
1072 }
1073 }
1074  
1075 return $self->{ ENCODING }
1076 103 ▶ ? Encode::decode( $self->{ ENCODING }, $string )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ENCODING'} ? : 0 103 103
1077 : $string;
1078 }
1079  
1080  
1081 117 1;
1082  
1083 __END__
« lib/Template/Plugins.pm ↑ Summary lib/Template/Namespace/Constants.pm »