ZofCMS/Template.pm

filter: Statementstmt 9.6% 15 / 156 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 20.0% 4 / 20 Podpod 7.1% 1 / 14 totaltotal 6.5% 20 / 307 CC 49 SCAR 46.9
CC
49
Cov
6%
prepare_defaults
306.0
assemble
132.0
_execute
42.0
CRAP 109.0
« ZofCMS/Plugin/Comments.pm ↑ Summary ZofCMS/Plugin/YouTube.pm »
1  
2 package App::ZofCMS::Template;
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '1.001010'; # VERSION
8  
9 2 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 require File::Spec;
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
16 0 my $class = shift;
17 0 my $config = shift;
18 0 my $self = bless {}, $class;
19 0 $self->config( $config );
20 0 $self->conf( $config->conf );
21 0 $self->query( $config->query );
22  
23 0 return $self;
24 }
25  
26 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my $self = shift;
28  
29 0 my $conf = $self->conf;
30 0 my $query = $self->query;
31  
32 my $template_file = File::Spec->catfile(
33 $conf->{templates},
34 $query->{dir},
35 $query->{page} . $conf->{zcms_template_extension},
36 0 );
37  
38 0 ▶ my $template = do $template_file
Branch: 1 branch
BranchTrueFalseTotal
my $template = do $template_file or croak("Failed to load template file [$template_file] ($!) ($@)") 0 0 0
39 or croak "Failed to load template file [$template_file] ($!) ($@)";
40  
41 0 return $self->template( $template );
42 }
43  
44 0 ▶sub prepare_defaults {
Subroutine prepare_defaults: not called CC=17 SCAR=57.2
Pod undocumented
45 0 my $self = shift;
46  
47 0 my $template = $self->template;
48 0 my $conf = $self->conf;
49 0 my $query = $self->query;
50  
51 0 ▶ my $dir_defaults = $conf->{dir_defaults}{ $query->{dir} } || {};
Condition0%0/2
$conf->{'dir_defaults'}{$query->{'dir'}} || {}
l!l
00
MC/DC0%0/1
$conf->{'dir_defaults'}{$query->{'dir'}} || {}
$conf->{'dir_defaults'}{$query->{'dir'}}
Truth table0%0/2
A
A= $conf->{'dir_defaults'}{$query->{'dir'}}
Aresult
11
00
52  
53 0 %$template = ( %$dir_defaults, %$template );
54 0 ▶ %$template = ( %{ $conf->{template_defaults} || {} }, %$template );
Condition0%0/2
$conf->{'template_defaults'} || {}
l!l
00
MC/DC0%0/1
$conf->{'template_defaults'} || {}
$conf->{'template_defaults'}
Truth table0%0/2
A
A= $conf->{'template_defaults'}
Aresult
11
00
55  
56 0 %{ $template->{conf} } = (
57 0 ▶ %{ $conf->{template_defaults}{conf} || {} },
Condition0%0/2
$conf->{'template_defaults'}{'conf'} || {}
l!l
00
MC/DC0%0/1
$conf->{'template_defaults'}{'conf'} || {}
$conf->{'template_defaults'}{'conf'}
Truth table0%0/2
A
A= $conf->{'template_defaults'}{'conf'}
Aresult
11
00
58 0 ▶ %{ $dir_defaults->{conf} || {} },
Condition0%0/2
$dir_defaults->{'conf'} || {}
l!l
00
MC/DC0%0/1
$dir_defaults->{'conf'} || {}
$dir_defaults->{'conf'}
Truth table0%0/2
A
A= $dir_defaults->{'conf'}
Aresult
11
00
59 0 ▶ %{ $template->{conf} || {} },
Condition0%0/2
$template->{'conf'} || {}
l!l
00
MC/DC0%0/1
$template->{'conf'} || {}
$template->{'conf'}
Truth table0%0/2
A
A= $template->{'conf'}
Aresult
11
00
60 );
61  
62 0 %{ $template->{t} } = (
63 0 ▶ %{ $conf->{template_defaults}{t} || {} },
Condition0%0/2
$conf->{'template_defaults'}{'t'} || {}
l!l
00
MC/DC0%0/1
$conf->{'template_defaults'}{'t'} || {}
$conf->{'template_defaults'}{'t'}
Truth table0%0/2
A
A= $conf->{'template_defaults'}{'t'}
Aresult
11
00
64 0 ▶ %{ $dir_defaults->{t} || {} },
Condition0%0/2
$dir_defaults->{'t'} || {}
l!l
00
MC/DC0%0/1
$dir_defaults->{'t'} || {}
$dir_defaults->{'t'}
Truth table0%0/2
A
A= $dir_defaults->{'t'}
Aresult
11
00
65 0 ▶ %{ $template->{t} || {} },
Condition0%0/2
$template->{'t'} || {}
l!l
00
MC/DC0%0/1
$template->{'t'} || {}
$template->{'t'}
Truth table0%0/2
A
A= $template->{'t'}
Aresult
11
00
66 );
67  
68 0 %{ $template->{d} } = (
69 0 ▶ %{ $conf->{template_defaults}{d} || {} },
Condition0%0/2
$conf->{'template_defaults'}{'d'} || {}
l!l
00
MC/DC0%0/1
$conf->{'template_defaults'}{'d'} || {}
$conf->{'template_defaults'}{'d'}
Truth table0%0/2
A
A= $conf->{'template_defaults'}{'d'}
Aresult
11
00
70 0 ▶ %{ $dir_defaults->{d} || {} },
Condition0%0/2
$dir_defaults->{'d'} || {}
l!l
00
MC/DC0%0/1
$dir_defaults->{'d'} || {}
$dir_defaults->{'d'}
Truth table0%0/2
A
A= $dir_defaults->{'d'}
Aresult
11
00
71 0 ▶ %{ $template->{d} || {} },
Condition0%0/2
$template->{'d'} || {}
l!l
00
MC/DC0%0/1
$template->{'d'} || {}
$template->{'d'}
Truth table0%0/2
A
A= $template->{'d'}
Aresult
11
00
72 );
73  
74 my @plug_keys = map /(\d+)/, grep /^plugins\d+$/,
75 keys %$template,
76 0 ▶ keys %{ $conf->{template_defaults} || {} },
Condition0%0/2
$conf->{'template_defaults'} || {}
l!l
00
MC/DC0%0/1
$conf->{'template_defaults'} || {}
$conf->{'template_defaults'}
Truth table0%0/2
A
A= $conf->{'template_defaults'}
Aresult
11
00
77 keys %$dir_defaults;
78  
79 0 my %unique_plug_keys;
80 0 @unique_plug_keys{ @plug_keys } = ();
81 0 @plug_keys = sort { $a <=> $b } keys %unique_plug_keys;
82  
83 0 unshift @plug_keys, ''; # add this for 'plugins' key that doesn't have a number
84 0 $self->unique_plug_keys( \@plug_keys );
85  
86 0 for ( @plug_keys ) {
87 $template->{ "plugins$_" } = $self->sort_plugins(
88 ( $conf->{template_defaults}{ "plugins$_" } || [] ),
89 ( $dir_defaults->{ "plugins$_" } || [] ),
90 0 ▶ ( $template->{ "plugins$_" } || [] ),
Condition0%0/6
$conf->{'template_defaults'}{"plugins$_"} || []
l!l
00
$$dir_defaults{"plugins$_"} || []
l!l
00
$$template{"plugins$_"} || []
l!l
00
MC/DC0%0/1
$conf->{'template_defaults'}{"plugins$_"} || []
$conf->{'template_defaults'}{"plugins$_"}
MC/DC0%0/1
$$dir_defaults{"plugins$_"} || []
$$dir_defaults{"plugins$_"}
MC/DC0%0/1
$$template{"plugins$_"} || []
$$template{"plugins$_"}
Truth table0%0/2
A
A= $conf->{'template_defaults'}{"plugins$_"}
Aresult
11
00
Truth table0%0/2
A
A= $$dir_defaults{"plugins$_"}
Aresult
11
00
Truth table0%0/2
A
A= $$template{"plugins$_"}
Aresult
11
00
91 );
92 }
93 }
94  
95 0 ▶sub assemble {
Subroutine assemble: not called CC=11 SCAR=48.8
Pod undocumented
96 0 my $self = shift;
97  
98 0 my $template = $self->template;
99 0 my $conf = $self->conf;
100 0 my $query = $self->query;
101  
102 my $html_template = HTML::Template->new(
103 filename => File::Spec->catfile(
104 $conf->{data_store},
105 $template->{conf}{base},
106 ),
107 0 ▶ die_on_bad_params => $template->{conf}{die_on_bad_params} || 0,
Condition0%0/2
$template->{'conf'}{'die_on_bad_params'} || 0
l!l
00
MC/DC0%0/1
$template->{'conf'}{'die_on_bad_params'} || 0
$template->{'conf'}{'die_on_bad_params'}
Truth table0%0/2
A
A= $template->{'conf'}{'die_on_bad_params'}
Aresult
11
00
108 );
109  
110 0 my $data_store = $conf->{data_store};
111  
112 0 $self->_exec_plugins;
113  
114 0 $html_template->param( %{ $template->{t} } );
115  
116 0 ▶ while ( my ($key, $value) = each %$template ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$template) 0 0 0
117 next
118 0 ▶ if $key eq 'conf'
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "conf" or $key eq "t" or $key eq "plugins" or $key eq "d" 0 0 0
Condition0%0/9
$key eq "conf" or $key eq "t" or $key eq "plugins" or $key eq "d"
l!l&&r!l&&!r
000
$key eq "conf" or $key eq "t" or $key eq "plugins"
l!l&&r!l&&!r
000
$key eq "conf" or $key eq "t"
l!l&&r!l&&!r
000
MC/DC0%0/4
$key eq "conf" or $key eq "t" or $key eq "plugins" or $key eq "d"
$key eq "conf" $key eq "t" $key eq "plugins" $key eq "d"
Truth table0%0/5
A or B or C or D
A= $key eq "conf"B= $key eq "t"C= $key eq "plugins"D= $key eq "d"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
119 or $key eq 't'
120 or $key eq 'plugins'
121 or $key eq 'd';
122  
123 0 ▶ if ( ref $value eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'SCALAR') { } 0 0 0
124 0 my $file = File::Spec->catfile( $data_store, $$value );
125 0 ▶ if ( substr( $file, -5) eq '.tmpl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($file, -5) eq '.tmpl') { } 0 0 0
126 my $sub_html_template
127 = HTML::Template->new(
128 filename => $file,
129 die_on_bad_params
130 0 ▶ => $template->{conf}{die_on_bad_params} || 0,
Condition0%0/2
$template->{'conf'}{'die_on_bad_params'} || 0
l!l
00
MC/DC0%0/1
$template->{'conf'}{'die_on_bad_params'} || 0
$template->{'conf'}{'die_on_bad_params'}
Truth table0%0/2
A
A= $template->{'conf'}{'die_on_bad_params'}
Aresult
11
00
131 );
132  
133 0 $sub_html_template->param( %{ $template->{t} } );
134  
135 0 $html_template->param( $key => $sub_html_template->output );
136 }
137 else {
138 0 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Failed to open file [$file] for reading ($!)") 0 0 0
139 or croak "Failed to open file [$file] for reading ($!)";
140  
141 0 $html_template->param( $key => do { local $/; <$fh>; } );
142 0 close $fh;
143 }
144 }
145 else {
146 0 $html_template->param( $key => $value );
147 }
148 }
149  
150 0 return $self->html_template( $html_template );
151 }
152  
153 0 ▶sub execute_before {
Subroutine execute_before: not called CC=1 SCAR=6.9
Pod undocumented
154 0 return shift->_execute('before');
155 }
156 0 ▶sub execute {
Subroutine execute: not called CC=1 SCAR=6.9
Pod undocumented
157 0 return shift->_execute;
158 }
159  
160 0 ▶sub _execute {
Subroutine _execute: not called CC=6 SCAR=37.4
161 0 my $self = shift;
162 0 my $is_before = shift;
163 0 my $template = $self->template;
164 0 my $conf = $self->conf;
165 0 my $query = $self->query;
166  
167 0 ▶ my $conf_key = $is_before ? 'exec_before' : 'exec';
Branch: 1 branch
BranchTrueFalseTotal
$is_before ? : 0 0 0
168  
169 0 ▶ if ( ref $template->{conf}{ $conf_key } eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $template->{'conf'}{$conf_key} eq 'CODE') { } 0 0 0
elsif (defined $template->{'conf'}{$conf_key} and length $template->{'conf'}{$conf_key}) { } 0 0 0
170 0 return $template->{conf}{ $conf_key }->(
171 $query,
172 $self->template,
173 $self->config,
174 $self->html_template,
175 );
176 }
177 ▶ elsif ( defined $template->{conf}{ $conf_key }
Condition0%0/3
defined $template->{"conf"}{$conf_key} and length $template->{"conf"}{$conf_key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $template->{"conf"}{$conf_key} and length $template->{"conf"}{$conf_key}
defined $template->{"conf"}{$conf_key} length $template->{"conf"}{$conf_key}
Truth table0%0/3
A and B
A= defined $template->{"conf"}{$conf_key}B= length $template->{"conf"}{$conf_key}
ABresult
0X0
100
111
178 and length $template->{conf}{ $conf_key }
179 ) {
180 0 my $package = 'App::ZofCMS::Execs::' . $template->{conf}{ $conf_key };
181 0 eval "use $package;";
182 0 ▶ $@ and croak "Failed to use() module specified by the template "
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak('Failed to use() module specified by the template ' . "[$package] Error: $@") 0 0 0
183 . "[$package] Error: $@";
184  
185 0 return $package->new->execute(
186 $query,
187 $self->template,
188 $self->config,
189 $self->html_template,
190 );
191 }
192  
193 0 return 1;
194 }
195  
196  
197 0 ▶sub _exec_plugins {
Subroutine _exec_plugins: not called CC=5 SCAR=34.0
198 0 my $self = shift;
199 0 my $template = $self->template;
200 0 my $query = $self->query;
201 0 my $config = $self->config;
202  
203 0 for my $plug_key ( @{ $self->unique_plug_keys } ) {
204 0 ▶ for ( @{ $template->{ "plugins$plug_key" } || [] } ) {
Condition0%0/2
$$template{"plugins$plug_key"} || []
l!l
00
MC/DC0%0/1
$$template{"plugins$plug_key"} || []
$$template{"plugins$plug_key"}
Truth table0%0/2
A
A= $$template{"plugins$plug_key"}
Aresult
11
00
205 0 my $plugin = "App::ZofCMS::Plugin::$_";
206 0 eval "use $plugin";
207 0 ▶ $@ and croak "Failed to use() plugin $plugin: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak("Failed to use() plugin ${plugin}: $@") 0 0 0
208 0 $plugin->new->process( $template, $query, $config );
209 }
210 }
211  
212 0 return;
213 }
214  
215 0 ▶sub sort_plugins {
Subroutine sort_plugins: not called CC=6 SCAR=37.4
Pod undocumented
216 0 my $self = shift;
217 0 my ( $conf_plugins, $dir_defaults_plugins, $template_plugins, ) = @_;
218  
219 0 for my $plugs (
220 $template_plugins , $dir_defaults_plugins, $conf_plugins
221 ) {
222  
223 0 for ( @$plugs ) {
224  
225 0 ▶ unless ( ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_) 0 0 0
226 0 $_ = { name => $_, priority => 10000, };
227 0 next;
228 }
229  
230 0 ▶ if (ref eq 'HASH' and 1 == keys %$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "HASH" and 1 == keys %$_) 0 0 0
Condition0%0/3
ref $_ eq "HASH" and 1 == keys %$_
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq "HASH" and 1 == keys %$_
ref $_ eq "HASH" 1 == keys %$_
Truth table0%0/3
A and B
A= ref $_ eq "HASH"B= 1 == keys %$_
ABresult
0X0
100
111
231 0 my ($name, $priority) = %$_;
232 0 @{ $_={} }{ qw/name priority/ } = ($name, $priority);
233 }
234 }
235 }
236  
237 my $r = [
238 map $_->{name},
239 sort {
240 0 $a->{priority}
241 <=>
242 $b->{priority}
243 0 }
244 $self->uniq_plugins(
245 @$template_plugins,
246 @$dir_defaults_plugins,
247 @$conf_plugins,
248 )
249 ];
250 0 return $r;
251 }
252  
253 0 ▶sub uniq_plugins {
Subroutine uniq_plugins: not called CC=2 SCAR=17.9
Pod undocumented
254 0 my $self = shift;
255 0 my %h;
256 0 ▶ return map { $h{ $_->{name} }++ == 0 ? $_ : () } @_;
Branch: 1 branch
BranchTrueFalseTotal
$h{$_->{'name'}}++ == 0 ? : 0 0 0
257 }
258  
259  
260 0 ▶sub html_template {
Subroutine html_template: not called CC=2 SCAR=17.9
Pod undocumented
261 0 my $self = shift;
262 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
263 0 $self->{ html_template } = shift;
264 }
265 0 return $self->{ html_template };
266 }
267  
268  
269 0 ▶sub template {
Subroutine template: not called CC=2 SCAR=17.9
Pod undocumented
270 0 my $self = shift;
271 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
272 0 $self->{ template } = shift;
273 }
274 0 return $self->{ template };
275 }
276  
277  
278 0 ▶sub query {
Subroutine query: not called CC=2 SCAR=17.9
Pod undocumented
279 0 my $self = shift;
280 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
281 0 $self->{ query } = shift;
282 }
283 0 return $self->{ query };
284 }
285  
286  
287 0 ▶sub config {
Subroutine config: not called CC=2 SCAR=17.9
Pod undocumented
288 0 my $self = shift;
289 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
290 0 $self->{ config } = shift;
291 }
292 0 return $self->{ config };
293 }
294  
295  
296 0 ▶sub conf {
Subroutine conf: not called CC=2 SCAR=17.9
Pod documented
297 0 my $self = shift;
298 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
299 0 $self->{ conf } = shift;
300 }
301 0 return $self->{ conf };
302 }
303  
304  
305 0 ▶sub unique_plug_keys {
Subroutine unique_plug_keys: not called CC=2 SCAR=17.9
Pod undocumented
306 0 my $self = shift;
307 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
308 0 $self->{UNIQUE_PLUG_KEYS} = shift;
309 }
310 0 return $self->{UNIQUE_PLUG_KEYS};
311 }
312  
313 2 1;
314 __END__
« ZofCMS/Plugin/Comments.pm ↑ Summary ZofCMS/Plugin/YouTube.pm »