From a992a476bfdcec5b583534531aa38e22b61bc16c Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 24 Jan 2018 13:04:34 -0500
Subject: [PATCH] Modernize resolver driver service configuration. - Use fully
 qualified class names as service names. - Move configuration to plugin
 manager. - Eliminate static factories.

---
 module/VuFind/config/module.config.php        |  16 +--
 .../Driver/DriverWithHttpClientFactory.php    |  68 +++++++++++
 .../src/VuFind/Resolver/Driver/Factory.php    | 108 ------------------
 .../VuFind/Resolver/Driver/PluginManager.php  |  48 ++++++++
 4 files changed, 117 insertions(+), 123 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/Resolver/Driver/DriverWithHttpClientFactory.php
 delete mode 100644 module/VuFind/src/VuFind/Resolver/Driver/Factory.php

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 391388d17af..c2940062d09 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -446,21 +446,7 @@ $config = [
             'recorddriver' => [ /* See VuFind\RecordDriver\PluginManager for defaults */ ],
             'recordtab' => [ /* See VuFind\RecordTab\PluginManager for defaults */ ],
             'related' => [ /* See VuFind\Related\PluginManager for defaults */ ],
-            'resolver_driver' => [
-                'abstract_factories' => ['VuFind\Resolver\Driver\PluginFactory'],
-                'factories' => [
-                    '360link' => 'VuFind\Resolver\Driver\Factory::getThreesixtylink',
-                    'ezb' => 'VuFind\Resolver\Driver\Factory::getEzb',
-                    'sfx' => 'VuFind\Resolver\Driver\Factory::getSfx',
-                    'redi' => 'VuFind\Resolver\Driver\Factory::getRedi',
-                ],
-                'invokables' => [
-                    'demo' => 'VuFind\Resolver\Driver\Demo',
-                ],
-                'aliases' => [
-                    'threesixtylink' => '360link',
-                ],
-            ],
+            'resolver_driver' => [ /* See VuFind\Resolver\Driver\PluginManager for defaults */ ],
             'search_backend' => [ /* See VuFind\Search\BackendRegistry for defaults */ ],
             'search_options' => [ /* See VuFind\Search\Options\PluginManager for defaults */ ],
             'search_params' => [ /* See VuFind\Search\Params\PluginManager for defaults */ ],
diff --git a/module/VuFind/src/VuFind/Resolver/Driver/DriverWithHttpClientFactory.php b/module/VuFind/src/VuFind/Resolver/Driver/DriverWithHttpClientFactory.php
new file mode 100644
index 00000000000..5c791540454
--- /dev/null
+++ b/module/VuFind/src/VuFind/Resolver/Driver/DriverWithHttpClientFactory.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Generic factory suitable for most resolver drivers.
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2018.
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * @category VuFind
+ * @package  Resolver_Drivers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+namespace VuFind\Resolver\Driver;
+
+use Interop\Container\ContainerInterface;
+use Zend\ServiceManager\Factory\FactoryInterface;
+
+/**
+ * Generic factory suitable for most resolver drivers.
+ *
+ * @category VuFind
+ * @package  Resolver_Drivers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+class DriverWithHttpClientFactory implements FactoryInterface
+{
+    /**
+     * Create an object
+     *
+     * @param ContainerInterface $container     Service manager
+     * @param string             $requestedName Service being created
+     * @param null|array         $options       Extra options (optional)
+     *
+     * @return object
+     *
+     * @throws ServiceNotFoundException if unable to resolve the service.
+     * @throws ServiceNotCreatedException if an exception is raised when
+     * creating a service.
+     * @throws ContainerException if any other error occurs
+     */
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $options = null
+    ) {
+        $config = $container->get('VuFind\Config')->get('config');
+        return new $requestedName(
+            $config->OpenURL->url,
+            $container->get('VuFind\Http')->createClient(),
+            ...($options ?: [])
+        );
+    }
+}
diff --git a/module/VuFind/src/VuFind/Resolver/Driver/Factory.php b/module/VuFind/src/VuFind/Resolver/Driver/Factory.php
deleted file mode 100644
index c60f6f1ba86..00000000000
--- a/module/VuFind/src/VuFind/Resolver/Driver/Factory.php
+++ /dev/null
@@ -1,108 +0,0 @@
-<?php
-/**
- * Resolver Driver Factory Class
- *
- * PHP version 5
- *
- * Copyright (C) Villanova University 2014.
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * @category VuFind
- * @package  Resolver_Drivers
- * @author   Demian Katz <demian.katz@villanova.edu>
- * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
- */
-namespace VuFind\Resolver\Driver;
-
-use Zend\ServiceManager\ServiceManager;
-
-/**
- * Resolver Driver Factory Class
- *
- * @category VuFind
- * @package  Resolver_Drivers
- * @author   Demian Katz <demian.katz@villanova.edu>
- * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
- *
- * @codeCoverageIgnore
- */
-class Factory
-{
-    /**
-     * Factory for Threesixtylink record driver.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return Threesixtylink
-     */
-    public static function getThreesixtylink(ServiceManager $sm)
-    {
-        $config = $sm->get('VuFind\Config')->get('config');
-        return new Threesixtylink(
-            $config->OpenURL->url,
-            $sm->get('VuFind\Http')->createClient()
-        );
-    }
-
-    /**
-     * Factory for Ezb record driver.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return Ezb
-     */
-    public static function getEzb(ServiceManager $sm)
-    {
-        $config = $sm->get('VuFind\Config')->get('config');
-        return new Ezb(
-            $config->OpenURL->url,
-            $sm->get('VuFind\Http')->createClient()
-        );
-    }
-
-    /**
-     * Factory for Sfx record driver.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return Sfx
-     */
-    public static function getSfx(ServiceManager $sm)
-    {
-        $config = $sm->get('VuFind\Config')->get('config');
-        return new Sfx(
-            $config->OpenURL->url,
-            $sm->get('VuFind\Http')->createClient()
-        );
-    }
-
-    /**
-     * Factory for Redi record driver.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return Redi
-     */
-    public static function getRedi(ServiceManager $sm)
-    {
-        $config = $sm->get('VuFind\Config')->get('config');
-        return new Redi(
-            $config->OpenURL->url,
-            $sm->get('VuFind\Http')->createClient()
-        );
-    }
-}
diff --git a/module/VuFind/src/VuFind/Resolver/Driver/PluginManager.php b/module/VuFind/src/VuFind/Resolver/Driver/PluginManager.php
index a003c23f6d3..e3a14201cb9 100644
--- a/module/VuFind/src/VuFind/Resolver/Driver/PluginManager.php
+++ b/module/VuFind/src/VuFind/Resolver/Driver/PluginManager.php
@@ -38,6 +38,54 @@ namespace VuFind\Resolver\Driver;
  */
 class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
 {
+    /**
+     * Default plugin aliases.
+     *
+     * @var array
+     */
+    protected $aliases = [
+        '360link' => 'VuFind\Resolver\Driver\Threesixtylink',
+        'demo' => 'VuFind\Resolver\Driver\Demo',
+        'ezb' => 'VuFind\Resolver\Driver\Ezb',
+        'sfx' => 'VuFind\Resolver\Driver\Sfx',
+        'redi' => 'VuFind\Resolver\Driver\Redi',
+        'threesixtylink' => 'VuFind\Resolver\Driver\Threesixtylink',
+    ];
+
+    /**
+     * Default plugin factories.
+     *
+     * @var array
+     */
+    protected $factories = [
+        'VuFind\Resolver\Driver\Threesixtylink' =>
+            'VuFind\Resolver\Driver\DriverWithHttpClientFactory',
+        'VuFind\Resolver\Driver\Demo' =>
+            'Zend\ServiceManager\Factory\InvokableFactory',
+        'VuFind\Resolver\Driver\Ezb' =>
+            'VuFind\Resolver\Driver\DriverWithHttpClientFactory',
+        'VuFind\Resolver\Driver\Sfx' =>
+            'VuFind\Resolver\Driver\DriverWithHttpClientFactory',
+        'VuFind\Resolver\Driver\Redi' =>
+            'VuFind\Resolver\Driver\DriverWithHttpClientFactory',
+    ];
+
+    /**
+     * Constructor
+     *
+     * Make sure plugins are properly initialized.
+     *
+     * @param mixed $configOrContainerInstance Configuration or container instance
+     * @param array $v3config                  If $configOrContainerInstance is a
+     * container, this value will be passed to the parent constructor.
+     */
+    public function __construct($configOrContainerInstance = null,
+        array $v3config = []
+    ) {
+        $this->addAbstractFactory('VuFind\Resolver\Driver\PluginFactory');
+        parent::__construct($configOrContainerInstance, $v3config);
+    }
+
     /**
      * Return the name of the base class or interface that plug-ins must conform
      * to.
-- 
GitLab