| 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 | use strict; | ||
| 24 | use warnings; | ||
| 25 | use base 'Template::Base'; | ||
| 26 | use Template::Constants; | ||
| 27 | |||
| 28 | our $VERSION = '3.100'; | ||
| 29 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 30 | our $PLUGIN_BASE = 'Template::Plugin'; | ||
| 31 | 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 | sub fetch { | ||
| 86 | my ($self, $name, $args, $context) = @_; | ||
| 87 | my ($factory, $plugin, $error); | ||
| 88 | |||
| 89 | $self->debug("fetch($name, ", | ||
| 90 | defined $args ? ('[ ', join(', ', @$args), ' ]') : '<no args>', ', ', | ||
| 91 | defined $context ? $context : '<no context>', | ||
| 92 | ')') if $self->{ DEBUG }; | ||
| 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 | $args ||= [ ]; | ||
| 102 | unshift @$args, $context; | ||
| 103 | |||
| 104 | $factory = $self->{ FACTORY }->{ $name } ||= do { | ||
| 105 | ($factory, $error) = $self->_load($name, $context); | ||
| 106 | return ($factory, $error) if $error; ## RETURN | ||
| 107 | $factory; | ||
| 108 | }; | ||
| 109 | |||
| 110 | # call the new() method on the factory object or class name | ||
| 111 | eval { | ||
| 112 | if (ref $factory eq 'CODE') { | ||
| 113 | defined( $plugin = &$factory(@$args) ) | ||
| 114 | || die "$name plugin failed\n"; | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | defined( $plugin = $factory->new(@$args) ) | ||
| 118 | || die "$name plugin failed: ", $factory->error(), "\n"; | ||
| 119 | } | ||
| 120 | }; | ||
| 121 | if ($error = $@) { | ||
| 122 | return $self->{ TOLERANT } | ||
| 123 | ? (undef, Template::Constants::STATUS_DECLINED) | ||
| 124 | : ($error, Template::Constants::STATUS_ERROR); | ||
| 125 | } | ||
| 126 | |||
| 127 | return $plugin; | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | #======================================================================== | ||
| 133 | # -- PRIVATE METHODS -- | ||
| 134 | #======================================================================== | ||
| 135 | |||
| 136 | #------------------------------------------------------------------------ | ||
| 137 | # _init(\%config) | ||
| 138 | # | ||
| 139 | # Private initialisation method. | ||
| 140 | #------------------------------------------------------------------------ | ||
| 141 | |||
| 142 | sub _init { | ||
| 143 | my ($self, $params) = @_; | ||
| 144 | my ($pbase, $plugins, $factory) = | ||
| 145 | @$params{ qw( PLUGIN_BASE PLUGINS PLUGIN_FACTORY ) }; | ||
| 146 | |||
| 147 | $plugins ||= { }; | ||
| 148 | |||
| 149 | # update PLUGIN_BASE to an array ref if necessary | ||
| 150 | $pbase = [ ] unless defined $pbase; | ||
| 151 | $pbase = [ $pbase ] unless ref($pbase) eq 'ARRAY'; | ||
| 152 | |||
| 153 | # add default plugin base (Template::Plugin) if set | ||
| 154 | push(@$pbase, $PLUGIN_BASE) if $PLUGIN_BASE; | ||
| 155 | |||
| 156 | $self->{ PLUGIN_BASE } = $pbase; | ||
| 157 | $self->{ PLUGINS } = { %$STD_PLUGINS, %$plugins }; | ||
| 158 | $self->{ TOLERANT } = $params->{ TOLERANT } || 0; | ||
| 159 | $self->{ LOAD_PERL } = $params->{ LOAD_PERL } || 0; | ||
| 160 | $self->{ FACTORY } = $factory || { }; | ||
| 161 | $self->{ DEBUG } = ( $params->{ DEBUG } || 0 ) | ||
| 162 | & Template::Constants::DEBUG_PLUGINS; | ||
| 163 | |||
| 164 | 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 | sub _load { | ||
| 178 | my ($self, $name, $context) = @_; | ||
| 179 | my ($factory, $module, $base, $pkg, $file, $ok, $error); | ||
| 180 | |||
| 181 | if ($module = $self->{ PLUGINS }->{ $name } || $self->{ PLUGINS }->{ lc $name }) { | ||
| 182 | # plugin module name is explicitly stated in PLUGIN_NAME | ||
| 183 | $pkg = $module; | ||
| 184 | ($file = $module) =~ s|::|/|g; | ||
| 185 | $self->debug("loading $module.pm (PLUGIN_NAME)") | ||
| 186 | if $self->{ DEBUG }; | ||
| 187 | $ok = eval { $module->isa($PLUGIN_BASE) or require "$file.pm" }; | ||
| 188 | $error = $@; | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | # try each of the PLUGIN_BASE values to build module name | ||
| 192 | ($module = $name) =~ s/\./::/g; | ||
| 193 | |||
| 194 | foreach $base (@{ $self->{ PLUGIN_BASE } }) { | ||
| 195 | $pkg = $base . '::' . $module; | ||
| 196 | ($file = $pkg) =~ s|::|/|g; | ||
| 197 | |||
| 198 | $self->debug("loading $file.pm (PLUGIN_BASE)") | ||
| 199 | if $self->{ DEBUG }; | ||
| 200 | |||
| 201 | $ok = eval { $pkg->isa($PLUGIN_BASE) or require "$file.pm" }; | ||
| 202 | last unless $@; | ||
| 203 | |||
| 204 | $error .= "$@\n" | ||
| 205 | unless ($@ =~ /^Can't locate \Q$file\E\.pm/); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | if ($ok) { | ||
| 210 | $self->debug("calling $pkg->load()") if $self->{ DEBUG }; | ||
| 211 | |||
| 212 | $factory = eval { $pkg->load($context) }; | ||
| 213 | $error = ''; | ||
| 214 | if ($@ || ! $factory) { | ||
| 215 | $error = $@ || 'load() returned a false value'; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | elsif ($self->{ LOAD_PERL }) { | ||
| 219 | # fallback - is it a regular Perl module? | ||
| 220 | ($file = $module) =~ s|::|/|g; | ||
| 221 | eval { require "$file.pm" }; | ||
| 222 | if ($@) { | ||
| 223 | $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 | $factory = sub { | ||
| 231 | shift; | ||
| 232 | $module->new(@_); | ||
| 233 | }; | ||
| 234 | $error = ''; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | if ($factory) { | ||
| 239 | $self->debug("$name => $factory") if $self->{ DEBUG }; | ||
| 240 | return $factory; | ||
| 241 | } | ||
| 242 | elsif ($error) { | ||
| 243 | return $self->{ TOLERANT } | ||
| 244 | ? (undef, Template::Constants::STATUS_DECLINED) | ||
| 245 | : ($error, Template::Constants::STATUS_ERROR); | ||
| 246 | } | ||
| 247 | else { | ||
| 248 | return (undef, Template::Constants::STATUS_DECLINED); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | 1; | ||
| 253 | |||
| 254 | __END__ |