From f347a1c6a52937a5e601065d0df256bc834fcaf0 Mon Sep 17 00:00:00 2001 From: mtrojan-ub <mario.trojan@uni-tuebingen.de> Date: Mon, 23 Oct 2017 16:33:51 +0200 Subject: [PATCH] Improved inheritance for generic controller factory (using static namespace) (#1055) --- .../VuFind/src/VuFind/Controller/Factory.php | 39 +-------- .../src/VuFind/Controller/GenericFactory.php | 84 +++++++++++++++++++ .../src/VuFindAdmin/Controller/Factory.php | 38 +-------- .../src/VuFindConsole/Controller/Factory.php | 38 +-------- .../src/VuFindDevTools/Controller/Factory.php | 38 +-------- 5 files changed, 88 insertions(+), 149 deletions(-) create mode 100644 module/VuFind/src/VuFind/Controller/GenericFactory.php diff --git a/module/VuFind/src/VuFind/Controller/Factory.php b/module/VuFind/src/VuFind/Controller/Factory.php index a6330d48f05..f15bb99b039 100644 --- a/module/VuFind/src/VuFind/Controller/Factory.php +++ b/module/VuFind/src/VuFind/Controller/Factory.php @@ -40,45 +40,8 @@ use Zend\ServiceManager\ServiceManager; * * @codeCoverageIgnore */ -class Factory +class Factory extends GenericFactory { - /** - * Construct a generic controller. - * - * @param string $name Name of table to construct (fully qualified - * class name, or else a class name within the current namespace) - * @param ServiceManager $sm Service manager - * - * @return object - */ - public static function getGenericController($name, ServiceManager $sm) - { - // Prepend the current namespace unless we receive a FQCN: - $class = (strpos($name, '\\') === false) - ? __NAMESPACE__ . '\\' . $name : $name; - if (!class_exists($class)) { - throw new \Exception('Cannot construct ' . $class); - } - return new $class($sm->getServiceLocator()); - } - - /** - * Construct a generic controller. - * - * @param string $name Method name being called - * @param array $args Method arguments - * - * @return object - */ - public static function __callStatic($name, $args) - { - // Strip "get" from method name to get name of class; pass first argument - // on assumption that it should be the ServiceManager object. - return static::getGenericController( - substr($name, 3), isset($args[0]) ? $args[0] : null - ); - } - /** * Construct the BrowseController. * diff --git a/module/VuFind/src/VuFind/Controller/GenericFactory.php b/module/VuFind/src/VuFind/Controller/GenericFactory.php new file mode 100644 index 00000000000..1a3141d5178 --- /dev/null +++ b/module/VuFind/src/VuFind/Controller/GenericFactory.php @@ -0,0 +1,84 @@ +<?php +/** + * Generic factory for controllers (contains generic default behavior shared + * across VuFind's modules). + * + * PHP version 5 + * + * Copyright (C) Villanova University 2017. + * + * 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 Controller + * @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\Controller; + +use Zend\ServiceManager\ServiceManager; + +/** + * Generic factory for controllers (contains generic default behavior shared + * across VuFind's modules). + * + * @category VuFind + * @package Controller + * @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 GenericFactory +{ + /** + * Construct a generic controller. + * + * @param string $name Name of table to construct (fully qualified + * class name, or else a class name within the current namespace) + * @param ServiceManager $sm Service manager + * + * @return object + */ + public static function getGenericController($name, ServiceManager $sm) + { + // Prepend the current (inherited) namespace unless we receive a FQCN: + $class = (strpos($name, '\\') === false) + ? substr(static::class, 0, strrpos(static::class, '\\') + 1) . $name + : $name; + if (!class_exists($class)) { + throw new \Exception('Cannot construct ' . $class); + } + return new $class($sm->getServiceLocator()); + } + + /** + * Construct a generic controller. + * + * @param string $name Method name being called + * @param array $args Method arguments + * + * @return object + */ + public static function __callStatic($name, $args) + { + // Strip "get" from method name to get name of class; pass first argument + // on assumption that it should be the ServiceManager object. + return static::getGenericController( + substr($name, 3), isset($args[0]) ? $args[0] : null + ); + } +} diff --git a/module/VuFindAdmin/src/VuFindAdmin/Controller/Factory.php b/module/VuFindAdmin/src/VuFindAdmin/Controller/Factory.php index ff3b51a0df8..2edd4d44783 100644 --- a/module/VuFindAdmin/src/VuFindAdmin/Controller/Factory.php +++ b/module/VuFindAdmin/src/VuFindAdmin/Controller/Factory.php @@ -40,42 +40,6 @@ use Zend\ServiceManager\ServiceManager; * * @codeCoverageIgnore */ -class Factory +class Factory extends \VuFind\Controller\GenericFactory { - /** - * Construct a generic controller. - * - * @param string $name Name of table to construct (fully qualified - * class name, or else a class name within the current namespace) - * @param ServiceManager $sm Service manager - * - * @return object - */ - public static function getGenericController($name, ServiceManager $sm) - { - // Prepend the current namespace unless we receive a FQCN: - $class = (strpos($name, '\\') === false) - ? __NAMESPACE__ . '\\' . $name : $name; - if (!class_exists($class)) { - throw new \Exception('Cannot construct ' . $class); - } - return new $class($sm->getServiceLocator()); - } - - /** - * Construct a generic controller. - * - * @param string $name Method name being called - * @param array $args Method arguments - * - * @return object - */ - public static function __callStatic($name, $args) - { - // Strip "get" from method name to get name of class; pass first argument - // on assumption that it should be the ServiceManager object. - return static::getGenericController( - substr($name, 3), isset($args[0]) ? $args[0] : null - ); - } } diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/Factory.php b/module/VuFindConsole/src/VuFindConsole/Controller/Factory.php index a17a9121e79..50a748673be 100644 --- a/module/VuFindConsole/src/VuFindConsole/Controller/Factory.php +++ b/module/VuFindConsole/src/VuFindConsole/Controller/Factory.php @@ -40,42 +40,6 @@ use Zend\ServiceManager\ServiceManager; * * @codeCoverageIgnore */ -class Factory +class Factory extends \VuFind\Controller\GenericFactory { - /** - * Construct a generic controller. - * - * @param string $name Name of table to construct (fully qualified - * class name, or else a class name within the current namespace) - * @param ServiceManager $sm Service manager - * - * @return object - */ - public static function getGenericController($name, ServiceManager $sm) - { - // Prepend the current namespace unless we receive a FQCN: - $class = (strpos($name, '\\') === false) - ? __NAMESPACE__ . '\\' . $name : $name; - if (!class_exists($class)) { - throw new \Exception('Cannot construct ' . $class); - } - return new $class($sm->getServiceLocator()); - } - - /** - * Construct a generic controller. - * - * @param string $name Method name being called - * @param array $args Method arguments - * - * @return object - */ - public static function __callStatic($name, $args) - { - // Strip "get" from method name to get name of class; pass first argument - // on assumption that it should be the ServiceManager object. - return static::getGenericController( - substr($name, 3), isset($args[0]) ? $args[0] : null - ); - } } diff --git a/module/VuFindDevTools/src/VuFindDevTools/Controller/Factory.php b/module/VuFindDevTools/src/VuFindDevTools/Controller/Factory.php index 433d8243cd4..bea3d5e6235 100644 --- a/module/VuFindDevTools/src/VuFindDevTools/Controller/Factory.php +++ b/module/VuFindDevTools/src/VuFindDevTools/Controller/Factory.php @@ -40,42 +40,6 @@ use Zend\ServiceManager\ServiceManager; * * @codeCoverageIgnore */ -class Factory +class Factory extends \VuFind\Controller\GenericFactory { - /** - * Construct a generic controller. - * - * @param string $name Name of table to construct (fully qualified - * class name, or else a class name within the current namespace) - * @param ServiceManager $sm Service manager - * - * @return object - */ - public static function getGenericController($name, ServiceManager $sm) - { - // Prepend the current namespace unless we receive a FQCN: - $class = (strpos($name, '\\') === false) - ? __NAMESPACE__ . '\\' . $name : $name; - if (!class_exists($class)) { - throw new \Exception('Cannot construct ' . $class); - } - return new $class($sm->getServiceLocator()); - } - - /** - * Construct a generic controller. - * - * @param string $name Method name being called - * @param array $args Method arguments - * - * @return object - */ - public static function __callStatic($name, $args) - { - // Strip "get" from method name to get name of class; pass first argument - // on assumption that it should be the ServiceManager object. - return static::getGenericController( - substr($name, 3), isset($args[0]) ? $args[0] : null - ); - } } -- GitLab