From 31b735bba20c6720724a95e06eedbf425235bf40 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Wed, 12 Sep 2012 14:37:27 -0400 Subject: [PATCH] Eliminated \VuFind\Cache\Manager::getInstance() -- now using service manager. --- module/VuFind/config/module.config.php | 1 + module/VuFind/src/VuFind/Cache/Manager.php | 41 ++++++++--------- .../src/VuFind/Config/SearchSpecsReader.php | 40 +++++++++++++++-- .../src/VuFind/Controller/CoverController.php | 32 ++++++++------ .../VuFind/Controller/InstallController.php | 4 +- .../VuFind/Controller/SearchController.php | 4 +- .../VuFind/Controller/SummonController.php | 4 +- .../VuFind/Controller/UpgradeController.php | 7 ++- module/VuFind/src/VuFind/ILS/Driver/Aleph.php | 44 ++++++++++++++++--- .../VuFind/src/VuFind/ILS/Driver/Symphony.php | 13 +++--- .../src/VuFind/Translator/Translator.php | 5 +-- 11 files changed, 132 insertions(+), 63 deletions(-) diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index dcb0154257c..d1805c3fdae 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -262,6 +262,7 @@ $config = array( 'invokables' => array( 'authmanager' => 'VuFind\Auth\Manager', 'cart' => 'VuFind\Cart', + 'cachemanager' => 'VuFind\Cache\Manager', 'recordloader' => 'VuFind\Record\Loader', 'searchspecsreader' => 'VuFind\Config\SearchSpecsReader', 'sessionmanager' => 'Zend\Session\SessionManager', diff --git a/module/VuFind/src/VuFind/Cache/Manager.php b/module/VuFind/src/VuFind/Cache/Manager.php index 39de1030424..b785301a434 100644 --- a/module/VuFind/src/VuFind/Cache/Manager.php +++ b/module/VuFind/src/VuFind/Cache/Manager.php @@ -41,21 +41,32 @@ use VuFind\Config\Reader as ConfigReader, Zend\Cache\StorageFactory; */ class Manager { + /** + * Was there a problem building cache directories? + * + * @var bool + */ protected $directoryCreationError = false; + + /** + * Settings used to generate cache objects. + * + * @var array + */ protected $cacheSettings = array(); + + /** + * Actual cache objects generated from settings. + * + * @var array + */ protected $caches = array(); /** - * Constructor (protected to enforce use of getInstance). + * Constructor */ - protected function __construct() + public function __construct() { - // If we have a parent constructor, call it (none exists at the time of - // this writing, but this is just in case Zend Framework changes later). - if (is_callable($this, 'parent::__construct')) { - parent::__construct(); - } - // Get base cache directory. $cacheBase = $this->getCacheDir(); @@ -161,18 +172,4 @@ class Manager 'plugins' => array('serializer') ); } - - /** - * Get the current instance of the class. - * - * @return Manager - */ - public static function getInstance() - { - static $instance = false; - if (!$instance) { - $instance = new Manager(); - } - return $instance; - } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Config/SearchSpecsReader.php b/module/VuFind/src/VuFind/Config/SearchSpecsReader.php index f9616b0f5a2..f393ee7349a 100644 --- a/module/VuFind/src/VuFind/Config/SearchSpecsReader.php +++ b/module/VuFind/src/VuFind/Config/SearchSpecsReader.php @@ -26,8 +26,8 @@ * @link http://vufind.org Main Site */ namespace VuFind\Config; -use Horde_Yaml as Yaml, - VuFind\Cache\Manager as CacheManager; +use Horde_Yaml as Yaml, Zend\ServiceManager\ServiceLocatorAwareInterface, + Zend\ServiceManager\ServiceLocatorInterface; /** * VuFind SearchSpecs Configuration Reader @@ -38,8 +38,15 @@ use Horde_Yaml as Yaml, * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org Main Site */ -class SearchSpecsReader +class SearchSpecsReader implements ServiceLocatorAwareInterface { + /** + * Service locator + * + * @var ServiceLocatorInterface + */ + protected $serviceLocator; + /** * Cache of loaded search specs. * @@ -59,7 +66,9 @@ class SearchSpecsReader // Load data if it is not already in the object's cache: if (!isset($this->searchSpecs[$filename])) { // Connect to searchspecs cache: - $cache = CacheManager::getInstance()->getCache('searchspecs'); + $sm = $this->getServiceLocator(); + $cache = (is_object($sm) && $sm->has('CacheManager')) + ? $sm->get('CacheManager')->getCache('searchspecs') : false; // Determine full configuration file path: $fullpath = Reader::getBaseConfigPath($filename); @@ -90,4 +99,27 @@ class SearchSpecsReader return $this->searchSpecs[$filename]; } + + /** + * Set the service locator. + * + * @param ServiceLocatorInterface $serviceLocator Locator to register + * + * @return SearchSpecsReader + */ + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + { + $this->serviceLocator = $serviceLocator; + return $this; + } + + /** + * Get the service locator. + * + * @return \Zend\ServiceManager\ServiceLocatorInterface + */ + public function getServiceLocator() + { + return $this->serviceLocator; + } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Controller/CoverController.php b/module/VuFind/src/VuFind/Controller/CoverController.php index 73471ab2896..f0b5b68a743 100644 --- a/module/VuFind/src/VuFind/Controller/CoverController.php +++ b/module/VuFind/src/VuFind/Controller/CoverController.php @@ -26,8 +26,7 @@ * @link http://www.vufind.org Main Page */ namespace VuFind\Controller; -use VuFind\Cache\Manager as CacheManager, VuFind\Config\Reader as ConfigReader, - VuFind\Cover\Loader; +use VuFind\Config\Reader as ConfigReader, VuFind\Cover\Loader; /** * Generates covers for book entries @@ -40,18 +39,23 @@ use VuFind\Cache\Manager as CacheManager, VuFind\Config\Reader as ConfigReader, */ class CoverController extends AbstractBase { - protected $loader; + protected $loader = false; /** - * Constructor + * Get the cover loader object + * + * @return Loader */ - public function __construct() + protected function getLoader() { - // Construct object for loading cover images: - $this->loader = new Loader( - ConfigReader::getConfig(), CacheManager::getInstance()->getCacheDir() - ); - parent::__construct(); + // Construct object for loading cover images if it does not already exist: + if (!$this->loader) { + $this->loader = new Loader( + ConfigReader::getConfig(), + $this->getServiceLocator()->get('CacheManager')->getCacheDir() + ); + } + return $this->loader; } /** @@ -61,7 +65,7 @@ class CoverController extends AbstractBase */ public function showAction() { - $this->loader->loadImage( + $this->getLoader()->loadImage( $this->params()->fromQuery('isn'), $this->params()->fromQuery('size'), $this->params()->fromQuery('contenttype') @@ -76,7 +80,7 @@ class CoverController extends AbstractBase */ public function unavailableAction() { - $this->loader->loadUnavailable(); + $this->getLoader()->loadUnavailable(); return $this->displayImage(); } @@ -91,9 +95,9 @@ class CoverController extends AbstractBase $response = $this->getResponse(); $headers = $response->getHeaders(); $headers->addHeaderLine( - 'Content-type', $this->loader->getContentType() + 'Content-type', $this->getLoader()->getContentType() ); - $response->setContent($this->loader->getImage()); + $response->setContent($this->getLoader()->getImage()); return $response; } } diff --git a/module/VuFind/src/VuFind/Controller/InstallController.php b/module/VuFind/src/VuFind/Controller/InstallController.php index a45e98a9f66..f82b77573f1 100644 --- a/module/VuFind/src/VuFind/Controller/InstallController.php +++ b/module/VuFind/src/VuFind/Controller/InstallController.php @@ -163,7 +163,7 @@ class InstallController extends AbstractBase */ protected function checkCache() { - $cache = \VuFind\Cache\Manager::getInstance(); + $cache = $this->getServiceLocator()->get('CacheManager'); return array( 'title' => 'Cache', 'status' => !$cache->hasDirectoryCreationError(), @@ -178,7 +178,7 @@ class InstallController extends AbstractBase */ public function fixcacheAction() { - $cache = \VuFind\Cache\Manager::getInstance(); + $cache = $this->getServiceLocator()->get('CacheManager'); $view = $this->createViewModel(); $view->cacheDir = $cache->getCacheDir(); if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) { diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php index 5766babee3c..4374a4eb224 100644 --- a/module/VuFind/src/VuFind/Controller/SearchController.php +++ b/module/VuFind/src/VuFind/Controller/SearchController.php @@ -27,7 +27,7 @@ */ namespace VuFind\Controller; -use VuFind\Cache\Manager as CacheManager, VuFind\Config\Reader as ConfigReader, +use VuFind\Config\Reader as ConfigReader, VuFind\Connection\Manager as ConnectionManager, VuFind\Exception\Mail as MailException, VuFind\Mailer, VuFind\Search\Memory, VuFind\Solr\Utils as SolrUtils; @@ -499,7 +499,7 @@ class SearchController extends AbstractSearch protected function getAdvancedFacets() { // Check if we have facet results cached, and build them if we don't. - $cache = CacheManager::getInstance()->getCache('object'); + $cache = $this->getServiceLocator()->get('CacheManager')->getCache('object'); if (!($results = $cache->getItem('solrSearchHomeFacets'))) { // Use advanced facet settings to get summary facets on the front page; // we may want to make this more flexible later. Also keep in mind that diff --git a/module/VuFind/src/VuFind/Controller/SummonController.php b/module/VuFind/src/VuFind/Controller/SummonController.php index d52165428d9..710bbbfadd9 100644 --- a/module/VuFind/src/VuFind/Controller/SummonController.php +++ b/module/VuFind/src/VuFind/Controller/SummonController.php @@ -26,7 +26,7 @@ * @link http://vufind.org Main Site */ namespace VuFind\Controller; -use VuFind\Cache\Manager as CacheManager, Zend\Mvc\MvcEvent; +use Zend\Mvc\MvcEvent; /** * Summon Controller @@ -125,7 +125,7 @@ class SummonController extends AbstractSearch protected function getAdvancedFacets() { // Check if we have facet results cached, and build them if we don't. - $cache = CacheManager::getInstance()->getCache('object'); + $cache = $this->getServiceLocator()->get('CacheManager')->getCache('object'); if (!($results = $cache->getItem('summonSearchHomeFacets'))) { $sm = $this->getSearchManager(); $params = $sm->setSearchClassId('Summon')->getParams(); diff --git a/module/VuFind/src/VuFind/Controller/UpgradeController.php b/module/VuFind/src/VuFind/Controller/UpgradeController.php index 672580226c5..809b4cc1d26 100644 --- a/module/VuFind/src/VuFind/Controller/UpgradeController.php +++ b/module/VuFind/src/VuFind/Controller/UpgradeController.php @@ -26,9 +26,8 @@ * @link http://vufind.org Main Site */ namespace VuFind\Controller; -use ArrayObject, VuFind\Cache\Manager as CacheManager, - VuFind\Config\Reader as ConfigReader, VuFind\Cookie\Container as CookieContainer, - VuFind\Db\AdapterFactory, +use ArrayObject, VuFind\Config\Reader as ConfigReader, + VuFind\Cookie\Container as CookieContainer, VuFind\Db\AdapterFactory, VuFind\Exception\RecordMissing as RecordMissingException, Zend\Db\TableGateway\Feature\GlobalAdapterFeature as DbGlobalAdapter, Zend\Session\Container as SessionContainer; @@ -463,7 +462,7 @@ class UpgradeController extends AbstractBase { // If the cache is messed up, nothing is going to work right -- check that // first: - $cache = CacheManager::getInstance(); + $cache = $this->getServiceLocator()->get('CacheManager'); if ($cache->hasDirectoryCreationError()) { return $this->redirect()->toRoute('install-fixcache'); } diff --git a/module/VuFind/src/VuFind/ILS/Driver/Aleph.php b/module/VuFind/src/VuFind/ILS/Driver/Aleph.php index 0fa76585b60..dc41f09d363 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/Aleph.php +++ b/module/VuFind/src/VuFind/ILS/Driver/Aleph.php @@ -36,8 +36,9 @@ * @link http://vufind.org/wiki/building_an_ils_driver Wiki */ namespace VuFind\ILS\Driver; -use VuFind\Cache\Manager as CacheManager, VuFind\Config\Reader as ConfigReader, - VuFind\Exception\ILS as ILSException; +use VuFind\Config\Reader as ConfigReader, VuFind\Exception\ILS as ILSException, + Zend\ServiceManager\ServiceLocatorAwareInterface, + Zend\ServiceManager\ServiceLocatorInterface; /** * Aleph Translator Class @@ -271,11 +272,18 @@ class AlephRestfulException extends \Exception * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/building_an_ils_driver Wiki */ -class Aleph extends AbstractBase +class Aleph extends AbstractBase implements ServiceLocatorAwareInterface { protected $duedates = false; protected $translator = false; + /** + * Service locator + * + * @var ServiceLocatorInterface + */ + protected $serviceLocator; + /** * Initialize the driver. * @@ -330,8 +338,11 @@ class Aleph extends AbstractBase && isset($this->config['util']['tab15']) && isset($this->config['util']['tab_sub_library']) ) { - if (isset($this->config['Cache']['type'])) { - $manager = CacheManager::getInstance(); + $serviceManager = $this->getServiceLocator()->getServiceLocator(); + if (isset($this->config['Cache']['type']) + && $serviceManager->has('CacheManager') + ) { + $manager = $serviceManager->get('CacheManager'); $cache = $manager->getCache($this->config['Cache']['type']); $this->translator = $cache->getItem('alephTranslator'); } @@ -1632,4 +1643,27 @@ class Aleph extends AbstractBase // TODO return array(); } + + /** + * Set the service locator. + * + * @param ServiceLocatorInterface $serviceLocator Locator to register + * + * @return Aleph + */ + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + { + $this->serviceLocator = $serviceLocator; + return $this; + } + + /** + * Get the service locator. + * + * @return \Zend\ServiceManager\ServiceLocatorInterface + */ + public function getServiceLocator() + { + return $this->serviceLocator; + } } diff --git a/module/VuFind/src/VuFind/ILS/Driver/Symphony.php b/module/VuFind/src/VuFind/ILS/Driver/Symphony.php index 7794f14352d..1aaead7fb6b 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/Symphony.php +++ b/module/VuFind/src/VuFind/ILS/Driver/Symphony.php @@ -27,8 +27,8 @@ * @link http://vufind.org/wiki/building_an_ils_driver Wiki */ namespace VuFind\ILS\Driver; -use SoapClient, SoapFault, VuFind\Cache\Manager as CacheManager, - VuFind\Config\Reader as ConfigReader, VuFind\Exception\ILS as ILSException, +use SoapClient, SoapFault, VuFind\Config\Reader as ConfigReader, + VuFind\Exception\ILS as ILSException, Zend\ServiceManager\ServiceLocatorAwareInterface, Zend\ServiceManager\ServiceLocatorInterface; @@ -113,9 +113,12 @@ class Symphony extends AbstractBase implements ServiceLocatorAwareInterface // Initialize cache manager. if (isset($configArray['PolicyCache']['type'])) { - $manager = CacheManager::getInstance(); - $this->policyCache - = $manager->getCache($configArray['PolicyCache']['type']); + $serviceManager = $this->getServiceLocator()->getServiceLocator(); + if ($serviceManager->has('CacheManager')) { + $manager = $serviceManager->get('CacheManager'); + $this->policyCache + = $manager->getCache($configArray['PolicyCache']['type']); + } } } diff --git a/module/VuFind/src/VuFind/Translator/Translator.php b/module/VuFind/src/VuFind/Translator/Translator.php index 52ca3b5e95d..5bbe710b97a 100644 --- a/module/VuFind/src/VuFind/Translator/Translator.php +++ b/module/VuFind/src/VuFind/Translator/Translator.php @@ -26,8 +26,7 @@ * @link http://www.vufind.org Main Page */ namespace VuFind\Translator; -use VuFind\Cache\Manager as CacheManager, - VuFind\Translator\Loader\ExtendedIni as ExtendedIniLoader, +use VuFind\Translator\Loader\ExtendedIni as ExtendedIniLoader, Zend\I18n\Translator\TranslatorServiceFactory; /** @@ -95,7 +94,7 @@ class Translator // Set up language caching for better performance: $translator - ->setCache(CacheManager::getInstance()->getCache('language')); + ->setCache($serviceManager->get('CacheManager')->getCache('language')); // Store the translator object in the VuFind Translator wrapper: self::setTranslator($translator); -- GitLab