| 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.100'; | ||
| 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 | $delim = ':' unless defined $delim; | ||
| 158 | |||
| 159 | # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary, | ||
| 160 | # by splitting on non-word characters | ||
| 161 | foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS WRAPPER )) { | ||
| 162 | $data = $config->{ $item }; | ||
| 163 | $self->{ $item } = [ ], next unless (defined $data); | ||
| 164 | $data = [ split($delim, $data || '') ] | ||
| 165 | unless ref $data eq 'ARRAY'; | ||
| 166 | $self->{ $item } = $data; | ||
| 167 | } | ||
| 168 | # unset PROCESS option unless explicitly specified in config | ||
| 169 | $self->{ PROCESS } = undef | ||
| 170 | unless defined $config->{ PROCESS }; | ||
| 171 | |||
| 172 | $self->{ ERROR } = $config->{ ERROR } || $config->{ ERRORS }; | ||
| 173 | $self->{ AUTO_RESET } = defined $config->{ AUTO_RESET } | ||
| 174 | ? $config->{ AUTO_RESET } : 1; | ||
| 175 | $self->{ DEBUG } = ( $config->{ DEBUG } || 0 ) | ||
| 176 | & Template::Constants::DEBUG_SERVICE; | ||
| 177 | |||
| 178 | $context = $self->{ CONTEXT } = $config->{ CONTEXT } | ||
| 179 | || Template::Config->context($config) | ||
| 180 | || return $self->error(Template::Config->error); | ||
| 181 | |||
| 182 | 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 | my ($self, $error) = @_; | ||
| 199 | my $context = $self->{ CONTEXT }; | ||
| 200 | 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 | unless blessed($$error) && $$error->isa(EXCEPTION); | ||
| 207 | |||
| 208 | # a 'stop' exception is thrown by [% STOP %] - we return the output | ||
| 209 | # buffer stored in the exception object | ||
| 210 | return $$error->text() | ||
| 211 | if $$error->type() eq 'stop'; | ||
| 212 | |||
| 213 | my $handlers = $self->{ ERROR } | ||
| 214 | || return undef; ## RETURN | ||
| 215 | |||
| 216 | if (ref $handlers eq 'HASH') { | ||
| 217 | if ($hkey = $$error->select_handler(keys %$handlers)) { | ||
| 218 | $handler = $handlers->{ $hkey }; | ||
| 219 | $self->debug("using error handler for $hkey") if $self->{ DEBUG }; | ||
| 220 | } | ||
| 221 | elsif ($handler = $handlers->{ default }) { | ||
| 222 | # use default handler | ||
| 223 | $self->debug("using default error handler") if $self->{ DEBUG }; | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | return undef; ## RETURN | ||
| 227 | } | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | $handler = $handlers; | ||
| 231 | $self->debug("using default error handler") if $self->{ DEBUG }; | ||
| 232 | } | ||
| 233 | |||
| 234 | eval { $handler = $context->template($handler) }; | ||
| 235 | if ($@) { | ||
| 236 | $$error = $@; | ||
| 237 | return undef; ## RETURN | ||
| 238 | }; | ||
| 239 | |||
| 240 | $context->stash->set('error', $$error); | ||
| 241 | eval { | ||
| 242 | $output .= $context->process($handler); | ||
| 243 | }; | ||
| 244 | if ($@) { | ||
| 245 | $$error = $@; | ||
| 246 | return undef; ## RETURN | ||
| 247 | } | ||
| 248 | |||
| 249 | return $output; | ||
| 250 | } | ||
| 251 | |||
| 252 | 1; | ||
| 253 | |||
| 254 | __END__ |