lib/Template/Service.pm

filter: Statementstmt 95.6% 110 / 115 Branchbran 69.7% 46 / 66 Conditioncond 68.4% 13 / 19 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 83.9% 188 / 224 CC 46 SCAR 27.9
CC
46
Cov
84%
process
24.3
_recover
16.5
_init
12.4
CRAP 16.3
« blib/lib/Template/VMethods.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 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 113 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 113 ▶use Template::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 113 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 113 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 113 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 113 ▶use constant EXCEPTION => 'Template::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 113 our $VERSION = '3.100';
34 113 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Service::DEBUG 113 0 113
35 113 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 sub process {
54 1346 ▶ my ($self, $template, $params) = @_;
Subroutine process: called CC=23 SCAR=31.9
Pod documented
55 1346 my $context = $self->{ CONTEXT };
56 1346 my ($name, $output, $procout, $error);
57 1346 $output = '';
58  
59 $self->debug(
60 "process($template, ",
61 defined $params ? $params : '<no params>',
62 ')'
63 1346 ▶ ) if $self->{ DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1346 1346
defined $params ? : 0 0 0
64  
65 $context->reset()
66 1346 ▶ if $self->{ AUTO_RESET };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"AUTO_RESET"} 1323 23 1346
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 1346 eval { $template = $context->template($template) };
71 1346 ▶ return $self->error($@)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 1343 1346
72 if $@;
73  
74 # localise the variable stash with any parameters passed
75 # and set the 'template' variable
76 1343 ▶ $params ||= { };
Condition100%2/2
$params ||= {}
l!l
1182161
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 1343 ▶ $params->{ template } = $template
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "CODE" 1343 0 1343
79 unless ref $template eq 'CODE';
80 1343 $context->localise($params);
81  
82 SERVICE: {
83 # PRE_PROCESS
84 1343 eval {
85 1343 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 1343 ▶ last SERVICE if ($error = $@);
Branch: 1 branch
BranchTrueFalseTotal
if $error = $@ 0 1343 1343
91  
92 # PROCESS
93 1343 eval {
94 1343 ▶ foreach $name (@{ $self->{ PROCESS } || [ $template ] }) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"PROCESS"} or [$template] 1337 6 1343
95 1343 ▶ $self->debug("PROCESS: $name") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1343 1343
96 1343 $procout .= $context->process($name);
97 }
98 };
99 1343 ▶ if ($error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error = $@) 20 1323 1343
100 last SERVICE
101 20 ▶ unless defined ($procout = $self->_recover(\$error));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($procout = $self->_recover(\$error)) 7 13 20
102 }
103  
104 1336 ▶ if (defined $procout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $procout) 1335 1 1336
105 # WRAPPER
106 1335 eval {
107 1335 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 1335 ▶ last SERVICE if ($error = $@);
Branch: 1 branch
BranchTrueFalseTotal
if $error = $@ 0 1335 1335
113 1335 $output .= $procout;
114 }
115  
116 # POST_PROCESS
117 1336 eval {
118 1336 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 1336 ▶ last SERVICE if ($error = $@);
Branch: 1 branch
BranchTrueFalseTotal
if $error = $@ 0 1336 1336
124 }
125  
126 1343 $context->delocalise();
127 1343 delete $params->{ template };
128  
129 1343 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 7 1336 1343
130 # $error = $error->as_string if ref $error;
131 7 return $self->error($error);
132 }
133  
134 1336 return $output;
135 }
136  
137  
138 #------------------------------------------------------------------------
139 # context()
140 #
141 # Returns the internal CONTEXT reference.
142 #------------------------------------------------------------------------
143  
144 sub context {
145 3 ▶ return $_[0]->{ CONTEXT };
Subroutine context: called CC=1 SCAR=0.0
Pod documented
146 }
147  
148  
149 #========================================================================
150 # -- PRIVATE METHODS --
151 #========================================================================
152  
153 sub _init {
154 234 ▶ my ($self, $config) = @_;
Subroutine _init: called CC=12 SCAR=25.1
155 234 my ($item, $data, $context, $block, $blocks);
156 234 my $delim = $config->{ DELIMITER };
157 234 ▶ $delim = ':' unless defined $delim;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delim 234 0 234
158  
159 # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary,
160 # by splitting on non-word characters
161 234 foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS WRAPPER )) {
162 936 $data = $config->{ $item };
163 936 ▶ $self->{ $item } = [ ], next unless (defined $data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 913 23 936
164 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
165 unless ref $data eq 'ARRAY';
166 23 $self->{ $item } = $data;
167 }
168 # unset PROCESS option unless explicitly specified in config
169 $self->{ PROCESS } = undef
170 234 ▶ unless defined $config->{ PROCESS };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"PROCESS"} 229 5 234
171  
172 234 ▶ $self->{ ERROR } = $config->{ ERROR } || $config->{ ERRORS };
Condition66%2/3
$config->{'ERROR'} || $config->{'ERRORS'}
l!l&&r!l&&!r
50229
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
173 $self->{ AUTO_RESET } = defined $config->{ AUTO_RESET }
174 234 ▶ ? $config->{ AUTO_RESET } : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'AUTO_RESET'} ? : 5 229 234
175 234 ▶ $self->{ DEBUG } = ( $config->{ DEBUG } || 0 )
Condition100%2/2
$config->{'DEBUG'} || 0
l!l
7227
MC/DC100%1/1
$config->{'DEBUG'} || 0
$config->{'DEBUG'}
Truth table100%2/2
A
A= $config->{'DEBUG'}
Aresult
11
00
176 & Template::Constants::DEBUG_SERVICE;
177  
178 $context = $self->{ CONTEXT } = $config->{ CONTEXT }
179 234 ▶ || Template::Config->context($config)
Condition40%2/5
$config->{'CONTEXT'} || 'Template::Config'->context($config) || (return $self->error('Template::Config'->error))
l!l
2340
$config->{"CONTEXT"} or "Template::Config"->context($config)
l!l&&r!l&&!r
02340
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
180 || return $self->error(Template::Config->error);
181  
182 234 return $self;
183 }
184  
185  
186 #------------------------------------------------------------------------
187 # _recover(\$exception)
188 #
189 # Examines the internal ERROR hash array to find a handler suitable
190 # for the exception object passed by reference. Selecting the handler
191 # is done by delegation to the exception's select_handler() method,
192 # passing the set of handler keys as arguments. A 'default' handler
193 # may also be provided. The handler value represents the name of a
194 # template which should be processed.
195 #------------------------------------------------------------------------
196  
197 sub _recover {
198 20 ▶ my ($self, $error) = @_;
Subroutine _recover: called CC=13 SCAR=28.0
199 20 my $context = $self->{ CONTEXT };
200 20 my ($hkey, $handler, $output);
201  
202 # there shouldn't ever be a non-exception object received at this
203 # point... unless a module like CGI::Carp messes around with the
204 # DIE handler.
205 return undef
206 20 ▶ unless blessed($$error) && $$error->isa(EXCEPTION);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($$error) and $$error->isa("Template::Exception") 0 20 20
Condition33%1/3
builtin::blessed($$error) and $$error->isa("Template::Exception")
!ll&&!rl&&r
0020
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
207  
208 # a 'stop' exception is thrown by [% STOP %] - we return the output
209 # buffer stored in the exception object
210 20 ▶ return $$error->text()
Branch: 1 branch
BranchTrueFalseTotal
if $$error->type eq "stop" 9 11 20
211 if $$error->type() eq 'stop';
212  
213 my $handlers = $self->{ ERROR }
214 11 ▶ || return undef; ## RETURN
Condition100%2/2
$self->{'ERROR'} || (return undef)
l!l
47
MC/DC100%1/1
$self->{'ERROR'} || (return undef)
$self->{'ERROR'}
Truth table100%2/2
A
A= $self->{'ERROR'}
Aresult
11
00
215  
216 4 ▶ if (ref $handlers eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handlers eq 'HASH') { } 3 1 4
217 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
218 1 $handler = $handlers->{ $hkey };
219 1 ▶ $self->debug("using error handler for $hkey") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1 1
220 }
221 elsif ($handler = $handlers->{ default }) {
222 # use default handler
223 2 ▶ $self->debug("using default error handler") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 2 2
224 }
225 else {
226 0 return undef; ## RETURN
227 }
228 }
229 else {
230 1 $handler = $handlers;
231 1 ▶ $self->debug("using default error handler") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1 1
232 }
233  
234 4 eval { $handler = $context->template($handler) };
235 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
236 0 $$error = $@;
237 0 return undef; ## RETURN
238 };
239  
240 4 $context->stash->set('error', $$error);
241 4 eval {
242 4 $output .= $context->process($handler);
243 };
244 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
245 0 $$error = $@;
246 0 return undef; ## RETURN
247 }
248  
249 4 return $output;
250 }
251  
252 113 1;
253  
254 __END__
« blib/lib/Template/VMethods.pm ↑ Summary lib/Template/Plugins.pm »