lib/Template/Service.pm

filter: Statementstmt 95.6% 109 / 114 Branchbran 68.3% 41 / 60 Conditioncond 72.0% 18 / 25 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 83.6% 189 / 226 CC 46 SCAR 27.9
CC
46
Cov
84%
process
24.3
_recover
16.5
_init
12.4
CRAP 16.3
« lib/Template/Context.pm ↑ Summary lib/Template/Plugins.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Service
4 #
5 # DESCRIPTION
6 # Module implementing a template processing service which wraps a
7 # template within PRE_PROCESS and POST_PROCESS templates and offers
8 # ERROR recovery.
9 #
10 # AUTHOR
11 # Andy Wardley <abw@wardley.org>
12 #
13 # COPYRIGHT
14 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 #============================================================================
20  
21 package Template::Service;
22  
23 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 116 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 116 ▶use Template::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 116 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 116 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 116 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 116 ▶use constant EXCEPTION => 'Template::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 116 our $VERSION = '3.106';
34 116 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Service::DEBUG 116 0 116
35 116 our $ERROR = '';
36  
37  
38 #========================================================================
39 # ----- PUBLIC METHODS -----
40 #========================================================================
41  
42 #------------------------------------------------------------------------
43 # process($template, \%params)
44 #
45 # Process a template within a service framework. A service may encompass
46 # PRE_PROCESS and POST_PROCESS templates and an ERROR hash which names
47 # templates to be substituted for the main template document in case of
48 # error. Each service invocation begins by resetting the state of the
49 # context object via a call to reset(). The AUTO_RESET option may be set
50 # to 0 (default: 1) to bypass this step.
51 #------------------------------------------------------------------------
52  
53 1406 ▶sub process {
Subroutine process: called CC=23 SCAR=31.9
Pod documented
54 1406 my ($self, $template, $params) = @_;
55 1406 my $context = $self->{ CONTEXT };
56 1406 my ($name, $output, $procout, $error);
57 1406 $output = '';
58  
59 $self->debug(
60 "process($template, ",
61 defined $params ? $params : '<no params>',
62 ')'
63 1406 ▶ ) if $self->{ DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1406 1406
defined $params ? : 0 0 0
64  
65 $context->reset()
66 1406 ▶ if $self->{ AUTO_RESET };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"AUTO_RESET"} 1383 23 1406
67  
68 # pre-request compiled template from context so that we can alias it
69 # in the stash for pre-processed templates to reference
70 1406 eval { $template = $context->template($template) };
71 1406 ▶ return $self->error($@)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 1403 1406
72 if $@;
73  
74 # localise the variable stash with any parameters passed
75 # and set the 'template' variable
76 1403 ▶ $params ||= { };
Condition100%2/2
$params ||= {}
l!l
1219184
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
77 # TODO: change this to C<||=> so we can use a template parameter
78 1403 ▶ $params->{ template } = $template
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "CODE" 1403 0 1403
79 unless ref $template eq 'CODE';
80 1403 $context->localise($params);
81  
82 SERVICE: {
83 # PRE_PROCESS
84 1403 eval {
85 1403 foreach $name (@{ $self->{ PRE_PROCESS } }) {
86 30 ▶ $self->debug("PRE_PROCESS: $name") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 30 30
87 30 $output .= $context->process($name);
88 }
89 };
90 1403 ▶ last SERVICE if ($error = $@);
Branch: 1 branch
BranchTrueFalseTotal
if $error = $@ 0 1403 1403
91  
92 # PROCESS
93 1403 eval {
94 1403 ▶ foreach $name (@{ $self->{ PROCESS } || [ $template ] }) {
Condition100%2/2
$self->{'PROCESS'} || [$template]
l!l
61397
MC/DC100%1/1
$self->{'PROCESS'} || [$template]
$self->{'PROCESS'}
Truth table100%2/2
A
A= $self->{'PROCESS'}
Aresult
11
00
95 1403 ▶ $self->debug("PROCESS: $name") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1403 1403
96 1403 $procout .= $context->process($name);
97 }
98 };
99 1403 ▶ if ($error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error = $@) 22 1381 1403
100 last SERVICE
101 22 ▶ unless defined ($procout = $self->_recover(\$error));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($procout = $self->_recover(\$error)) 9 13 22
102 }
103  
104 1394 ▶ if (defined $procout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $procout) 1393 1 1394
105 # WRAPPER
106 1393 eval {
107 1393 foreach $name (reverse @{ $self->{ WRAPPER } }) {
108 3 ▶ $self->debug("WRAPPER: $name") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 3 3
109 3 $procout = $context->process($name, { content => $procout });
110 }
111 };
112 1393 ▶ last SERVICE if ($error = $@);
Branch: 1 branch
BranchTrueFalseTotal
if $error = $@ 0 1393 1393
113 1393 $output .= $procout;
114 }
115  
116 # POST_PROCESS
117 1394 eval {
118 1394 foreach $name (@{ $self->{ POST_PROCESS } }) {
119 17 ▶ $self->debug("POST_PROCESS: $name") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 17 17
120 17 $output .= $context->process($name);
121 }
122 };
123 1394 ▶ last SERVICE if ($error = $@);
Branch: 1 branch
BranchTrueFalseTotal
if $error = $@ 0 1394 1394
124 }
125  
126 1403 $context->delocalise();
127 1403 delete $params->{ template };
128  
129 1403 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 9 1394 1403
130 # $error = $error->as_string if ref $error;
131 9 return $self->error($error);
132 }
133  
134 1394 return $output;
135 }
136  
137  
138 #------------------------------------------------------------------------
139 # context()
140 #
141 # Returns the internal CONTEXT reference.
142 #------------------------------------------------------------------------
143  
144 3 ▶sub context {
Subroutine context: called CC=1 SCAR=0.0
Pod documented
145 3 return $_[0]->{ CONTEXT };
146 }
147  
148  
149 #========================================================================
150 # -- PRIVATE METHODS --
151 #========================================================================
152  
153 253 ▶sub _init {
Subroutine _init: called CC=12 SCAR=25.2
154 253 my ($self, $config) = @_;
155 253 my ($item, $data, $context, $block, $blocks);
156 253 ▶ my $delim = $config->{ DELIMITER } // ':';
Condition50%1/2
$config->{"DELIMITER"} // ":"
l!l
0253
MC/DC0%0/1
$config->{"DELIMITER"} // ":"
$config->{"DELIMITER"}
Truth table50%1/2
A
A= $config->{"DELIMITER"}
Aresult
11
00
157  
158 # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary,
159 # by splitting on non-word characters
160 253 foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS WRAPPER )) {
161 1012 $data = $config->{ $item };
162 1012 ▶ $self->{ $item } = [ ], next unless (defined $data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 989 23 1012
163 23 ▶ $data = [ split($delim, $data || '') ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 19 4 23
Condition100%2/2
$data || ''
l!l
181
MC/DC100%1/1
$data || ''
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
164 unless ref $data eq 'ARRAY';
165 23 $self->{ $item } = $data;
166 }
167 # unset PROCESS option unless explicitly specified in config
168 $self->{ PROCESS } = undef
169 253 ▶ unless defined $config->{ PROCESS };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"PROCESS"} 248 5 253
170  
171 253 ▶ $self->{ ERROR } = $config->{ ERROR } || $config->{ ERRORS };
Condition66%2/3
$config->{'ERROR'} || $config->{'ERRORS'}
l!l&&r!l&&!r
50248
MC/DC50%1/2
$config->{'ERROR'} || $config->{'ERRORS'}
$config->{'ERROR'} $config->{'ERRORS'}
Truth table66%2/3
A || B
A= $config->{'ERROR'}B= $config->{'ERRORS'}
ABresult
1X1
011
000
172 253 ▶ $self->{ AUTO_RESET } = $config->{ AUTO_RESET } // 1;
Condition100%2/2
$config->{"AUTO_RESET"} // 1
l!l
5248
MC/DC100%1/1
$config->{"AUTO_RESET"} // 1
$config->{"AUTO_RESET"}
Truth table100%2/2
A
A= $config->{"AUTO_RESET"}
Aresult
11
00
173 253 ▶ $self->{ DEBUG } = ( $config->{ DEBUG } || 0 )
Condition100%2/2
$config->{'DEBUG'} || 0
l!l
7246
MC/DC100%1/1
$config->{'DEBUG'} || 0
$config->{'DEBUG'}
Truth table100%2/2
A
A= $config->{'DEBUG'}
Aresult
11
00
174 & Template::Constants::DEBUG_SERVICE;
175  
176 $context = $self->{ CONTEXT } = $config->{ CONTEXT }
177 253 ▶ || Template::Config->context($config)
Condition40%2/5
$config->{'CONTEXT'} || 'Template::Config'->context($config) || (return $self->error('Template::Config'->error))
l!l
2530
$config->{"CONTEXT"} or "Template::Config"->context($config)
l!l&&r!l&&!r
02530
MC/DC0%0/2
$config->{'CONTEXT'} || 'Template::Config'->context($config) || (return $self->error('Template::Config'->error))
$config->{"CONTEXT"} "Template::Config"->context($config)
Truth table33%1/3
A or B
A= $config->{"CONTEXT"}B= "Template::Config"->context($config)
ABresult
1X1
011
000
178 || return $self->error(Template::Config->error);
179  
180 253 return $self;
181 }
182  
183  
184 #------------------------------------------------------------------------
185 # _recover(\$exception)
186 #
187 # Examines the internal ERROR hash array to find a handler suitable
188 # for the exception object passed by reference. Selecting the handler
189 # is done by delegation to the exception's select_handler() method,
190 # passing the set of handler keys as arguments. A 'default' handler
191 # may also be provided. The handler value represents the name of a
192 # template which should be processed.
193 #------------------------------------------------------------------------
194  
195 22 ▶sub _recover {
Subroutine _recover: called CC=13 SCAR=28.0
196 22 my ($self, $error) = @_;
197 22 my $context = $self->{ CONTEXT };
198 22 my ($hkey, $handler, $output);
199  
200 # there shouldn't ever be a non-exception object received at this
201 # point... unless a module like CGI::Carp messes around with the
202 # DIE handler.
203 return undef
204 22 ▶ unless blessed($$error) && $$error->isa(EXCEPTION);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($$error) and $$error->isa("Template::Exception") 0 22 22
Condition33%1/3
builtin::blessed($$error) and $$error->isa("Template::Exception")
!ll&&!rl&&r
0022
MC/DC0%0/2
builtin::blessed($$error) and $$error->isa("Template::Exception")
builtin::blessed($$error) $$error->isa("Template::Exception")
Truth table33%1/3
A and B
A= builtin::blessed($$error)B= $$error->isa("Template::Exception")
ABresult
0X0
100
111
205  
206 # a 'stop' exception is thrown by [% STOP %] - we return the output
207 # buffer stored in the exception object
208 22 ▶ return $$error->text()
Branch: 1 branch
BranchTrueFalseTotal
if $$error->type eq "stop" 9 13 22
209 if $$error->type() eq 'stop';
210  
211 my $handlers = $self->{ ERROR }
212 13 ▶ || return undef; ## RETURN
Condition100%2/2
$self->{'ERROR'} || (return undef)
l!l
49
MC/DC100%1/1
$self->{'ERROR'} || (return undef)
$self->{'ERROR'}
Truth table100%2/2
A
A= $self->{'ERROR'}
Aresult
11
00
213  
214 4 ▶ if (ref $handlers eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handlers eq 'HASH') { } 3 1 4
215 3 ▶ if ($hkey = $$error->select_handler(keys %$handlers)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hkey = $$error->select_handler(keys %$handlers)) { } 1 2 3
elsif ($handler = $handlers->{'default'}) { } 2 0 2
216 1 $handler = $handlers->{ $hkey };
217 1 ▶ $self->debug("using error handler for $hkey") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1 1
218 }
219 elsif ($handler = $handlers->{ default }) {
220 # use default handler
221 2 ▶ $self->debug("using default error handler") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 2 2
222 }
223 else {
224 0 return undef; ## RETURN
225 }
226 }
227 else {
228 1 $handler = $handlers;
229 1 ▶ $self->debug("using default error handler") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1 1
230 }
231  
232 4 eval { $handler = $context->template($handler) };
233 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
234 0 $$error = $@;
235 0 return undef; ## RETURN
236 };
237  
238 4 $context->stash->set('error', $$error);
239 4 eval {
240 4 $output .= $context->process($handler);
241 };
242 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
243 0 $$error = $@;
244 0 return undef; ## RETURN
245 }
246  
247 4 return $output;
248 }
249  
250 116 1;
251  
252 __END__
« lib/Template/Context.pm ↑ Summary lib/Template/Plugins.pm »