From 1720db92f5e7722d21dde2912b2812fe7eff2dfa Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Tue, 11 Sep 2012 10:58:26 -0400 Subject: [PATCH] Added plugin manager for link resolver drivers; changed resolver connection class to use dependency injection and to eliminate unnecessary complexity. --- module/VuFind/config/module.config.php | 11 +++ module/VuFind/src/VuFind/Bootstrap.php | 2 +- .../src/VuFind/Controller/AjaxController.php | 12 ++- .../VuFind/src/VuFind/Resolver/Connection.php | 76 +++++++------------ .../VuFind/Resolver/Driver/PluginFactory.php | 48 ++++++++++++ .../VuFind/Resolver/Driver/PluginManager.php | 51 +++++++++++++ 6 files changed, 148 insertions(+), 52 deletions(-) create mode 100644 module/VuFind/src/VuFind/Resolver/Driver/PluginFactory.php create mode 100644 module/VuFind/src/VuFind/Resolver/Driver/PluginManager.php diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 758599e3ebf..ac0bcf7215a 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -209,6 +209,17 @@ $config = array( 'worldcatsimilar' => 'VuFind\Related\WorldCatSimilar', ), ), + 'resolver_driver_plugin_manager' => array( + 'abstract_factories' => array('VuFind\Resolver\Driver\PluginFactory'), + 'invokables' => array( + '360link' => 'VuFind\Resolver\Driver\Threesixtylink', + 'ezb' => 'VuFind\Resolver\Driver\Ezb', + 'sfx' => 'VuFind\Resolver\Driver\Sfx', + ), + 'aliases' => array( + 'threesixtylink' => '360link', + ), + ), 'search_manager' => array( 'default_namespace' => 'VuFind\Search', 'namespaces_by_id' => array( diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php index e85e547b85b..05bb596364f 100644 --- a/module/VuFind/src/VuFind/Bootstrap.php +++ b/module/VuFind/src/VuFind/Bootstrap.php @@ -92,7 +92,7 @@ class Bootstrap // Use naming conventions to set up a bunch of services based on namespace: $namespaces = array( 'Auth', 'Autocomplete', 'ILS\Driver', 'Recommend', 'RecordDriver', - 'Related', 'Session' + 'Related', 'Resolver\Driver', 'Session' ); foreach ($namespaces as $ns) { $serviceName = str_replace('\\', '', $ns) . 'PluginManager'; diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php index b1c42163614..1e08c53d6f7 100644 --- a/module/VuFind/src/VuFind/Controller/AjaxController.php +++ b/module/VuFind/src/VuFind/Controller/AjaxController.php @@ -1227,14 +1227,20 @@ class AjaxController extends AbstractBase $config = ConfigReader::getConfig(); $resolverType = isset($config->OpenURL->resolver) ? $config->OpenURL->resolver : 'other'; - $resolver = new \VuFind\Resolver\Connection($resolverType); - if (!$resolver->driverLoaded()) { + $pluginManager = $this->getServiceLocator() + ->get('ResolverDriverPluginManager'); + if (!$pluginManager->has($resolverType)) { return $this->output( Translator::translate("Could not load driver for $resolverType"), self::STATUS_ERROR ); } - + $resolver = new \VuFind\Resolver\Connection( + $pluginManager->get($resolverType) + ); + if (isset($config->OpenURL->resolver_cache)) { + $resolver->enableCache($config->OpenURL->resolver_cache); + } $result = $resolver->fetchLinks($openUrl); // Sort the returned links into categories based on service type: diff --git a/module/VuFind/src/VuFind/Resolver/Connection.php b/module/VuFind/src/VuFind/Resolver/Connection.php index 392dc314234..6f6e764a075 100644 --- a/module/VuFind/src/VuFind/Resolver/Connection.php +++ b/module/VuFind/src/VuFind/Resolver/Connection.php @@ -48,14 +48,6 @@ use VuFind\Config\Reader as ConfigReader; */ class Connection { - /** - * A boolean value that defines whether a connection has been successfully - * made. - * - * @var bool - */ - public $status = false; - /** * The object of the appropriate driver. * @@ -64,58 +56,49 @@ class Connection protected $driver = false; /** - * A boolean value that defines whether to cache the resolver results - * - * @var bool - */ - protected $useCache = false; - - /** - * The path to the resolver cache, if any + * The path to the resolver cache, if any (empty string for no caching) * * @var string */ - protected $cachePath; + protected $cachePath = ''; /** * Constructor * * This is responsible for instantiating the driver that has been specified. * - * @param string $driver The name of the driver to load. + * @param \VuFind\Resolver\Driver\DriverInterface $driver The driver to use */ - public function __construct($driver) + public function __construct(\VuFind\Resolver\Driver\DriverInterface $driver) { - // Backward compatibility -- can't have class beginning with number: - if (strtolower($driver) == '360link') { - $driver = 'threesixtylink'; - } - $class = 'VuFind\Resolver\Driver\\' . ucwords($driver); - if (class_exists($class)) { - $this->driver = new $class(); - $this->status = true; - $config = ConfigReader::getConfig(); - if (isset($config->OpenURL->resolver_cache) - && is_dir($config->OpenURL->resolver_cache) - && is_writable($config->OpenURL->resolver_cache) - ) { - $this->useCache = true; - $this->cachePath = $config->OpenURL->resolver_cache; - if (!(substr($this->cachePath, -1) == '/')) { - $this->cachePath .= '/'; - } - } - } + $this->driver = $driver; + } + + /** + * Disable caching. + * + * @return void + */ + public function disableCache() + { + $this->cachePath = ''; } /** - * Check if driver loaded successfully. + * Enable caching. + * + * @param string $cacheDir Directory to use for cache. * - * @return bool + * @return void */ - public function driverLoaded() + public function enableCache($cacheDir) { - return is_object($this->driver); + if (is_dir($cacheDir) && is_writable($cacheDir)) { + $this->cachePath = $cacheDir; + if (!(substr($this->cachePath, -1) == '/')) { + $this->cachePath .= '/'; + } + } } /** @@ -133,16 +116,13 @@ class Connection */ public function fetchLinks($openURL) { - $cache_found = false; - if ($this->useCache) { + if (!empty($this->cachePath)) { $hashedURL = md5($openURL); if (file_exists($this->cachePath . $hashedURL)) { $links = file_get_contents($this->cachePath . $hashedURL); } else { $links = $this->driver->fetchLinks($openURL); - $fp = fopen($this->cachePath . $hashedURL, 'w'); - fwrite($fp, $links); - fclose($fp); + file_put_contents($this->cachePath . $hashedURL, $links); } } else { $links = $this->driver->fetchLinks($openURL); diff --git a/module/VuFind/src/VuFind/Resolver/Driver/PluginFactory.php b/module/VuFind/src/VuFind/Resolver/Driver/PluginFactory.php new file mode 100644 index 00000000000..1a7262d028f --- /dev/null +++ b/module/VuFind/src/VuFind/Resolver/Driver/PluginFactory.php @@ -0,0 +1,48 @@ +<?php +/** + * Resolver driver plugin factory + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Resolver_Drivers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +namespace VuFind\Resolver\Driver; + +/** + * Resolver driver plugin factory + * + * @category VuFind2 + * @package Resolver_Drivers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +class PluginFactory extends \VuFind\ServiceManager\AbstractPluginFactory +{ + /** + * Constructor + */ + public function __construct() + { + $this->defaultNamespace = 'VuFind\Resolver\Driver'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Resolver/Driver/PluginManager.php b/module/VuFind/src/VuFind/Resolver/Driver/PluginManager.php new file mode 100644 index 00000000000..2da97e8eb8e --- /dev/null +++ b/module/VuFind/src/VuFind/Resolver/Driver/PluginManager.php @@ -0,0 +1,51 @@ +<?php +/** + * Resolver driver plugin manager + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Resolver_Drivers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +namespace VuFind\Resolver\Driver; + +/** + * Resolver driver plugin manager + * + * @category VuFind2 + * @package Resolver_Drivers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager +{ + /** + * Return the name of the base class or interface that plug-ins must conform + * to. + * + * @return string + */ + protected function getExpectedInterface() + { + return 'VuFind\Resolver\Driver\DriverInterface'; + } +} \ No newline at end of file -- GitLab