diff --git a/module/VuFind/src/VuFind/Config/Reader/CacheDecorator.php b/module/VuFind/src/VuFind/Config/Reader/CacheDecorator.php new file mode 100644 index 0000000000000000000000000000000000000000..0e487e65628ddc1203b764638ec1f4c110603c95 --- /dev/null +++ b/module/VuFind/src/VuFind/Config/Reader/CacheDecorator.php @@ -0,0 +1,125 @@ +<?php + +/** + * Cache decorator. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Config + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ + +namespace VuFind\Config\Reader; + +use Zend\Config\Reader\ReaderInterface; +use Zend\Cache\Storage\StorageInterface; + +/** + * This class decorates a configuration file reader with caching support. + * + * @category VuFind2 + * @package Config + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ +class CacheDecorator implements ReaderInterface +{ + + /** + * The decorated reader. + * + * @var ReaderInterface + */ + protected $reader; + + /** + * Cache storage. + * + * @var StorageInterface + */ + protected $storage; + + /** + * Constructor. + * + * @param ReaderInterface $reader Config reader + * @param StorageInterface $storage Cache storage + * + * @return void + */ + public function __construct (ReaderInterface $reader, StorageInterface $storage) + { + $this->reader = $reader; + $this->storage = $storage; + } + + /** + * Read from a file and create an array + * + * @param string $filename Filename + * + * @return array + */ + public function fromFile ($filename) + { + $key = $this->generateKey($filename); + if ($this->storage->hasItem($key)) { + return $this->storage->getItem($key); + } + $config = $this->reader->fromFile($filename); + $this->storage->setItem($key, $config); + return $config; + } + + /** + * Read from a string and create an array + * + * @param string $string String + * + * @return array|boolean + */ + public function fromString ($string) + { + $key = $this->generateKey($string); + if ($this->storage->hasItem($key)) { + return $this->storage->getItem($key); + } + $config = $this->reader->fromString($string); + $this->storage->setItem($key, $config); + return $config; + } + + /// Internal API + + /** + * Return a cache key. + * + * @param string $string String + * + * @return string + */ + protected function generateKey ($string) + { + return md5($string); + } + +} \ No newline at end of file diff --git a/module/VuFind/tests/unit-tests/src/Config/Reader/CacheDecoratorTest.php b/module/VuFind/tests/unit-tests/src/Config/Reader/CacheDecoratorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..70a5a7f21bfa2db7bfe8ac894eb6e5c61d22ade5 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/Config/Reader/CacheDecoratorTest.php @@ -0,0 +1,87 @@ +<?php + +/** + * Config CacheDecorator test class file. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Tests + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/unit_tests Wiki + */ + +namespace VuFindTest\Config\Reader; + +use VuFind\Config\Reader\CacheDecorator; + +/** + * Config CacheDecorator test class. + * + * @category VuFind2 + * @package Tests + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/unit_tests Wiki + */ +class CacheDecoratorTest extends \PHPUnit_Framework_TestCase +{ + + /** + * Read config from while, new file. + * + * @return void + */ + public function testFromFile () + { + $cache = $this->getMockForAbstractClass('Zend\Cache\Storage\StorageInterface', array('setItem', 'hasItem')); + $cache->expects($this->once()) + ->method('setItem'); + $cache->expects($this->once()) + ->method('hasItem') + ->will($this->returnValue(false)); + $reader = $this->getMockForAbstractClass('Zend\Config\Reader\ReaderInterface', array('fromFile')); + $reader->expects($this->once()) + ->method('fromFile') + ->will($this->returnValue(array())); + $deco = new CacheDecorator($reader, $cache); + $deco->fromFile('ignore'); + } + + /** + * Read config from while, cached file. + * + * @return void + */ + public function testFromFileCached () + { + $cache = $this->getMockForAbstractClass('Zend\Cache\Storage\StorageInterface', array('setItem', 'hasItem', 'getItem')); + $cache->expects($this->never()) + ->method('setItem'); + $cache->expects($this->once()) + ->method('hasItem') + ->will($this->returnValue(true)); + $cache->expects($this->once()) + ->method('getItem') + ->will($this->returnValue(array())); + $reader = $this->getMockForAbstractClass('Zend\Config\Reader\ReaderInterface', array('fromFile')); + $deco = new CacheDecorator($reader, $cache); + $deco->fromFile('ignore'); + } +} \ No newline at end of file