From 6dd515671c555afaa6e12d0e232f517e9479af6c Mon Sep 17 00:00:00 2001 From: Ere Maijala <ere.maijala@helsinki.fi> Date: Tue, 8 Mar 2016 17:51:57 +0200 Subject: [PATCH] Fixed cache member variable name conflict with base class in MultiBackend driver and simplified the driver instantiation. --- .../src/VuFind/ILS/Driver/MultiBackend.php | 99 +++++-------------- 1 file changed, 24 insertions(+), 75 deletions(-) diff --git a/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php b/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php index 7dfaaebde4c..bd13f223f5e 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php +++ b/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php @@ -4,7 +4,7 @@ * * PHP version 5 * - * Copyright (C) The National Library of Finland 2012. + * Copyright (C) The National Library of Finland 2012-2016. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -67,19 +67,11 @@ class MultiBackend extends AbstractBase protected $defaultDriver; /** - * The array of cached pre-instantiated drivers + * The array of cached drivers * * @var object[] */ - protected $cache = []; - - /** - * The array of booleans letting us know if a - * driver in the cache has been initialized. - * - * @var boolean[] - */ - protected $isInitialized = []; + protected $driverCache = []; /** * The array of driver configuration options. @@ -1312,83 +1304,40 @@ class MultiBackend extends AbstractBase } } - if (!isset($this->isInitialized[$source]) - || !$this->isInitialized[$source] - ) { - $driverInst = null; - - // And we don't have a copy in our cache... - if (!isset($this->cache[$source])) { - // Get an uninitialized copy - $driverInst = $this->getUninitializedDriver($source); - } else { - // Otherwise, use the uninitialized cached copy - $driverInst = $this->cache[$source]; - } - - // If we have a driver, initialize it. That version has already - // been cached. - if ($driverInst) { - $this->initializeDriver($driverInst, $source); - } else { + // Check for a cached driver + if (!array_key_exists($source, $this->driverCache)) { + // Create the driver + $this->driverCache[$source] = $this->createDriver($source); + if (null === $this->driverCache[$source]) { $this->debug("Could not initialize driver for source '$source'"); return null; } } - return $this->cache[$source]; + return $this->driverCache[$source]; } /** - * Find the correct driver for the correct configuration file - * for the given source. For performance reasons, we do not - * want to initialize the driver yet if it hasn't been already. + * Create a driver for the given source. * - * @param string $source the source title for the driver. + * @param string $source Source id for the driver. * - * @return mixed On success an uninitialized driver object, otherwise null. + * @return mixed On success a driver object, otherwise null. */ - protected function getUninitializedDriver($source) + protected function createDriver($source) { - // We don't really care if it's initialized here. If it is, then there's - // still no added overhead of returning an initialized driver. - if (isset($this->cache[$source])) { - return $this->cache[$source]; - } - - if (isset($this->drivers[$source])) { - $driver = $this->drivers[$source]; - $config = $this->getDriverConfig($source); - if (!$config) { - $this->error("No configuration found for source '$source'"); - return null; - } - $driverInst = clone($this->getServiceLocator()->get($driver)); - $driverInst->setConfig($config); - $this->cache[$source] = $driverInst; - $this->isInitialized[$source] = false; - return $driverInst; + if (!isset($this->drivers[$source])) { + return null; } - - return null; - } - - /** - * Initialize an uninitialized driver. - * - * @param object $driver The driver object to be initialized - * @param string $source The source related to the driver for caching purposes. - * - * @return void - */ - protected function initializeDriver($driver, $source) - { - if (!isset($this->isInitialized[$source]) - || !$this->isInitialized[$source] - ) { - $driver->init(); - $this->isInitialized[$source] = true; - $this->cache[$source] = $driver; + $driver = $this->drivers[$source]; + $config = $this->getDriverConfig($source); + if (!$config) { + $this->error("No configuration found for source '$source'"); + return null; } + $driverInst = clone($this->getServiceLocator()->get($driver)); + $driverInst->setConfig($config); + $driverInst->init(); + return $driverInst; } /** -- GitLab