From 57010471b0605e91cb1bfb8e38955b3a72f3dec3 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 31 Oct 2017 08:20:14 -0400
Subject: [PATCH] Refactor initializers to prepare for ServiceManager v3.
 (#1066)

---
 module/VuFind/config/module.config.php        |  4 +-
 .../src/VuFind/Controller/CoverController.php |  5 +-
 .../VuFind/Controller/QRCodeController.php    |  5 +-
 .../ServiceManager/AbstractPluginManager.php  |  2 +-
 ...Initializer.php => ServiceInitializer.php} | 52 +++--------------
 .../VuFindPluginInitializer.php               | 58 +++++++++++++++++++
 .../ServiceManager/ZendPluginInitializer.php  | 57 ++++++++++++++++++
 .../src/VuFindTest/Auth/ShibbolethTest.php    |  5 +-
 8 files changed, 132 insertions(+), 56 deletions(-)
 rename module/VuFind/src/VuFind/ServiceManager/{Initializer.php => ServiceInitializer.php} (67%)
 create mode 100644 module/VuFind/src/VuFind/ServiceManager/VuFindPluginInitializer.php
 create mode 100644 module/VuFind/src/VuFind/ServiceManager/ZendPluginInitializer.php

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 3fb4db62d2b..7caf5bd9356 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -249,7 +249,7 @@ $config = [
             'VuFind\Session\Settings' => 'VuFind\Session\Settings',
         ],
         'initializers' => [
-            'VuFind\ServiceManager\Initializer::initInstance',
+            'VuFind\ServiceManager\ServiceInitializer',
         ],
         'aliases' => [
             'mvctranslator' => 'VuFind\Translator',
@@ -259,7 +259,7 @@ $config = [
     'translator' => [],
     'view_helpers' => [
         'initializers' => [
-            'VuFind\ServiceManager\Initializer::initZendPlugin',
+            'VuFind\ServiceManager\ZendPluginInitializer',
         ],
     ],
     'view_manager' => [
diff --git a/module/VuFind/src/VuFind/Controller/CoverController.php b/module/VuFind/src/VuFind/Controller/CoverController.php
index 8e62c53bfe0..f71d0ce3072 100644
--- a/module/VuFind/src/VuFind/Controller/CoverController.php
+++ b/module/VuFind/src/VuFind/Controller/CoverController.php
@@ -83,9 +83,8 @@ class CoverController extends AbstractBase
                 $this->serviceLocator->get('VuFind\Http')->createClient(),
                 $cacheDir
             );
-            \VuFind\ServiceManager\Initializer::initInstance(
-                $this->loader, $this->serviceLocator
-            );
+            $initializer = new \VuFind\ServiceManager\ServiceInitializer();
+            $initializer->initialize($this->loader, $this->serviceLocator);
         }
         return $this->loader;
     }
diff --git a/module/VuFind/src/VuFind/Controller/QRCodeController.php b/module/VuFind/src/VuFind/Controller/QRCodeController.php
index 6937ff1151d..d5e286e496f 100644
--- a/module/VuFind/src/VuFind/Controller/QRCodeController.php
+++ b/module/VuFind/src/VuFind/Controller/QRCodeController.php
@@ -62,9 +62,8 @@ class QRCodeController extends AbstractBase
                 $this->getConfig(),
                 $this->serviceLocator->get('VuFindTheme\ThemeInfo')
             );
-            \VuFind\ServiceManager\Initializer::initInstance(
-                $this->loader, $this->serviceLocator
-            );
+            $initializer = new \VuFind\ServiceManager\ServiceInitializer();
+            $initializer->initialize($this->loader, $this->serviceLocator);
         }
         return $this->loader;
     }
diff --git a/module/VuFind/src/VuFind/ServiceManager/AbstractPluginManager.php b/module/VuFind/src/VuFind/ServiceManager/AbstractPluginManager.php
index de6dbe75522..f766a7d4574 100644
--- a/module/VuFind/src/VuFind/ServiceManager/AbstractPluginManager.php
+++ b/module/VuFind/src/VuFind/ServiceManager/AbstractPluginManager.php
@@ -57,7 +57,7 @@ abstract class AbstractPluginManager extends Base
     ) {
         parent::__construct($configOrContainerInstance, $v3config);
         $this->addInitializer(
-            ['VuFind\ServiceManager\Initializer', 'initPlugin'], false
+            'VuFind\ServiceManager\VuFindPluginInitializer', false
         );
     }
 
diff --git a/module/VuFind/src/VuFind/ServiceManager/Initializer.php b/module/VuFind/src/VuFind/ServiceManager/ServiceInitializer.php
similarity index 67%
rename from module/VuFind/src/VuFind/ServiceManager/Initializer.php
rename to module/VuFind/src/VuFind/ServiceManager/ServiceInitializer.php
index 1872c148fdd..11cf43e73a0 100644
--- a/module/VuFind/src/VuFind/ServiceManager/Initializer.php
+++ b/module/VuFind/src/VuFind/ServiceManager/ServiceInitializer.php
@@ -27,7 +27,7 @@
  */
 namespace VuFind\ServiceManager;
 
-use Zend\ServiceManager\ServiceManager;
+use Zend\ServiceManager\ServiceLocatorInterface;
 
 /**
  * VuFind Service Initializer
@@ -38,16 +38,16 @@ use Zend\ServiceManager\ServiceManager;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     https://vufind.org/wiki/development Wiki
  */
-class Initializer
+class ServiceInitializer implements \Zend\ServiceManager\InitializerInterface
 {
     /**
      * Check if the record cache is enabled within a service manager.
      *
-     * @param ServiceManager $sm Service manager
+     * @param ServiceLocatorInterface $sm Service manager
      *
      * @return bool
      */
-    protected static function isCacheEnabled(ServiceManager $sm)
+    protected function isCacheEnabled(ServiceLocatorInterface $sm)
     {
         // Use static cache to save time on repeated lookups:
         static $enabled = null;
@@ -72,12 +72,12 @@ class Initializer
     /**
      * Given an instance and a Service Manager, initialize the instance.
      *
-     * @param object         $instance Instance to initialize
-     * @param ServiceManager $sm       Service manager
+     * @param object                  $instance Instance to initialize
+     * @param ServiceLocatorInterface $sm       Service manager
      *
      * @return object
      */
-    public static function initInstance($instance, ServiceManager $sm)
+    public function initialize($instance, ServiceLocatorInterface $sm)
     {
         if ($instance instanceof \VuFind\Db\Table\DbTableAwareInterface) {
             $instance->setDbTableManager($sm->get('VuFind\DbTablePluginManager'));
@@ -93,46 +93,10 @@ class Initializer
         }
         // Only inject cache if configuration enabled (to save resources):
         if ($instance instanceof \VuFind\Record\Cache\RecordCacheAwareInterface
-            && static::isCacheEnabled($sm)
+            && $this->isCacheEnabled($sm)
         ) {
             $instance->setRecordCache($sm->get('VuFind\RecordCache'));
         }
         return $instance;
     }
-
-    /**
-     * Given a Zend Framework Plugin Manager, initialize the instance.
-     *
-     * @param object                                     $instance Instance to
-     * initialize
-     * @param \Zend\ServiceManager\AbstractPluginManager $manager  Plugin manager
-     *
-     * @return object
-     */
-    public static function initZendPlugin($instance,
-        \Zend\ServiceManager\AbstractPluginManager $manager
-    ) {
-        $sm = $manager->getServiceLocator();
-        if (null !== $sm) {
-            static::initInstance($instance, $sm);
-        }
-        return $instance;
-    }
-
-    /**
-     * Given an instance and a Plugin Manager, initialize the instance.
-     *
-     * @param object                $instance Instance to initialize
-     * @param AbstractPluginManager $manager  Plugin manager
-     *
-     * @return object
-     */
-    public static function initPlugin($instance, AbstractPluginManager $manager)
-    {
-        static::initZendPlugin($instance, $manager);
-        if (method_exists($instance, 'setPluginManager')) {
-            $instance->setPluginManager($manager);
-        }
-        return $instance;
-    }
 }
diff --git a/module/VuFind/src/VuFind/ServiceManager/VuFindPluginInitializer.php b/module/VuFind/src/VuFind/ServiceManager/VuFindPluginInitializer.php
new file mode 100644
index 00000000000..3f02894dea0
--- /dev/null
+++ b/module/VuFind/src/VuFind/ServiceManager/VuFindPluginInitializer.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * VuFind Plugin Initializer
+ *
+ * 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  ServiceManager
+ * @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\ServiceManager;
+
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * VuFind Plugin Initializer
+ *
+ * @category VuFind
+ * @package  ServiceManager
+ * @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 VuFindPluginInitializer extends ZendPluginInitializer
+{
+    /**
+     * Given an instance and a Plugin Manager, initialize the instance.
+     *
+     * @param object                  $instance Instance to initialize
+     * @param ServiceLocatorInterface $manager  Plugin manager
+     *
+     * @return object
+     */
+    public function initialize($instance, ServiceLocatorInterface $manager)
+    {
+        if (method_exists($instance, 'setPluginManager')) {
+            $instance->setPluginManager($manager);
+        }
+        return parent::initialize($instance, $manager);
+    }
+}
diff --git a/module/VuFind/src/VuFind/ServiceManager/ZendPluginInitializer.php b/module/VuFind/src/VuFind/ServiceManager/ZendPluginInitializer.php
new file mode 100644
index 00000000000..17896becc23
--- /dev/null
+++ b/module/VuFind/src/VuFind/ServiceManager/ZendPluginInitializer.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Zend Framework Plugin Initializer
+ *
+ * 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  ServiceManager
+ * @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\ServiceManager;
+
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * Zend Framework Plugin Initializer
+ *
+ * @category VuFind
+ * @package  ServiceManager
+ * @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 ZendPluginInitializer extends ServiceInitializer
+{
+    /**
+     * Given an instance and a Plugin Manager, initialize the instance.
+     *
+     * @param object                  $instance Instance to initialize
+     * @param ServiceLocatorInterface $manager  Plugin manager
+     *
+     * @return object
+     */
+    public function initialize($instance, ServiceLocatorInterface $manager)
+    {
+        $sm = $manager->getServiceLocator();
+        return (null !== $sm)
+            ? parent::initialize($instance, $sm) : $instance;
+    }
+}
diff --git a/module/VuFind/tests/integration-tests/src/VuFindTest/Auth/ShibbolethTest.php b/module/VuFind/tests/integration-tests/src/VuFindTest/Auth/ShibbolethTest.php
index e08fb39ef40..4387bef40d2 100644
--- a/module/VuFind/tests/integration-tests/src/VuFindTest/Auth/ShibbolethTest.php
+++ b/module/VuFind/tests/integration-tests/src/VuFindTest/Auth/ShibbolethTest.php
@@ -80,9 +80,8 @@ class ShibbolethTest extends \VuFindTest\Unit\DbTestCase
             $config = $this->getAuthConfig();
         }
         $obj = new Shibboleth($this->createMock('Zend\Session\ManagerInterface'));
-        \VuFind\ServiceManager\Initializer::initInstance(
-            $obj, $this->getServiceManager()
-        );
+        $initializer = new \VuFind\ServiceManager\ServiceInitializer();
+        $initializer->initialize($obj, $this->getServiceManager());
         $obj->setConfig($config);
         return $obj;
     }
-- 
GitLab