blib/lib/Template/Service.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 25 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 217 CC 30 SCAR 51.0
CC
30
Cov
0%
process
306.0
_recover
132.0
_init
20.0
CRAP 164.7
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 use strict;
24 use warnings;
25 use base 'Template::Base';
26 use Template::Config;
27 use Template::Exception;
28 use Template::Constants;
29 use Scalar::Util 'blessed';
30  
31 use constant EXCEPTION => 'Template::Exception';
32  
33 our $VERSION = '3.105';
34 our $DEBUG = 0 unless defined $DEBUG;
35 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 my ($self, $template, $params) = @_;
55 my $context = $self->{ CONTEXT };
56 my ($name, $output, $procout, $error);
57 $output = '';
58  
59 $self->debug(
60 "process($template, ",
61 defined $params ? $params : '<no params>',
62 ')'
63 ) if $self->{ DEBUG };
64  
65 $context->reset()
66 if $self->{ AUTO_RESET };
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 eval { $template = $context->template($template) };
71 return $self->error($@)
72 if $@;
73  
74 # localise the variable stash with any parameters passed
75 # and set the 'template' variable
76 $params ||= { };
77 # TODO: change this to C<||=> so we can use a template parameter
78 $params->{ template } = $template
79 unless ref $template eq 'CODE';
80 $context->localise($params);
81  
82 SERVICE: {
83 # PRE_PROCESS
84 eval {
85 foreach $name (@{ $self->{ PRE_PROCESS } }) {
86 $self->debug("PRE_PROCESS: $name") if $self->{ DEBUG };
87 $output .= $context->process($name);
88 }
89 };
90 last SERVICE if ($error = $@);
91  
92 # PROCESS
93 eval {
94 foreach $name (@{ $self->{ PROCESS } || [ $template ] }) {
95 $self->debug("PROCESS: $name") if $self->{ DEBUG };
96 $procout .= $context->process($name);
97 }
98 };
99 if ($error = $@) {
100 last SERVICE
101 unless defined ($procout = $self->_recover(\$error));
102 }
103  
104 if (defined $procout) {
105 # WRAPPER
106 eval {
107 foreach $name (reverse @{ $self->{ WRAPPER } }) {
108 $self->debug("WRAPPER: $name") if $self->{ DEBUG };
109 $procout = $context->process($name, { content => $procout });
110 }
111 };
112 last SERVICE if ($error = $@);
113 $output .= $procout;
114 }
115  
116 # POST_PROCESS
117 eval {
118 foreach $name (@{ $self->{ POST_PROCESS } }) {
119 $self->debug("POST_PROCESS: $name") if $self->{ DEBUG };
120 $output .= $context->process($name);
121 }
122 };
123 last SERVICE if ($error = $@);
124 }
125  
126 $context->delocalise();
127 delete $params->{ template };
128  
129 if ($error) {
130 # $error = $error->as_string if ref $error;
131 return $self->error($error);
132 }
133  
134 return $output;
135 }
136  
137  
138 #------------------------------------------------------------------------
139 # context()
140 #
141 # Returns the internal CONTEXT reference.
142 #------------------------------------------------------------------------
143  
144 sub context {
145 return $_[0]->{ CONTEXT };
146 }
147  
148  
149 #========================================================================
150 # -- PRIVATE METHODS --
151 #========================================================================
152  
153 sub _init {
154 my ($self, $config) = @_;
155 my ($item, $data, $context, $block, $blocks);
156 my $delim = $config->{ DELIMITER } // ':';
157  
158 # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary,
159 # by splitting on non-word characters
160 foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS WRAPPER )) {
161 $data = $config->{ $item };
162 $self->{ $item } = [ ], next unless (defined $data);
163 $data = [ split($delim, $data || '') ]
164 unless ref $data eq 'ARRAY';
165 $self->{ $item } = $data;
166 }
167 # unset PROCESS option unless explicitly specified in config
168 $self->{ PROCESS } = undef
169 unless defined $config->{ PROCESS };
170  
171 $self->{ ERROR } = $config->{ ERROR } || $config->{ ERRORS };
172 $self->{ AUTO_RESET } = $config->{ AUTO_RESET } // 1;
173 $self->{ DEBUG } = ( $config->{ DEBUG } || 0 )
174 & Template::Constants::DEBUG_SERVICE;
175  
176 $context = $self->{ CONTEXT } = $config->{ CONTEXT }
177 || Template::Config->context($config)
178 || return $self->error(Template::Config->error);
179  
180 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 sub _recover {
196 my ($self, $error) = @_;
197 my $context = $self->{ CONTEXT };
198 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 unless blessed($$error) && $$error->isa(EXCEPTION);
205  
206 # a 'stop' exception is thrown by [% STOP %] - we return the output
207 # buffer stored in the exception object
208 return $$error->text()
209 if $$error->type() eq 'stop';
210  
211 my $handlers = $self->{ ERROR }
212 || return undef; ## RETURN
213  
214 if (ref $handlers eq 'HASH') {
215 if ($hkey = $$error->select_handler(keys %$handlers)) {
216 $handler = $handlers->{ $hkey };
217 $self->debug("using error handler for $hkey") if $self->{ DEBUG };
218 }
219 elsif ($handler = $handlers->{ default }) {
220 # use default handler
221 $self->debug("using default error handler") if $self->{ DEBUG };
222 }
223 else {
224 return undef; ## RETURN
225 }
226 }
227 else {
228 $handler = $handlers;
229 $self->debug("using default error handler") if $self->{ DEBUG };
230 }
231  
232 eval { $handler = $context->template($handler) };
233 if ($@) {
234 $$error = $@;
235 return undef; ## RETURN
236 };
237  
238 $context->stash->set('error', $$error);
239 eval {
240 $output .= $context->process($handler);
241 };
242 if ($@) {
243 $$error = $@;
244 return undef; ## RETURN
245 }
246  
247 return $output;
248 }
249  
250 1;
251  
252 __END__