From fb65e006e17f10a0115dbeb58213035c35e54702 Mon Sep 17 00:00:00 2001
From: Chris Hallberg <crhallberg@gmail.com>
Date: Fri, 19 Jan 2018 13:38:00 -0500
Subject: [PATCH] Modernize Hierarchy/TreeRenderer service management. - Use
 fully qualified class names as service names. - Eliminate static factory. -
 Move configuration to PluginManager.

---
 module/VuFind/config/module.config.php        |  6 +-
 .../VuFind/Hierarchy/TreeRenderer/Factory.php | 60 -----------------
 .../Hierarchy/TreeRenderer/JSTreeFactory.php  | 67 +++++++++++++++++++
 .../Hierarchy/TreeRenderer/PluginManager.php  | 19 ++++++
 4 files changed, 87 insertions(+), 65 deletions(-)
 delete mode 100644 module/VuFind/src/VuFind/Hierarchy/TreeRenderer/Factory.php
 create mode 100644 module/VuFind/src/VuFind/Hierarchy/TreeRenderer/JSTreeFactory.php

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 5b983d4d20f..35f4d2d5c95 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -419,11 +419,7 @@ $config = [
             'hierarchy_driver' => [ /* see VuFind\Hierarchy\Driver\PluginManager for defaults */ ],
             'hierarchy_treedataformatter' => [ /* see VuFind\Hierarchy\TreeDataFormatter\PluginManager for defaults */ ],
             'hierarchy_treedatasource' => [ /* see VuFind\Hierarchy\TreeDataSource\PluginManager for defaults */ ],
-            'hierarchy_treerenderer' => [
-                'factories' => [
-                    'jstree' => 'VuFind\Hierarchy\TreeRenderer\Factory::getJSTree'
-                ],
-            ],
+            'hierarchy_treerenderer' => [ /* see VuFind\Hierarchy\TreeRenderer\PluginManager for defaults */ ],
             'ils_driver' => [ /* See VuFind\ILS\Driver\PluginManager for defaults */ ],
             'recommend' => [ /* See VuFind\Recommend\PluginManager for defaults */ ],
             'recorddriver' => [ /* See VuFind\RecordDriver\PluginManager for defaults */ ],
diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/Factory.php b/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/Factory.php
deleted file mode 100644
index b4d77c2569c..00000000000
--- a/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/Factory.php
+++ /dev/null
@@ -1,60 +0,0 @@
-<?php
-/**
- * Hierarchy Renderer Factory Class
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * @category VuFind
- * @package  HierarchyTree_Renderer
- * @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\Hierarchy\TreeRenderer;
-
-use Zend\ServiceManager\ServiceManager;
-
-/**
- * Hierarchy Renderer Factory Class
- *
- * This is a factory class to build objects for rendering hierarchies.
- *
- * @category VuFind
- * @package  HierarchyTree_DataSource
- * @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 JSTree renderer.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return JSTree
-     */
-    public static function getJSTree(ServiceManager $sm)
-    {
-        return new JSTree(
-            $sm->get('ControllerPluginManager')->get('Url')
-        );
-    }
-}
diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/JSTreeFactory.php b/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/JSTreeFactory.php
new file mode 100644
index 00000000000..053734c059a
--- /dev/null
+++ b/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/JSTreeFactory.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * JSTree hierarchy tree renderer plugin factory.
+ *
+ * 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  HierarchyTree_Renderer
+ * @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\Hierarchy\TreeRenderer;
+
+use Interop\Container\ContainerInterface;
+
+/**
+ * JSTree hierarchy tree renderer plugin factory.
+ *
+ * @category VuFind
+ * @package  HierarchyTree_Renderer
+ * @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 JSTreeFactory implements \Zend\ServiceManager\Factory\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
+    ) {
+        if ($options !== null) {
+            throw new \Exception('Unexpected options sent to factory!');
+        }
+        return new $requestedName(
+            $container->get('ControllerPluginManager')->get('Url')
+        );
+    }
+}
diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/PluginManager.php b/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/PluginManager.php
index 4ea337b1344..4470811ec3c 100644
--- a/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/PluginManager.php
+++ b/module/VuFind/src/VuFind/Hierarchy/TreeRenderer/PluginManager.php
@@ -38,6 +38,25 @@ namespace VuFind\Hierarchy\TreeRenderer;
  */
 class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
 {
+    /**
+     * Default plugin aliases.
+     *
+     * @var array
+     */
+    protected $aliases = [
+        'jstree' => 'VuFind\Hierarchy\TreeRenderer\JSTree'
+    ];
+
+    /**
+     * Default plugin factories.
+     *
+     * @var array
+     */
+    protected $factories = [
+        'VuFind\Hierarchy\TreeRenderer\JSTree' =>
+            'VuFind\Hierarchy\TreeRenderer\JSTreeFactory'
+    ];
+
     /**
      * Return the name of the base class or interface that plug-ins must conform
      * to.
-- 
GitLab