From 800df8a9d422365a681ff906e6f3517fb7aadee8 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 24 Jan 2019 15:11:46 -0500
Subject: [PATCH] Remove static VuFindTheme view helper factories.

---
 module/VuFindTheme/Module.php                 |  31 ++--
 .../src/VuFindTheme/View/Helper/Factory.php   | 147 ------------------
 .../View/Helper/HeadThemeResourcesFactory.php |  68 ++++++++
 .../View/Helper/ImageLinkFactory.php          |  68 ++++++++
 .../View/Helper/PipelineInjectorFactory.php   | 100 ++++++++++++
 5 files changed, 253 insertions(+), 161 deletions(-)
 delete mode 100644 module/VuFindTheme/src/VuFindTheme/View/Helper/Factory.php
 create mode 100644 module/VuFindTheme/src/VuFindTheme/View/Helper/HeadThemeResourcesFactory.php
 create mode 100644 module/VuFindTheme/src/VuFindTheme/View/Helper/ImageLinkFactory.php
 create mode 100644 module/VuFindTheme/src/VuFindTheme/View/Helper/PipelineInjectorFactory.php

diff --git a/module/VuFindTheme/Module.php b/module/VuFindTheme/Module.php
index 4d3bd2ba43a..51ced41c3d5 100644
--- a/module/VuFindTheme/Module.php
+++ b/module/VuFindTheme/Module.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * ZF2 module definition for the VuFind theme system.
+ * Module definition for the VuFind theme system.
  *
  * PHP version 7
  *
@@ -30,7 +30,7 @@ namespace VuFindTheme;
 use Zend\ServiceManager\Factory\InvokableFactory;
 
 /**
- * ZF2 module definition for the VuFind theme system.
+ * Module definition for the VuFind theme system.
  *
  * @category VuFind
  * @package  Theme
@@ -84,20 +84,23 @@ class Module
     {
         return [
             'factories' => [
-                'VuFindTheme\View\Helper\HeadThemeResources' =>
-                    'VuFindTheme\View\Helper\Factory::getHeadThemeResources',
-                'VuFindTheme\View\Helper\ImageLink' =>
-                    'VuFindTheme\View\Helper\Factory::getImageLink',
-                'Zend\View\Helper\HeadLink' =>
-                    'VuFindTheme\View\Helper\Factory::getHeadLink',
-                'Zend\View\Helper\HeadScript' =>
-                    'VuFindTheme\View\Helper\Factory::getHeadScript',
-                'Zend\View\Helper\InlineScript' =>
-                    'VuFindTheme\View\Helper\Factory::getInlineScript',
+                View\Helper\HeadThemeResources::class =>
+                    View\Helper\HeadThemeResourcesFactory::class,
+                View\Helper\ImageLink::class => View\Helper\ImageLinkFactory::class,
+                View\Helper\HeadLink::class =>
+                    View\Helper\PipelineInjectorFactory::class,
+                View\Helper\HeadScript::class =>
+                    View\Helper\PipelineInjectorFactory::class,
+                View\Helper\InlineScript::class =>
+                    View\Helper\PipelineInjectorFactory::class,
             ],
             'aliases' => [
-                'headThemeResources' => 'VuFindTheme\View\Helper\HeadThemeResources',
-                'imageLink' => 'VuFindTheme\View\Helper\ImageLink',
+                'headThemeResources' => View\Helper\HeadThemeResources::class,
+                'imageLink' => View\Helper\ImageLink::class,
+                \Zend\View\Helper\HeadLink::class => View\Helper\HeadLink::class,
+                \Zend\View\Helper\HeadScript::class => View\Helper\HeadScript::class,
+                \Zend\View\Helper\InlineScript::class =>
+                    View\Helper\InlineScript::class,
             ],
         ];
     }
diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/Factory.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/Factory.php
deleted file mode 100644
index 662e5dfeeee..00000000000
--- a/module/VuFindTheme/src/VuFindTheme/View/Helper/Factory.php
+++ /dev/null
@@ -1,147 +0,0 @@
-<?php
-/**
- * Factory for VuFindTheme view helpers.
- *
- * PHP version 7
- *
- * 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  View_Helpers
- * @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 VuFindTheme\View\Helper;
-
-use Zend\ServiceManager\ServiceManager;
-
-/**
- * Factory for VuFindTheme view helpers.
- *
- * @category VuFind
- * @package  View_Helpers
- * @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
- *
- * @codeCoverageIgnore
- */
-class Factory
-{
-    /**
-     * Split config and return prefixed setting with current environment.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return string|bool
-     */
-    protected static function getPipelineConfig(ServiceManager $sm)
-    {
-        $config = $sm->get('VuFind\Config\PluginManager')->get('config');
-        $default = false;
-        if (isset($config['Site']['asset_pipeline'])) {
-            $settings = array_map(
-                'trim',
-                explode(';', $config['Site']['asset_pipeline'])
-            );
-            foreach ($settings as $setting) {
-                $parts = array_map('trim', explode(':', $setting));
-                if (APPLICATION_ENV === $parts[0]) {
-                    return $parts[1];
-                } elseif (count($parts) == 1) {
-                    $default = $parts[0];
-                } elseif ($parts[0] === '*') {
-                    $default = $parts[1];
-                }
-            }
-        }
-        return $default;
-    }
-
-    /**
-     * Construct the HeadLink helper.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return HeadLink
-     */
-    public static function getHeadLink(ServiceManager $sm)
-    {
-        return new HeadLink(
-            $sm->get('VuFindTheme\ThemeInfo'),
-            Factory::getPipelineConfig($sm)
-        );
-    }
-
-    /**
-     * Construct the HeadScript helper.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return HeadScript
-     */
-    public static function getHeadScript(ServiceManager $sm)
-    {
-        return new HeadScript(
-            $sm->get('VuFindTheme\ThemeInfo'),
-            Factory::getPipelineConfig($sm)
-        );
-    }
-
-    /**
-     * Construct the HeadThemeResources helper.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return HeadThemeResources
-     */
-    public static function getHeadThemeResources(ServiceManager $sm)
-    {
-        return new HeadThemeResources(
-            $sm->get('VuFindTheme\ResourceContainer')
-        );
-    }
-
-    /**
-     * Construct the ImageLink helper.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return ImageLink
-     */
-    public static function getImageLink(ServiceManager $sm)
-    {
-        return new ImageLink(
-            $sm->get('VuFindTheme\ThemeInfo')
-        );
-    }
-
-    /**
-     * Construct the InlineScript helper.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return InlineScript
-     */
-    public static function getInlineScript(ServiceManager $sm)
-    {
-        return new InlineScript(
-            $sm->get('VuFindTheme\ThemeInfo'),
-            Factory::getPipelineConfig($sm)
-        );
-    }
-}
diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadThemeResourcesFactory.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadThemeResourcesFactory.php
new file mode 100644
index 00000000000..a6253e7a1b9
--- /dev/null
+++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadThemeResourcesFactory.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Factory for HeadThemeResources view helper.
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 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
+ *
+ * @category VuFind
+ * @package  Theme
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+namespace VuFindTheme\View\Helper;
+
+use Interop\Container\ContainerInterface;
+use Zend\ServiceManager\Factory\FactoryInterface;
+
+/**
+ * Factory for HeadThemeResources view helper.
+ *
+ * @category VuFind
+ * @package  Theme
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+class HeadThemeResourcesFactory 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
+    ) {
+        if (!empty($options)) {
+            throw new \Exception('Unexpected options sent to factory.');
+        }
+        return new $requestedName(
+            $container->get(\VuFindTheme\ResourceContainer::class)
+        );
+    }
+}
diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/ImageLinkFactory.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/ImageLinkFactory.php
new file mode 100644
index 00000000000..eeda2a5e1d9
--- /dev/null
+++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/ImageLinkFactory.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Factory for ImageLink view helper.
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 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
+ *
+ * @category VuFind
+ * @package  Theme
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+namespace VuFindTheme\View\Helper;
+
+use Interop\Container\ContainerInterface;
+use Zend\ServiceManager\Factory\FactoryInterface;
+
+/**
+ * Factory for ImageLink view helper.
+ *
+ * @category VuFind
+ * @package  Theme
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+class ImageLinkFactory 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
+    ) {
+        if (!empty($options)) {
+            throw new \Exception('Unexpected options sent to factory.');
+        }
+        return new $requestedName(
+            $container->get(\VuFindTheme\ThemeInfo::class)
+        );
+    }
+}
diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/PipelineInjectorFactory.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/PipelineInjectorFactory.php
new file mode 100644
index 00000000000..dc508babdb3
--- /dev/null
+++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/PipelineInjectorFactory.php
@@ -0,0 +1,100 @@
+<?php
+/**
+ * Factory for helpers relying on asset pipeline configuration.
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 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
+ *
+ * @category VuFind
+ * @package  Theme
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+namespace VuFindTheme\View\Helper;
+
+use Interop\Container\ContainerInterface;
+use Zend\Config\Config;
+use Zend\ServiceManager\Factory\FactoryInterface;
+
+/**
+ * Factory for helpers relying on asset pipeline configuration.
+ *
+ * @category VuFind
+ * @package  Theme
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+class PipelineInjectorFactory implements FactoryInterface
+{
+    /**
+     * Split config and return prefixed setting with current environment.
+     *
+     * @param Config $config Configuration settings
+     *
+     * @return string|bool
+     */
+    protected function getPipelineConfig(Config $config)
+    {
+        $default = false;
+        if (isset($config['Site']['asset_pipeline'])) {
+            $settings = array_map(
+                'trim',
+                explode(';', $config['Site']['asset_pipeline'])
+            );
+            foreach ($settings as $setting) {
+                $parts = array_map('trim', explode(':', $setting));
+                if (APPLICATION_ENV === $parts[0]) {
+                    return $parts[1];
+                } elseif (count($parts) == 1) {
+                    $default = $parts[0];
+                } elseif ($parts[0] === '*') {
+                    $default = $parts[1];
+                }
+            }
+        }
+        return $default;
+    }
+
+    /**
+     * 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 (!empty($options)) {
+            throw new \Exception('Unexpected options sent to factory.');
+        }
+        $configManager = $container->get(\VuFind\Config\PluginManager::class);
+        return new $requestedName(
+            $container->get(\VuFindTheme\ThemeInfo::class),
+            $this->getPipelineConfig($configManager->get('config'))
+        );
+    }
+}
-- 
GitLab