lib/Template/Plugins.pm

filter: Statementstmt 97.6% 84 / 86 Branchbran 69.2% 36 / 52 Conditioncond 80.6% 25 / 31 MC/DCmcdc 55.5% 10 / 18 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 83.6% 164 / 196 CC 40 SCAR 27.5
CC
40
Cov
86%
_load
23.7
fetch
12.8
_init
9.0
CRAP 15.7
« lib/Template/Service.pm ↑ Summary lib/Template/Provider.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugins
4 #
5 # DESCRIPTION
6 # Plugin provider which handles the loading of plugin modules and
7 # instantiation of plugin objects.
8 #
9 # AUTHORS
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
14 # Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
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::Plugins;
22  
23 107 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 107 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 107 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 107 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 107 our $VERSION = '3.106';
29 107 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Plugins::DEBUG 107 0 107
30 107 our $PLUGIN_BASE = 'Template::Plugin';
31 107 our $STD_PLUGINS = {
32 'assert' => 'Template::Plugin::Assert',
33 'cgi' => 'Template::Plugin::CGI',
34 'datafile' => 'Template::Plugin::Datafile',
35 'date' => 'Template::Plugin::Date',
36 'debug' => 'Template::Plugin::Debug',
37 'directory' => 'Template::Plugin::Directory',
38 'dbi' => 'Template::Plugin::DBI',
39 'dumper' => 'Template::Plugin::Dumper',
40 'file' => 'Template::Plugin::File',
41 'format' => 'Template::Plugin::Format',
42 'html' => 'Template::Plugin::HTML',
43 'image' => 'Template::Plugin::Image',
44 'iterator' => 'Template::Plugin::Iterator',
45 'latex' => 'Template::Plugin::Latex',
46 'pod' => 'Template::Plugin::Pod',
47 'scalar' => 'Template::Plugin::Scalar',
48 'table' => 'Template::Plugin::Table',
49 'url' => 'Template::Plugin::URL',
50 'view' => 'Template::Plugin::View',
51 'wrap' => 'Template::Plugin::Wrap',
52 'xml' => 'Template::Plugin::XML',
53 'xmlstyle' => 'Template::Plugin::XML::Style',
54 };
55  
56  
57 #========================================================================
58 # -- PUBLIC METHODS --
59 #========================================================================
60  
61 #------------------------------------------------------------------------
62 # fetch($name, \@args, $context)
63 #
64 # General purpose method for requesting instantiation of a plugin
65 # object. The name of the plugin is passed as the first parameter.
66 # The internal FACTORY lookup table is consulted to retrieve the
67 # appropriate factory object or class name. If undefined, the _load()
68 # method is called to attempt to load the module and return a factory
69 # class/object which is then cached for subsequent use. A reference
70 # to the calling context should be passed as the third parameter.
71 # This is passed to the _load() class method. The new() method is
72 # then called against the factory class name or prototype object to
73 # instantiate a new plugin object, passing any arguments specified by
74 # list reference as the second parameter. e.g. where $factory is the
75 # class name 'MyClass', the new() method is called as a class method,
76 # $factory->new(...), equivalent to MyClass->new(...) . Where
77 # $factory is a prototype object, the new() method is called as an
78 # object method, $myobject->new(...). This latter approach allows
79 # plugins to act as Singletons, cache shared data, etc.
80 #
81 # Returns a reference to a plugin, (undef, STATUS_DECLINE) to decline
82 # the request or ($error, STATUS_ERROR) on error.
83 #------------------------------------------------------------------------
84  
85 259 ▶sub fetch {
Subroutine fetch: called CC=12 SCAR=25.5
Pod documented
86 259 my ($self, $name, $args, $context) = @_;
87 259 my ($factory, $plugin, $error);
88  
89 $self->debug("fetch($name, ",
90 defined $args ? ('[ ', join(', ', @$args), ' ]') : '<no args>', ', ',
91 defined $context ? $context : '<no context>',
92 259 ▶ ')') if $self->{ DEBUG };
Branch: 3 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 259 259
defined $args ? : 0 0 0
defined $context ? : 0 0 0
93  
94 # NOTE:
95 # the $context ref gets passed as the first parameter to all regular
96 # plugins, but not to those loaded via LOAD_PERL; to hack around
97 # this until we have a better implementation, we pass the $args
98 # reference to _load() and let it unshift the first args in the
99 # LOAD_PERL case
100  
101 259 ▶ $args ||= [ ];
Condition100%2/2
$args ||= []
l!l
17584
MC/DC100%1/1
$args ||= []
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
102 259 unshift @$args, $context;
103  
104 259 ▶ $factory = $self->{ FACTORY }->{ $name } ||= do {
Condition100%3/3
$self->{"FACTORY"}{$name} ||= do { ($factory, $error) = $self->_load($name, $context); return $factory, $error if $error; $factory }
l!l&&r!l&&!r
192652
MC/DC100%2/2
$self->{"FACTORY"}{$name} ||= do { ($factory, $error) = $self->_load($name, $context); return $factory, $error if $error; $factory }
$self->{"FACTORY"}{$name} do { ($factory, $error) = $self->_load($name, $context); return $factory, $error if $error; $factory }
Truth table100%3/3
A ||= B
A= $self->{"FACTORY"}{$name}B= do { ($factory, $error) = $self->_load($name, $context); return $factory, $error if $error; $factory }
ABresult
1X1
011
000
105 67 ($factory, $error) = $self->_load($name, $context);
106 67 ▶ return ($factory, $error) if $error; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $error 6 61 67
107 61 $factory;
108 };
109  
110 # call the new() method on the factory object or class name
111 253 eval {
112 253 ▶ if (ref $factory eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $factory eq 'CODE') { } 2 251 253
113 2 ▶ defined( $plugin = &$factory(@$args) )
Branch: 1 branch
BranchTrueFalseTotal
defined($plugin = &$factory(@$args)) or die "$name plugin failed\n" 2 0 2
114 || die "$name plugin failed\n";
115 }
116 else {
117 251 ▶ defined( $plugin = $factory->new(@$args) )
Branch: 1 branch
BranchTrueFalseTotal
defined($plugin = $factory->new(@$args)) or die "$name plugin failed: ", $factory->error, "\n" 246 2 248
118 || die "$name plugin failed: ", $factory->error(), "\n";
119 }
120 };
121 253 ▶ if ($error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error = $@) 5 248 253
122 return $self->{ TOLERANT }
123 5 ▶ ? (undef, Template::Constants::STATUS_DECLINED)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'TOLERANT'} ? : 0 5 5
124 : ($error, Template::Constants::STATUS_ERROR);
125 }
126  
127 248 return $plugin;
128 }
129  
130  
131  
132 #========================================================================
133 # -- PRIVATE METHODS --
134 #========================================================================
135  
136 #------------------------------------------------------------------------
137 # _init(\%config)
138 #
139 # Private initialisation method.
140 #------------------------------------------------------------------------
141  
142 268 ▶sub _init {
Subroutine _init: called CC=9 SCAR=22.0
143 268 my ($self, $params) = @_;
144 my ($pbase, $plugins, $factory) =
145 268 @$params{ qw( PLUGIN_BASE PLUGINS PLUGIN_FACTORY ) };
146  
147 268 ▶ $plugins ||= { };
Condition100%2/2
$plugins ||= {}
l!l
7261
MC/DC100%1/1
$plugins ||= {}
$plugins
Truth table100%2/2
A
A= $plugins
Aresult
11
00
148  
149 # update PLUGIN_BASE to an array ref if necessary
150 268 ▶ $pbase //= [ ];
Condition100%2/2
$pbase //= []
l!l
10258
MC/DC100%1/1
$pbase //= []
$pbase
Truth table100%2/2
A
A= $pbase
Aresult
11
00
151 268 ▶ $pbase = [ $pbase ] unless ref($pbase) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pbase eq "ARRAY" 6 262 268
152  
153 # add default plugin base (Template::Plugin) if set
154 268 ▶ push(@$pbase, $PLUGIN_BASE) if $PLUGIN_BASE;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Plugins::PLUGIN_BASE 265 3 268
155  
156 268 $self->{ PLUGIN_BASE } = $pbase;
157 268 $self->{ PLUGINS } = { %$STD_PLUGINS, %$plugins };
158 268 ▶ $self->{ TOLERANT } = $params->{ TOLERANT } || 0;
Condition50%1/2
$params->{'TOLERANT'} || 0
l!l
0268
MC/DC0%0/1
$params->{'TOLERANT'} || 0
$params->{'TOLERANT'}
Truth table50%1/2
A
A= $params->{'TOLERANT'}
Aresult
11
00
159 268 ▶ $self->{ LOAD_PERL } = $params->{ LOAD_PERL } || 0;
Condition100%2/2
$params->{'LOAD_PERL'} || 0
l!l
2266
MC/DC100%1/1
$params->{'LOAD_PERL'} || 0
$params->{'LOAD_PERL'}
Truth table100%2/2
A
A= $params->{'LOAD_PERL'}
Aresult
11
00
160 268 ▶ $self->{ FACTORY } = $factory || { };
Condition100%2/2
$factory || {}
l!l
1267
MC/DC100%1/1
$factory || {}
$factory
Truth table100%2/2
A
A= $factory
Aresult
11
00
161 268 ▶ $self->{ DEBUG } = ( $params->{ DEBUG } || 0 )
Condition100%2/2
$params->{'DEBUG'} || 0
l!l
8260
MC/DC100%1/1
$params->{'DEBUG'} || 0
$params->{'DEBUG'}
Truth table100%2/2
A
A= $params->{'DEBUG'}
Aresult
11
00
162 & Template::Constants::DEBUG_PLUGINS;
163  
164 268 return $self;
165 }
166  
167  
168  
169 #------------------------------------------------------------------------
170 # _load($name, $context)
171 #
172 # Private method which attempts to load a plugin module and determine the
173 # correct factory name or object by calling the load() class method in
174 # the loaded module.
175 #------------------------------------------------------------------------
176  
177 67 ▶sub _load {
Subroutine _load: called CC=21 SCAR=31.7
178 67 my ($self, $name, $context) = @_;
179 67 my ($factory, $module, $base, $pkg, $file, $ok, $error);
180  
181 67 ▶ if ($module = $self->{ PLUGINS }->{ $name } || $self->{ PLUGINS }->{ lc $name }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module = $self->{'PLUGINS'}{$name} || $self->{'PLUGINS'}{lc $name}) { } 46 21 67
Condition100%3/3
$self->{'PLUGINS'}{$name} || $self->{'PLUGINS'}{lc $name}
l!l&&r!l&&!r
331321
MC/DC100%2/2
$self->{'PLUGINS'}{$name} || $self->{'PLUGINS'}{lc $name}
$self->{'PLUGINS'}{$name} $self->{'PLUGINS'}{lc $name}
Truth table100%3/3
A || B
A= $self->{'PLUGINS'}{$name}B= $self->{'PLUGINS'}{lc $name}
ABresult
1X1
011
000
182 # plugin module name is explicitly stated in PLUGIN_NAME
183 46 $pkg = $module;
184 46 ($file = $module) =~ s|::|/|g;
185 $self->debug("loading $module.pm (PLUGIN_NAME)")
186 46 ▶ if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 46 46
187 46 ▶ $ok = eval { $module->isa($PLUGIN_BASE) or require "$file.pm" };
Condition66%2/3
$module->isa($Template::Plugins::PLUGIN_BASE) || require "$file.pm"
l!l&&r!l&&!r
17290
MC/DC0%0/2
$module->isa($Template::Plugins::PLUGIN_BASE) || require "$file.pm"
$module->isa($Template::Plugins::PLUGIN_BASE) require "$file.pm"
Truth table66%2/3
A || B
A= $module->isa($Template::Plugins::PLUGIN_BASE)B= require "$file.pm"
ABresult
1X1
011
000
188 46 $error = $@;
189 }
190 else {
191 # try each of the PLUGIN_BASE values to build module name
192 21 ($module = $name) =~ s/\./::/g;
193  
194 21 foreach $base (@{ $self->{ PLUGIN_BASE } }) {
195 27 $pkg = $base . '::' . $module;
196 27 ($file = $pkg) =~ s|::|/|g;
197  
198 $self->debug("loading $file.pm (PLUGIN_BASE)")
199 27 ▶ if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 27 27
200  
201 27 ▶ $ok = eval { $pkg->isa($PLUGIN_BASE) or require "$file.pm" };
Condition100%3/3
$pkg->isa($Template::Plugins::PLUGIN_BASE) || require "$file.pm"
l!l&&r!l&&!r
7182
MC/DC0%0/2
$pkg->isa($Template::Plugins::PLUGIN_BASE) || require "$file.pm"
$pkg->isa($Template::Plugins::PLUGIN_BASE) require "$file.pm"
Truth table66%2/3
A || B
A= $pkg->isa($Template::Plugins::PLUGIN_BASE)B= require "$file.pm"
ABresult
1X1
011
000
202 27 ▶ last unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 16 11 27
203  
204 11 ▶ $error .= "$@\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^Can't locate \Q$file\E\.pm/ 0 11 11
205 unless ($@ =~ /^Can't locate \Q$file\E\.pm/);
206 }
207 }
208  
209 67 ▶ if ($ok) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ok) { } 60 7 67
elsif ($self->{'LOAD_PERL'}) { } 1 6 7
210 60 ▶ $self->debug("calling $pkg->load()") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 60 60
211  
212 60 $factory = eval { $pkg->load($context) };
213 60 $error = '';
214 60 ▶ if ($@ || ! $factory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $factory) 0 60 60
Condition33%1/3
$@ or not $factory
l!l&&r!l&&!r
0060
MC/DC0%0/2
$@ or not $factory
$@ not $factory
Truth table33%1/3
A or not(B)
A= $@B= not $factory
ABresult
1X1
011
000
215 0 ▶ $error = $@ || 'load() returned a false value';
Condition0%0/2
$@ || 'load() returned a false value'
l!l
00
MC/DC0%0/1
$@ || 'load() returned a false value'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
216 }
217 }
218 elsif ($self->{ LOAD_PERL }) {
219 # fallback - is it a regular Perl module?
220 1 ($file = $module) =~ s|::|/|g;
221 1 eval { require "$file.pm" };
222 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
223 0 $error = $@;
224 }
225 else {
226 # this is a regular Perl module so the new() constructor
227 # isn't expecting a $context reference as the first argument;
228 # so we construct a closure which removes it before calling
229 # $module->new(@_);
230 2 ▶ $factory = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
231 2 shift;
232 2 $module->new(@_);
233 1 };
234 1 $error = '';
235 }
236 }
237  
238 67 ▶ if ($factory) {
Branch: 2 branches
BranchTrueFalseTotal
if ($factory) { } 61 6 67
elsif ($error) { } 2 4 6
239 61 ▶ $self->debug("$name => $factory") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 61 61
240 61 return $factory;
241 }
242 elsif ($error) {
243 return $self->{ TOLERANT }
244 2 ▶ ? (undef, Template::Constants::STATUS_DECLINED)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'TOLERANT'} ? : 0 2 2
245 : ($error, Template::Constants::STATUS_ERROR);
246 }
247 else {
248 4 return (undef, Template::Constants::STATUS_DECLINED);
249 }
250 }
251  
252 107 1;
253  
254 __END__
« lib/Template/Service.pm ↑ Summary lib/Template/Provider.pm »