blib/lib/DTL/Fast/Template.pm

filter: Statementstmt 97.0% 99 / 102 Branchbran 86.3% 19 / 22 Conditioncond 69.2% 9 / 13 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 4 totaltotal 88.8% 144 / 162 SCAR 29.0
CC
18
Cov
93%
render
14.2
new
5.0
BEGIN
1.0
CRAP 18.1
« blib/lib/DTL/Fast/Cache/Compressed.pm ↑ Summary blib/lib/DTL/Fast/Entity.pm »
1 package DTL::Fast::Template;
2 98 ▶use parent 'DTL::Fast::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 98 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 98 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 98 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 98 ▶use DTL::Fast qw(get_template);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 98 ▶use DTL::Fast::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 98 ▶use DTL::Fast::Tags;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 98 ▶use DTL::Fast::Filters;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 98 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 98 our $CURRENT_TEMPLATE; # global variable for linking modules
14 98 our $CURRENT_TEMPLATE_LINE; # global variable for source line number
15
16 #@Override
17 sub new
18 {
19 962 ▶ my( $proto, $template, %kwargs ) = @_;
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
20 962 ▶ $template //= '';
Condition50%1/2
$template //= ""
l!l
9620
MC/DC0%0/1
$template //= ""
$template
Truth table50%1/2
A
A= $template
Aresult
11
00
21
22 962 $kwargs{'raw_chunks'} = _get_raw_chunks($template);
23 962 ▶ $kwargs{'dirs'} //= []; # optional dirs to look up for includes or parents
Condition100%2/2
$kwargs{"dirs"} //= []
l!l
76886
MC/DC100%1/1
$kwargs{"dirs"} //= []
$kwargs{"dirs"}
Truth table100%2/2
A
A= $kwargs{"dirs"}
Aresult
11
00
24 962 ▶ $kwargs{'file_path'} //= 'inline';
Condition100%2/2
$kwargs{"file_path"} //= "inline"
l!l
66896
MC/DC100%1/1
$kwargs{"file_path"} //= "inline"
$kwargs{"file_path"}
Truth table100%2/2
A
A= $kwargs{"file_path"}
Aresult
11
00
25 962 $kwargs{'perl'} = $];
26 962 $kwargs{'blocks'} = {};
27 962 ▶ $kwargs{'modules'} //= {
Condition50%1/2
$kwargs{"modules"} //= {"DTL::Fast", $DTL::Fast::VERSION}
l!l
0962
MC/DC0%0/1
$kwargs{"modules"} //= {"DTL::Fast", $DTL::Fast::VERSION}
$kwargs{"modules"}
Truth table50%1/2
A
A= $kwargs{"modules"}
Aresult
11
00
28 'DTL::Fast' => $DTL::Fast::VERSION,
29 };
30
31 962 my $self = $proto->SUPER::new(%kwargs);
32
33 946 return $self;
34 }
35
36 #@Override self-linking
37 sub remember_template
38 {
39 962 ▶ my ($self) = @_;
Subroutine remember_template: called CC=1 SCAR=0.0
Pod undocumented
40
41 962 $self->{'_template'} = $self;
42 962 $self->{'_template_line'} = 1;
43 962 weaken $self->{'_template'};
44
45 962 return $self;
46 }
47
48 #@Override
49 sub parse_chunks
50 {
51 962 ▶ my( $self ) = @_;
Subroutine parse_chunks: called CC=1 SCAR=0.0
Pod undocumented
52
53 962 my( $current_template_backup, $current_template_line_backup ) = ($CURRENT_TEMPLATE, $CURRENT_TEMPLATE_LINE);
54 962 ($CURRENT_TEMPLATE, $CURRENT_TEMPLATE_LINE) = ($self, 1);
55
56 962 $self->SUPER::parse_chunks();
57
58 946 ($CURRENT_TEMPLATE, $CURRENT_TEMPLATE_LINE) = ($current_template_backup, $current_template_line_backup);
59 946 return $self;
60 }
61
62 98 my $reg = qr/(
63 \{\#.+?\#\}
64 |\{\%.+?\%\}
65 |\{\{.+?\}\}
66 )/xs;
67
68 sub _get_raw_chunks
69 {
70 962 ▶ my( $template ) = @_;
Subroutine _get_raw_chunks: called CC=1 SCAR=0.0
71
72 962 my $result = [split $reg, $template];
73
74 962 return $result;
75 }
76
77 #@Override
78 sub render
79 {
80 2336 ▶ my( $self, $context ) = @_;
Subroutine render: called CC=14 SCAR=26.6
Pod undocumented
81
82 2336 ▶ $context //= {};
Condition100%2/2
$context //= {}
l!l
231422
MC/DC100%1/1
$context //= {}
$context
Truth table100%2/2
A
A= $context
Aresult
11
00
83
84 2336 ▶ if( ref $context eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $context eq 'HASH') { } 38 2298 2336
elsif (defined $context and ref $context ne 'DTL::Fast::Context') { } 0 2298 2298
85 {
86 38 $context = DTL::Fast::Context->new($context);
87 }
88 ▶ elsif(
Condition33%1/3
defined $context and ref $context ne "DTL::Fast::Context"
!ll&&!rl&&r
022980
MC/DC0%0/2
defined $context and ref $context ne "DTL::Fast::Context"
defined $context ref $context ne "DTL::Fast::Context"
Truth table33%1/3
A and B
A= defined $contextB= ref $context ne "DTL::Fast::Context"
ABresult
0X0
100
111
89 defined $context
90 and ref $context ne 'DTL::Fast::Context'
91 )
92 {
93 0 die "Context must be a DTL::Fast::Context object or a HASH reference";
94 }
95
96 2336 $context->push_scope();
97
98 2336 ▶ $context->{'ns'}->[-1]->{'_dtl_ssi_dirs'} = $self->{'ssi_dirs'} if $self->{'ssi_dirs'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ssi_dirs"} 2 2334 2336
99 2336 ▶ $context->{'ns'}->[-1]->{'_dtl_url_source'} = $self->{'url_source'} if $self->{'url_source'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"url_source"} 4 2332 2336
100
101 2336 my $template_path = $self->{'file_path'};
102
103 2336 ▶ if( not exists $context->{'ns'}->[-1]->{'_dtl_include_path'} ) # entry point
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $context->{'ns'}[-1]{'_dtl_include_path'}) { } 2313 23 2336
104 {
105 2313 $context->{'ns'}->[-1]->{'_dtl_include_path'} = [];
106 2313 $context->{'ns'}->[-1]->{'_dtl_include_files'} = {};
107 }
108 else # check for recursion, shouldn't this be in the include tag?
109 {
110 23 ▶ if( exists $context->{'ns'}->[-1]->{'_dtl_include_files'}->{$template_path} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $context->{"ns"}[-1]{"_dtl_include_files"}{$template_path}) 3 20 23
111 {
112 # recursive inclusion
113 die sprintf("Recursive inclusion detected:\n%s\n"
114 3 , join( "\n includes ", @{$context->{'ns'}->[-1]->{'_dtl_include_path'}}, $template_path)
115 );
116 }
117 }
118
119 2333 $context->{'ns'}->[-1]->{'_dtl_include_files'}->{$template_path} = 1;
120 2333 push @{$context->{'ns'}->[-1]->{'_dtl_include_path'}}, $template_path;
121
122 2333 my $result;
123 2333 ▶ if ( $self->{'extends'} ) # has parent template
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'extends'}) { } 11 2322 2333
124 {
125 11 my @descendants = ();
126 11 my $current_descendant = $self;
127 11 my %inheritance = ();
128
129 11 ▶ while( $current_descendant->{'extends'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($current_descendant->{"extends"}) 18 9 27
130 {
131 18 push @descendants, $current_descendant;
132 18 $inheritance{$current_descendant->{'file_path'}} = 1;
133
134 18 my $parent_template_name = $current_descendant->{'extends'}->render($context);
135
136 die sprintf(
137 "Unable to resolve parent template name for %s"
138 18 ▶ , $current_descendant->{'file_path'}
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_template_name 0 18 18
139 ) if not $parent_template_name;
140
141 $current_descendant = get_template(
142 $parent_template_name
143 18 , 'dirs' => $self->{'dirs'}
144 );
145
146 17 ▶ if ( defined $current_descendant )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_descendant) { } 17 0 17
147 {
148 17 ▶ if ( $inheritance{$current_descendant->{'file_path'}} )
Branch: 1 branch
BranchTrueFalseTotal
if ($inheritance{$current_descendant->{"file_path"}}) 1 16 17
149 {
150 die sprintf(
151 "Recursive inheritance detected:\n%s\n"
152 , join(
153 "\n inherited from ",
154 4 (map {$_->{'file_path'}} @descendants),
155 1 $current_descendant->{'file_path'}
156 )
157 );
158 }
159 }
160 else
161 {
162 die sprintf( "Couldn't found a parent template: %s in one of the following directories: %s"
163 , $parent_template_name
164 0 , join( ', ', @{$self->{'dirs'}})
165 );
166 }
167 }
168
169 9 push @descendants, $current_descendant;
170 9 $context->{'ns'}->[-1]->{'_dtl_descendants'} = \@descendants;
171 9 $context->{'ns'}->[-1]->{'_dtl_rendering_template'} = $current_descendant;
172 9 $result = $current_descendant->SUPER::render($context);
173
174 }
175 else
176 {
177 2322 delete $context->{'ns'}->[-1]->{'_dtl_descendants'};
178 2322 $context->{'ns'}->[-1]->{'_dtl_rendering_template'} = $self;
179 2322 $result = $self->SUPER::render($context);
180 }
181
182 2312 pop @{$context->{'ns'}->[-1]->{'_dtl_include_path'}};
183 2312 delete $context->{'ns'}->[-1]->{'_dtl_include_files'}->{$template_path};
184
185 2312 $context->pop_scope();
186
187 2312 return $result;
188 }
189
190 98 1;
« blib/lib/DTL/Fast/Cache/Compressed.pm ↑ Summary blib/lib/DTL/Fast/Entity.pm »