From 1d8c8223a3d529846ca7d1c571fc0695366b9aa5 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Thu, 24 Jan 2019 15:57:47 -0500 Subject: [PATCH] Eliminate static translator factory. --- module/VuFind/config/module.config.php | 2 +- .../I18n/Translator/TranslatorFactory.php | 103 ++++++++++++++++++ module/VuFind/src/VuFind/Service/Factory.php | 54 --------- .../Root/DisplayLanguageOptionFactory.php | 6 +- 4 files changed, 107 insertions(+), 58 deletions(-) create mode 100644 module/VuFind/src/VuFind/I18n/Translator/TranslatorFactory.php diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 6782df486c5..75694cf65b8 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -393,7 +393,7 @@ $config = [ 'VuFindHttp\HttpService' => 'VuFind\Service\Factory::getHttp', 'VuFindSearch\Service' => 'VuFind\Service\Factory::getSearchService', 'Zend\Db\Adapter\Adapter' => 'VuFind\Service\Factory::getDbAdapter', - 'Zend\Mvc\I18n\Translator' => 'VuFind\Service\Factory::getTranslator', + 'Zend\Mvc\I18n\Translator' => 'VuFind\I18n\Translator\TranslatorFactory', 'Zend\Session\SessionManager' => 'VuFind\Session\ManagerFactory', ], 'initializers' => [ diff --git a/module/VuFind/src/VuFind/I18n/Translator/TranslatorFactory.php b/module/VuFind/src/VuFind/I18n/Translator/TranslatorFactory.php new file mode 100644 index 00000000000..64f495a5476 --- /dev/null +++ b/module/VuFind/src/VuFind/I18n/Translator/TranslatorFactory.php @@ -0,0 +1,103 @@ +<?php +/** + * Translator factory. + * + * 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 Translator + * @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 VuFind\I18n\Translator; + +use Interop\Container\ContainerInterface; + +/** + * Translator factory. + * + * @category VuFind + * @package Translator + * @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 TranslatorFactory extends \Zend\Mvc\I18n\TranslatorFactory +{ + /** + * 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 + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function __invoke(ContainerInterface $container, $requestedName, + array $options = null + ) { + $translator = parent::__invoke($container, $requestedName, $options); + + // Set up the ExtendedIni plugin: + $config = $container->get(\VuFind\Config\PluginManager::class) + ->get('config'); + $pathStack = [ + APPLICATION_PATH . '/languages', + LOCAL_OVERRIDE_DIR . '/languages' + ]; + $fallbackLocales = $config->Site->language == 'en' + ? 'en' + : [$config->Site->language, 'en']; + try { + $pm = $translator->getPluginManager(); + } catch (\Zend\Mvc\I18n\Exception\BadMethodCallException $ex) { + // If getPluginManager is missing, this means that the user has + // disabled translation in module.config.php or PHP's intl extension + // is missing. We can do no further configuration of the object. + return $translator; + } + $pm->setService( + 'ExtendedIni', new Loader\ExtendedIni($pathStack, $fallbackLocales) + ); + + // Set up language caching for better performance: + try { + $translator->setCache( + $container->get(\VuFind\Cache\Manager::class)->getCache('language') + ); + } catch (\Exception $e) { + // Don't let a cache failure kill the whole application, but make + // note of it: + $logger = $container->get(\VuFind\Log\Logger::class); + $logger->debug( + 'Problem loading cache: ' . get_class($e) . ' exception: ' + . $e->getMessage() + ); + } + + return $translator; + } +} diff --git a/module/VuFind/src/VuFind/Service/Factory.php b/module/VuFind/src/VuFind/Service/Factory.php index ec700667ada..ea0f5da40c4 100644 --- a/module/VuFind/src/VuFind/Service/Factory.php +++ b/module/VuFind/src/VuFind/Service/Factory.php @@ -111,58 +111,4 @@ class Factory new \Zend\EventManager\EventManager($sm->get('SharedEventManager')) ); } - - /** - * Construct the translator. - * - * @param ServiceManager $sm Service manager. - * - * @return \Zend\Mvc\I18n\Translator - */ - public static function getTranslator(ServiceManager $sm) - { - $factory = new \Zend\Mvc\I18n\TranslatorFactory(); - $translator = $factory->createService($sm); - - // Set up the ExtendedIni plugin: - $config = $sm->get('VuFind\Config\PluginManager')->get('config'); - $pathStack = [ - APPLICATION_PATH . '/languages', - LOCAL_OVERRIDE_DIR . '/languages' - ]; - $fallbackLocales = $config->Site->language == 'en' - ? 'en' - : [$config->Site->language, 'en']; - try { - $pm = $translator->getPluginManager(); - } catch (\Zend\Mvc\I18n\Exception\BadMethodCallException $ex) { - // If getPluginManager is missing, this means that the user has - // disabled translation in module.config.php or PHP's intl extension - // is missing. We can do no further configuration of the object. - return $translator; - } - $pm->setService( - 'ExtendedIni', - new \VuFind\I18n\Translator\Loader\ExtendedIni( - $pathStack, $fallbackLocales - ) - ); - - // Set up language caching for better performance: - try { - $translator->setCache( - $sm->get('VuFind\Cache\Manager')->getCache('language') - ); - } catch (\Exception $e) { - // Don't let a cache failure kill the whole application, but make - // note of it: - $logger = $sm->get('VuFind\Log\Logger'); - $logger->debug( - 'Problem loading cache: ' . get_class($e) . ' exception: ' - . $e->getMessage() - ); - } - - return $translator; - } } diff --git a/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOptionFactory.php b/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOptionFactory.php index c5b0380e94b..28506916316 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOptionFactory.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOptionFactory.php @@ -28,6 +28,7 @@ namespace VuFind\View\Helper\Root; use Interop\Container\ContainerInterface; +use Zend\Mvc\I18n\Translator; use Zend\ServiceManager\Factory\FactoryInterface; /** @@ -64,8 +65,7 @@ class DisplayLanguageOptionFactory implements FactoryInterface // We want to construct a separate translator instance for this helper, // since it configures different language/locale than the core shared // instance! - return new $requestedName( - \VuFind\Service\Factory::getTranslator($container) - ); + $factory = new \VuFind\I18n\Translator\TranslatorFactory(); + return new $requestedName($factory($container, Translator::class)); } } -- GitLab