Cake/View/TT.pm

filter: Statementstmt 74.7% 130 / 174 Branchbran 60.0% 30 / 50 Conditioncond 28.5% 8 / 28 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 10 totaltotal 61.9% 182 / 294 CC 37 SCAR 21.9
CC
37
Cov
67%
load
17.9
getVar
9.7
render
9.1
CRAP 9.0
« Cake/Utils.pm ↑ Summary Cake/Plugins/Simple/Cache.pm »
1 package Cake::View::TT;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #=======================================================================
7 # Global REGex
8 #=======================================================================
9 1 my $L = "[%";
10 1 my $T = "%]";
11 1 my $PRE = qr/\Q$L/;
12 1 my $POST = qr/\Q$T/;
13
14 1 my $CODE = qr{
15 $PRE
16 \s* # optional leading whitespace
17 CODE # required BLOCK token
18 \s* # optional whitespace
19 $POST
20 (.*?) # grab block content
21 $PRE
22 \s*
23 END
24 \s*
25 $POST
26 }xs;
27
28 1 my $VAR = qr {
29 $PRE
30 \s*
31 (.*?)
32 \s*
33 $POST
34 }xs;
35
36 1 my $REP = qr {
37 \{\{
38 \s
39 (\d+)
40 \s
41 \}\}
42 }x;
43
44 1 my $PROCESS = qr{
45 ($PRE
46 \s*
47 PROCESS
48 \s+
49 .*?
50 \s*
51 $POST)
52 }x;
53
54 1 my $PROCESS_URL = qr{
55 $PRE
56 \s*
57 PROCESS
58 \s+
59 (.*?)
60 \s*
61 $POST
62 }x;
63
64 1 my $MAIN = qr {
65 $PRE
66 \s*
67 main
68 \s*
69 $POST
70 }x;
71
72 1 my $INCLUDE = qr{
73 ($PRE
74 \s*
75 INCLUDE
76 \s+
77 .*?
78 \s*
79 $POST)
80 }x;
81
82 1 my $INCLUDE_URL = qr{
83 $PRE
84 \s*
85 INCLUDE
86 \s+
87 (.*?)
88 \s*
89 $POST
90 }x;
91
92 1 my $SETTINGS = qr{
93 ($PRE
94 \s* # optional leading whitespace
95 SETTINGS # required SETTINGS token
96 \s* # optional whitespace
97 $POST
98 .*? # grab block content
99 $PRE
100 \s*
101 END
102 \s*
103 $POST)
104 }xs;
105
106 1 my $SETTINGS_CONTENT = qr{
107 $PRE
108 \s* # optional leading whitespace
109 SETTINGS # required SETTINGS token
110 \s* # optional whitespace
111 $POST
112 (.*?) # grab block content
113 $PRE
114 \s*
115 END
116 \s*
117 $POST
118 }xs;
119
120 =head1 Name
121
122 Cake::View::TT
123
124 =cut
125
126 =head1 SYNOPSIS
127
128 use Cake::View::TT;
129
130 my $temp = Cake::View::TT->new({
131 path => '/some/global/path/to/template/folder',
132 layout => 'layout.tmpl'
133 });
134
135 $temp->render('file.tmpl',{
136 fname => 'Mamod',
137 lname => 'Mehyar',
138 options => ['opt1','opt2','opt3'],
139 nested => {
140 test => 'something',
141 another => []
142 }
143 });
144
145 =cut
146
147 1 my $DEBUG = 0;
148 3 ▶sub DEBUG {$DEBUG};
Subroutine DEBUG: called CC=1 SCAR=0.0
Pod undocumented
149
150 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
151
152 1 my $class = shift;
153 1 my $options = shift;
154 croak "You have to specify full path of your templat files location"
155 1 ▶ if !$options->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"path"} 0 1 1
156
157 1 $DEBUG = $options->{DEBUG};
158
159 my $self = bless({
160 path => $options->{path},
161 layout => $options->{layout},
162 1 },$class);
163
164 1 return $self;
165 }
166
167 1 ▶sub render {
Subroutine render: called CC=8 SCAR=22.0
Pod undocumented
168 1 my $self = shift;
169 1 my $file = shift;
170 1 my $data = shift;
171 1 $data->{me} = $self;
172
173 1 $self->{obj} = bless($data,'Cake::View::Object');
174 1 my @matches = $self->loadMatches($file);
175 1 my $temp = $self->{temp};
176
177 1 foreach my $match (@matches){
178 3 my $perl = '';
179 3 my $content = $match->{content};
180
181 $self->{current} = {
182 pos => $match->{start},
183 index => $match->{index},
184 file => $match->{file},
185 content => $match->{content}
186 3 };
187
188 3 ▶ if ($match->{from} eq 'VAR'){
Branch: 1 branch
BranchTrueFalseTotal
if ($match->{'from'} eq 'VAR') { } 3 0 3
189 3 $content =~ s/\n+//g;
190 3 my $return = $self->setVar("$content","eval");
191 3 ▶ if ($return){
Branch: 1 branch
BranchTrueFalseTotal
if ($return) 2 1 3
192 2 $self->pushData($return);
193 }
194 } else {
195 0 $content =~ s/print\s+(.+?)\s*;/$self->_varsFromPrint($1)/ges;
196 0 $content =~ s/$VAR/\$self->setVar("$1","eval")/g;
197 0 eval $content;
198 }
199
200 ##better error handling
201 3 ▶ if (DEBUG && $@){
Branch: 1 branch
BranchTrueFalseTotal
if (DEBUG() and $@) 0 3 3
Condition33%1/3
DEBUG() and $@
!ll&&!rl&&r
300
MC/DC0%0/2
DEBUG() and $@
DEBUG() $@
Truth table33%1/3
A and B
A= DEBUG()B= $@
ABresult
0X0
100
111
202 die {
203 content => $self->{current}->{content},
204 file => $self->{current}->{file},
205 pos => $self->{current}->{pos},
206 0 message => $@
207 }
208 }
209 }
210
211 1 ▶ while (my ($id) = $temp =~ m{$REP}){
Branch: 1 branch
BranchTrueFalseTotal
if (my($id) = $temp =~ /$REP/x) 3 1 4
212 3 my $replace;
213 3 ▶ if (my $data = $self->{data}->{$id}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->{'data'}{$id}) { } 2 1 3
214 2 $replace = join '', @{$data};
215 } else {
216 1 $replace = '';
217 }
218
219 3 my $length = $+[0] - $-[0];
220 3 substr ($temp,$-[0],$length,$replace);
221 }
222
223 1 return $temp;
224 0 $self->{temp} = $temp;
225 0 $temp =~ s/\t+//g;
226 0 return $self;
227
228 }
229
230 0 ▶sub _varsFromPrint {
Subroutine _varsFromPrint: not called CC=2 SCAR=17.9
231 0 my $self = shift;
232 0 my $data = shift;
233 0 $data =~ s/\n\s*//g;
234 0 $data =~ s/(['"])\./$1,/g;
235
236 0 my @captures;
237 0 ▶ while ($data =~ s/((['"])(?:(?:.*?)*)$VAR(?:(?:.*?)*)\2)/\{\{POS\}\}/){
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/((['"])(?:(?:.*?)*)$VAR(?:(?:.*?)*)\2)/{{POS}}/) 0 0 0
238 0 my $capture = $1;
239 0 my $ter = $2;
240 0 $capture =~ s/$PRE/$ter,\$self->setVar($ter/g;
241 0 $capture =~ s/$POST/$ter,"eval"),$ter/g;
242 0 push @captures, $capture;
243 };
244
245 map {
246 0 $data =~ s/\{\{POS\}\}/$_/;
247 } @captures;
248
249 0 return '$self->pushData('.($data).');';
250 }
251
252
253 1 ▶sub loadMatches {
Subroutine loadMatches: called CC=3 SCAR=12.3
Pod undocumented
254 1 my $self = shift;
255 1 my $file = shift;
256 1 my $temp = $self->load($file);
257 1 ▶ if (my $layout = $self->{layout}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $layout = $self->{"layout"}) 0 1 1
258 0 $layout = $self->load($layout);
259 0 $layout =~ s/\{\{ main \}\}/$temp/g;
260 0 $temp = $layout;
261 }
262
263 1 $self->{temp} = $temp;
264 1 ▶ return ref $self->{matches} eq 'ARRAY' ? @{$self->{matches}} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'matches'} eq 'ARRAY' ? : 1 0 1
265 }
266
267 1 ▶sub load {
Subroutine load: called CC=12 SCAR=28.9
Pod undocumented
268 1 my ($self,$file) = @_;
269 1 my ($data);
270 1 $file = $self->{path}."/$file";
271
272 1 ▶ if (open(my $fh,'<',$file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $file) { } 1 0 1
273 1 $data = do { local $/; <$fh> };
274 1 close($fh);
275 1 $data = Encode::decode_utf8($data);
276
277 1 my @files;
278 1 $data =~ s/$MAIN/{{ main }}/g;
279 1 my $counter = 0;
280 1 ▶ while (my ($settings,$include,$process,$code,$var) = $data =~ m{$SETTINGS|$INCLUDE|$PROCESS|$CODE|$VAR}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = $data =~ /$SETTINGS|$INCLUDE|$PROCESS|$CODE|$VAR/) 4 1 5
281 4 $counter++;
282 4 ▶ if ($counter > 1000){
Branch: 1 branch
BranchTrueFalseTotal
if ($counter > 1000) 0 4 4
283 0 die "recrusive loop $process";
284 }
285
286 4 my $length = $+[0] - $-[0];
287 4 my $start = $-[0];
288 4 ▶ if ($code || $var){
Branch: 4 branches
BranchTrueFalseTotal
if ($code or $var) { } 3 1 4
elsif ($process) { } 0 1 1
elsif ($settings) { } 1 0 1
elsif ($include) { } 0 0 0
Condition66%2/3
$code or $var
l!l&&r!l&&!r
031
MC/DC50%1/2
$code or $var
$code $var
Truth table66%2/3
A or B
A= $codeB= $var
ABresult
1X1
011
000
289 3 ▶ my $content = $code || $var;
Condition33%1/3
$code || $var
l!l&&r!l&&!r
030
MC/DC0%0/2
$code || $var
$code $var
Truth table33%1/3
A || B
A= $codeB= $var
ABresult
1X1
011
000
290 3 ▶ push @{$self->{matches}},{
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 0 3 3
291 index => ++$self->{i},
292 3 content => $content,
293 start => $start,
294 from => $code ? 'CODE' : 'VAR',
295 file => $file
296 };
297 3 substr ($data,$start,$length,"{{ $self->{i} }}");
298 } elsif ($process){
299 0 my ($url) = $process =~ m/$PROCESS_URL/g;
300 0 substr ($data,$start,$length,$self->load($url));
301 } elsif($settings){
302 1 my ($content) = $settings =~ m/$SETTINGS_CONTENT/g;
303 1 substr ($data,$start,$length,$self->settings($content));
304 } elsif ($include){
305 0 my ($url) = $include =~ m/$INCLUDE_URL/g;
306 0 push @files,$url;
307 0 substr ($data,$start,$length,'{{% INC %}}');
308 }
309 }
310
311 1 foreach my $f (@files){
312 0 $data =~ s{\{\{% INC %\}\}}{ { $self->load($f) }}e;
313 }
314
315 1 return $data;
316 }
317
318 else {
319 0 croak "Can't open file $file: $!";
320 }
321 }
322
323 1 ▶sub settings {
Subroutine settings: called CC=4 SCAR=14.8
Pod undocumented
324 1 my $self = shift;
325 1 my $settings = shift;
326 1 my @settings = split "\n",$settings;
327 map {
328 1 my ($m,$m2) = $_ =~ m/\s*(.*?)\s*:\s*(.*)\s*/g;
329 2 ▶ if ($m){
Branch: 1 branch
BranchTrueFalseTotal
if ($m) 1 1 2
330 1 ▶ if ($m eq 'layout'){
Branch: 1 branch
BranchTrueFalseTotal
if ($m eq 'layout') { } 1 0 1
331 1 $self->{layout} = $m2;
332 }else {
333 0 ▶ $self->{obj}->{$m} = $self->setVar($m2,'eval') || $m2;
Condition0%0/3
$self->setVar($m2, 'eval') || $m2
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->setVar($m2, 'eval') || $m2
$self->setVar($m2, 'eval') $m2
Truth table0%0/3
A || B
A= $self->setVar($m2, 'eval')B= $m2
ABresult
1X1
011
000
334 }
335 }
336 } @settings;
337 1 return '';
338 }
339
340 2 ▶sub pushData {
Subroutine pushData: called CC=1 SCAR=0.0
Pod undocumented
341 2 my $self = shift;
342 2 my $cur = $self->{cur};
343 2 push @{ $self->{data}->{$self->{current}->{index}} },@_;
344 }
345
346 3 ▶sub setVar {
Subroutine setVar: called CC=5 SCAR=20.9
Pod undocumented
347 3 my $self = shift;
348 3 my $content = shift;
349 3 my $eval = shift;
350 3 my $local = shift;
351
352 3 my $perl = '';
353 3 my ($var,$val) = $content =~ m/\s*((?:\.*\w+(?:\(.*?\))*)+)(?:\s*=\s*(.*))*/;
354
355 3 ▶ if ($val){
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 0 3 3
356 ##set value
357 0 my $var = $self->getVar($var);
358 0 my $val = $self->getVar($val);
359 0 $perl = "$var = $val;";
360 0 ▶ eval $perl and return undef if $eval;
Branch: 1 branch
BranchTrueFalseTotal
if $eval 0 0 0
Condition0%0/5
$eval && (eval $perl && (return undef))
!ll&&!rl&&r
000
eval $perl and return undef
!ll
00
MC/DC0%0/2
$eval && (eval $perl && (return undef))
$eval eval $perl
Truth table0%0/3
A && B
A= $evalB= eval $perl
ABresult
0X0
100
111
361
362 } else {
363 3 $content =~ s{(.*)}{$self->getVar($1)}e;
364 3 $perl = $content;
365 }
366
367 3 ▶ $eval ? return eval $perl :
Branch: 1 branch
BranchTrueFalseTotal
$eval ? : 3 0 3
368 return $perl;
369 }
370
371 3 ▶sub getVar {
Subroutine getVar: called CC=8 SCAR=22.8
Pod undocumented
372
373 3 my $self = shift;
374 3 my $var = shift;
375
376 3 ▶ if ($var =~ /^\$/ || $var =~ /^\s*['"]/){
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^\$/ or $var =~ /^\s*['"]/) 0 3 3
Condition33%1/3
$var =~ /^\$/ or $var =~ /^\s*['"]/
l!l&&r!l&&!r
003
MC/DC0%0/2
$var =~ /^\$/ or $var =~ /^\s*['"]/
$var =~ /^\$/ $var =~ /^\s*['"]/
Truth table33%1/3
A or B
A= $var =~ /^\$/B= $var =~ /^\s*['"]/
ABresult
1X1
011
000
377 0 return $var;
378 }
379
380 3 my @sp;
381 my $newsp;
382 3 ▶ while ($var =~ s/(?: (?: (\w+(?:\(.*?\))*) | \.(\(.*?\)) ) )//x){
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ s/(?: (?: (\w+(?:\(.*?\))*) | \.(\(.*?\)) ) )//x) 6 3 9
383 6 ▶ push @sp,$1 || $2;
Condition33%1/3
$1 || $2
l!l&&r!l&&!r
600
MC/DC0%0/2
$1 || $2
$1 $2
Truth table33%1/3
A || B
A= $1B= $2
ABresult
1X1
011
000
384 6 ▶ my $match = $1 || $2;
Condition33%1/3
$1 || $2
l!l&&r!l&&!r
600
MC/DC0%0/2
$1 || $2
$1 $2
Truth table33%1/3
A || B
A= $1B= $2
ABresult
1X1
011
000
385
386 6 ▶ if ($match =~ m/\(.*?\)$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($match =~ /\(.*?\)$/) { } 3 3 6
387 3 $newsp .= '->'.$match;
388 } else {
389 3 $newsp .= '->{'.$match.'}';
390 }
391 }
392
393 3 my $newvar = '$self->{obj}'.$newsp;
394 3 ▶ return $newvar || '';
Condition50%1/2
$newvar || ''
l!l
30
MC/DC0%0/1
$newvar || ''
$newvar
Truth table50%1/2
A
A= $newvar
Aresult
11
00
395 }
396
397 0 ▶sub toHTML {
Subroutine toHTML: not called CC=1 SCAR=6.9
Pod undocumented
398 0 my $self = shift;
399 0 return join '', @{$self->{data}};
400 }
401
402 package Cake::View::Object;
403
404 1 1;
405
406 __END__
« Cake/Utils.pm ↑ Summary Cake/Plugins/Simple/Cache.pm »