From 3e4747d1b40f62b3f25b96154f45d2578ac6cc64 Mon Sep 17 00:00:00 2001
From: Dorian Merz <merz@ub.uni-leipzig.de>
Date: Wed, 6 Nov 2019 12:29:39 +0100
Subject: [PATCH] refs #16246 [master-v5] adapts OpenURL resolvers to VF5

* changes according new Zend ServiceManager configuration
---
 module/finc/config/module.config.php          |  8 +-
 .../finc/src/finc/Resolver/Driver/Factory.php | 76 -------------------
 .../Driver/FincResolverDriverFactory.php      | 69 +++++++++++++++++
 3 files changed, 75 insertions(+), 78 deletions(-)
 delete mode 100644 module/finc/src/finc/Resolver/Driver/Factory.php
 create mode 100644 module/finc/src/finc/Resolver/Driver/FincResolverDriverFactory.php

diff --git a/module/finc/config/module.config.php b/module/finc/config/module.config.php
index f54b13f2f0d..babf9b01b08 100644
--- a/module/finc/config/module.config.php
+++ b/module/finc/config/module.config.php
@@ -112,9 +112,13 @@ $config = [
             ],
             'resolver_driver' => [
                 'factories' => [
-                    'ezb' => 'finc\Resolver\Driver\Factory::getEzb',
-                    'redi' => 'finc\Resolver\Driver\Factory::getRedi'
+                    'finc\Resolver\Driver\Ezb' => 'finc\Resolver\Driver\FincResolverDriverFactory',
+                    'finc\Resolver\Driver\Redi' => 'finc\Resolver\Driver\FincResolverDriverFactory'
                 ],
+                'aliases' => [
+                    'ezb' => 'finc\Resolver\Driver\Ezb',
+                    'redi' => 'finc\Resolver\Driver\Redi'
+                ]
             ],
             'hierarchy_treedataformatter' => [
                 'factories' => [
diff --git a/module/finc/src/finc/Resolver/Driver/Factory.php b/module/finc/src/finc/Resolver/Driver/Factory.php
deleted file mode 100644
index 2393eade3a1..00000000000
--- a/module/finc/src/finc/Resolver/Driver/Factory.php
+++ /dev/null
@@ -1,76 +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 finc\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 Ezb record driver.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return Ezb
-     */
-    public static function getEzb(ServiceManager $sm)
-    {
-        $config = $sm->getServiceLocator()->get('VuFind\Config')->get('Resolver');
-        return new Ezb(
-            $config->Ezb,
-            $sm->getServiceLocator()->get('VuFind\Http')->createClient()
-        );
-    }
-
-    /**
-     * Factory for Redi record driver.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return finc\Resolver\Driver\Redi
-     */
-    public static function getRedi(ServiceManager $sm)
-    {
-        $config = $sm->getServiceLocator()->get('VuFind\Config')->get('Resolver');
-        return new Redi(
-            $config->Redi->url,
-            $sm->getServiceLocator()->get('VuFind\Http')->createClient()
-        );
-    }
-}
diff --git a/module/finc/src/finc/Resolver/Driver/FincResolverDriverFactory.php b/module/finc/src/finc/Resolver/Driver/FincResolverDriverFactory.php
new file mode 100644
index 00000000000..c57fb6595b8
--- /dev/null
+++ b/module/finc/src/finc/Resolver/Driver/FincResolverDriverFactory.php
@@ -0,0 +1,69 @@
+<?php
+
+/**
+ * Copyright (C) Leipzig University Library 2019.
+ *
+ * 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
+ *
+ * @author   Dorian Merz <merz@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ */
+
+namespace finc\Resolver\Driver;
+
+use VuFind\Resolver\Driver\DriverInterface;
+use Zend\Config\Config;
+use Zend\ServiceManager\Factory\FactoryInterface;
+use Interop\Container\ContainerInterface;
+
+class FincResolverDriverFactory 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
+    ) {
+        $resolverConfig = $this->getResolverConfig($container,$requestedName);
+        $client = $container->get('VuFindHttp\HttpService')->createClient();
+        return new $requestedName(
+            $resolverConfig,
+            $client
+        );
+    }
+
+    protected function getResolverConfig(ContainerInterface $container,$requestedName) {
+        /** @var Config $resolverConfig */
+        $resolverConfig = $container->get('VuFind\Config')->get('Resolver');
+        $name = $requestedName;
+        if ($config = $resolverConfig->get($name)) return $config;
+        $parts = explode('\\',$requestedName);
+        $name = array_pop($parts);
+        if ($config = $resolverConfig->get($name)) return $config;
+        $name = strtolower($name);
+        if ($config = $resolverConfig->get($name)) return $config;
+        return null;
+    }
+}
\ No newline at end of file
-- 
GitLab