TextTemplate/FakeEngine.pm

filter: Statementstmt 95.1% 78 / 82 Branchbran 83.3% 30 / 36 Conditioncond 61.1% 11 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 94.7% 18 / 19 Podpod 100% 1 / 1 totaltotal 85.5% 142 / 166 CC 25 SCAR 23.6
CC
25
Cov
88%
process
21.3
_declare_arg_variables
5.1
_rebuild_safe
2.0
CRAP 10.6
↑ Summary TextTemplate.pm »
1 package Dancer2::Template::TextTemplate::FakeEngine;
2 # ABSTRACT: Fake Template::Toolkit-like, persistent engine around Text::Template.
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '1.003'; # VERSION
8  
9 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use MooX::Types::MooseLike::Base qw( InstanceOf Bool ArrayRef Int Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Text::Template 1.46;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Safe 2.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19  
20 5 has _cache => (
21 is => 'rwp',
22 isa => InstanceOf['CHI::Driver'],
23 lazy => 1,
24 builder => '_build_cache',
25 );
26  
27 2 ▶sub _build_cache {
Subroutine _build_cache: called CC=1 SCAR=0.0
28 2 my $self = shift;
29 2 return CHI->new(
30 driver => 'Memory', # THIS should be generalized
31 expires_on_backend => 1,
32 global => 1, # CHI::Driver::Memory-specific
33 );
34 }
35  
36  
37 5 has caching => (
38 is => 'rw',
39 isa => Bool,
40 default => 1,
41 );
42  
43  
44 5 has expires => (
45 is => 'rw',
46 isa => Int,
47 default => 3600,
48 );
49  
50  
51 has delimiters => (
52 is => 'rw',
53 isa => ArrayRef[Str],
54 4 ▶ default => sub { [ '{', '}' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 5 );
56  
57  
58 5 has cache_stringrefs => (
59 is => 'rw',
60 isa => Bool,
61 default => 1,
62 );
63  
64  
65 5 has prepend => (
66 is => 'rw',
67 isa => Str,
68 default => <<'END',
69 use strict;
70 use warnings FATAL => 'all';
71 END
72 );
73  
74 # Text::Template's HASH variables (as exclusively used by FakeEngine) are not
75 # installed in the template evaluation package in a "use strict"-compatible
76 # way, but we enforce "use strict" in PREPEND, so we need to declare ourselves
77 # these variables just after the PREPENDed code.
78 21 ▶sub _declare_arg_variables {
Subroutine _declare_arg_variables: called CC=5 SCAR=16.3
79 21 my $hash = shift;
80 21 my @decls;
81 21 ▶ while ( my ( $name, $value ) = each %$hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hash) 7 21 28
82 7 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 7 7
83 7 push @decls, do {
84 0 ▶ if ( ref $value eq 'ARRAY' ) { '@' }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 7 7
elsif (ref $value eq 'HASH') { } 6 1 7
85 6 elsif ( ref $value eq 'HASH' ) { '%' }
86 1 else { '$' }
87 }
88 . $name;
89 }
90 21 return join "\n" => map { "our $_;" } @decls;
91 }
92  
93  
94 5 has safe => (
95 is => 'rw',
96 isa => Bool,
97 default => 1,
98 );
99  
100 has safe_opcodes => (
101 is => 'rw',
102 isa => ArrayRef[Str],
103 4 ▶ default => sub { [qw[ :default require caller dofile ]] },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 0 ▶ trigger => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 my $self = shift;
106 0 $self->_safe->permit_only(@{ $_[0] });
107 },
108 5 );
109  
110 has safe_disposable => (
111 is => 'rw',
112 isa => Bool,
113 default => 0,
114 3 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
115 3 my $self = shift;
116 3 ▶ $self->_rebuild_safe if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 2 1 3
117 },
118 5 );
119  
120 5 has _safe => (
121 is => 'rw',
122 isa => InstanceOf['Safe'],
123 lazy => 1,
124 builder => '_build_safe',
125 );
126  
127 12 ▶sub _build_safe {
Subroutine _build_safe: called CC=1 SCAR=0.0
128 12 my $self = shift;
129 12 my $safe = Safe->new;
130 12 $safe->permit_only(@{ $self->safe_opcodes });
131 12 return $safe;
132 }
133  
134 9 ▶sub _rebuild_safe {
Subroutine _rebuild_safe: called CC=2 SCAR=7.1
135 9 my $self = shift;
136 9 ▶ $self->_safe($self->_build_safe) if $self->safe_disposable;
Branch: 1 branch
BranchTrueFalseTotal
if $self->safe_disposable 9 0 9
137 9 return;
138 }
139  
140  
141 21 ▶sub process {
Subroutine process: called CC=19 SCAR=30.6
Pod documented
142 21 my ( $self, $template, $tokens ) = @_;
143  
144 21 ▶ defined $template
Branch: 1 branch
BranchTrueFalseTotal
defined $template and not builtin::blessed($template) and ref $template || -f $template or croak("$template is not a regular file (name) or a string reference") 21 0 21
Condition44%4/9
defined $template and not builtin::blessed($template) and ref $template || -f $template
!ll&&!rl&&r
0021
defined $template and not builtin::blessed($template)
!ll&&!rl&&r
0021
ref $template or -f $template
l!l&&r!l&&!r
1650
MC/DC0%0/4
defined $template and not builtin::blessed($template) and ref $template || -f $template
defined $template not builtin::blessed($template) ref $template -f $template
Truth table40%2/5
A and not(B) and C or D
A= defined $templateB= not builtin::blessed($template)C= ref $templateD= -f $template
ABCDresult
0XXX0
10XX0
11000
111X1
11011
145 && !blessed($template)
146 && ( ref $template || -f $template )
147 or croak "$template is not a regular file (name) or a string reference";
148  
149 21 ▶ my $tt = $self->caching
Branch: 2 branches
BranchTrueFalseTotal
$self->caching ? : 5 16 21
ref $template ? : 2 3 5
150 ? $self->_cache->get( ref $template ? $$template : $template )
151 : undef;
152 21 ▶ unless ($tt) { # either we don't cache or the cached instance expired
Branch: 1 branch
BranchTrueFalseTotal
unless ($tt) 19 2 21
153 19 ▶ $tt = Text::Template->new(
Branch: 2 branches
BranchTrueFalseTotal
ref $template ? : 15 4 19
ref $template ? : 15 4 19
154 TYPE => ref $template ? 'STRING' : 'FILE',
155 SOURCE => ref $template ? $$template : $template,
156 DELIMITERS => $self->delimiters,
157 );
158 }
159  
160 21 ▶ my $computed = $tt->fill_in(
Branch: 2 branches
BranchTrueFalseTotal
my $computed = $tt->fill_in("HASH", $tokens, "PREPEND", $self->prepend . _declare_arg_variables($tokens), $self->safe ? ("SAFE", $self->_safe) : ()) or our $ERROR = $Text::Template::ERROR 20 1 21
$self->safe ? : 21 0 21
161 HASH => $tokens,
162 PREPEND => $self->prepend . _declare_arg_variables($tokens),
163 $self->safe ? ( SAFE => $self->_safe ) : (),
164 ) or our $ERROR = $Text::Template::ERROR;
165  
166 21 ▶ $self->_rebuild_safe if $self->safe && $self->safe_disposable;
Branch: 1 branch
BranchTrueFalseTotal
if $self->safe and $self->safe_disposable 7 14 21
Condition66%2/3
$self->safe and $self->safe_disposable
!ll&&!rl&&r
0147
MC/DC50%1/2
$self->safe and $self->safe_disposable
$self->safe $self->safe_disposable
Truth table66%2/3
A and B
A= $self->safeB= $self->safe_disposable
ABresult
0X0
100
111
167  
168 21 ▶ if ( defined $computed && $self->caching ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $computed and $self->caching) 5 16 21
Condition100%3/3
defined $computed and $self->caching
!ll&&!rl&&r
1155
MC/DC100%2/2
defined $computed and $self->caching
defined $computed $self->caching
Truth table100%3/3
A and B
A= defined $computedB= $self->caching
ABresult
0X0
100
111
169 5 ▶ if ( ref $template && $self->cache_stringrefs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template and $self->cache_stringrefs) { } 2 3 5
Condition66%2/3
ref $template and $self->cache_stringrefs
!ll&&!rl&&r
302
MC/DC50%1/2
ref $template and $self->cache_stringrefs
ref $template $self->cache_stringrefs
Truth table66%2/3
A and B
A= ref $templateB= $self->cache_stringrefs
ABresult
0X0
100
111
170 2 $self->_cache->set( $$template, $tt, 'never' );
171 }
172 else { # filenames
173 3 ▶ $self->_cache->set( $template, $tt,
Branch: 1 branch
BranchTrueFalseTotal
$self->expires > 0 ? : 0 3 3
174 $self->expires > 0
175 ? { expires_in => $self->expires }
176 : 'never' );
177 }
178 }
179  
180 21 return $computed;
181 }
182  
183 5 1;
184  
185 __END__
↑ Summary TextTemplate.pm »