diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index bd952397f58056bf20fc15ce6a31a5d2f9e2e905..dcb0154257c834d64527277432efd0eac3e60c8b 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -263,6 +263,7 @@ $config = array( 'authmanager' => 'VuFind\Auth\Manager', 'cart' => 'VuFind\Cart', 'recordloader' => 'VuFind\Record\Loader', + 'searchspecsreader' => 'VuFind\Config\SearchSpecsReader', 'sessionmanager' => 'Zend\Session\SessionManager', ) ), diff --git a/module/VuFind/src/VuFind/Config/Reader.php b/module/VuFind/src/VuFind/Config/Reader.php index 9b2427b454ff8192f7a8022e4956f23939ccdb85..55ef9faef6a935d54522afaaf5bb42cc14311a0c 100644 --- a/module/VuFind/src/VuFind/Config/Reader.php +++ b/module/VuFind/src/VuFind/Config/Reader.php @@ -26,10 +26,7 @@ * @link http://vufind.org Main Site */ namespace VuFind\Config; -use Horde_Yaml as Yaml, - VuFind\Cache\Manager as CacheManager, - Zend\Config\Config, - Zend\Config\Reader\Ini as IniReader; +use Zend\Config\Config, Zend\Config\Reader\Ini as IniReader; /** * Class to digest VuFind configuration settings @@ -43,7 +40,6 @@ use Horde_Yaml as Yaml, class Reader { protected static $configs = array(); - protected static $searchSpecs = array(); /** * Load the proper config file @@ -202,50 +198,6 @@ class Reader return $config; } - /** - * Return search specs - * - * @param string $filename config file name - * - * @return array - */ - public static function getSearchSpecs($filename) - { - // Load data if it is not already in the object's static cache: - if (!isset(self::$searchSpecs[$filename])) { - // Connect to searchspecs cache: - $cache = CacheManager::getInstance()->getCache('searchspecs'); - - // Determine full configuration file path: - $fullpath = self::getBaseConfigPath($filename); - $local = self::getLocalConfigPath($filename); - - // Generate cache key: - $key = $filename . '-' . filemtime($fullpath); - if (!empty($local)) { - $key .= '-local-' . filemtime($local); - } - $key = md5($key); - - // Generate data if not found in cache: - if (!$cache || !($results = $cache->getItem($key))) { - $results = Yaml::load(file_get_contents($fullpath)); - if (!empty($local)) { - $localResults = Yaml::load(file_get_contents($local)); - foreach ($localResults as $key => $value) { - $results[$key] = $value; - } - } - if ($cache) { - $cache->setItem($key, $results); - } - } - self::$searchSpecs[$filename] = $results; - } - - return self::$searchSpecs[$filename]; - } - /** * readIniComments * diff --git a/module/VuFind/src/VuFind/Config/SearchSpecsReader.php b/module/VuFind/src/VuFind/Config/SearchSpecsReader.php new file mode 100644 index 0000000000000000000000000000000000000000..f9616b0f5a2469fac5aba258b8ca4551989812d2 --- /dev/null +++ b/module/VuFind/src/VuFind/Config/SearchSpecsReader.php @@ -0,0 +1,93 @@ +<?php +/** + * VuFind SearchSpecs Configuration Reader + * + * 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 Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ +namespace VuFind\Config; +use Horde_Yaml as Yaml, + VuFind\Cache\Manager as CacheManager; + +/** + * VuFind SearchSpecs Configuration Reader + * + * @category VuFind2 + * @package Config + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ +class SearchSpecsReader +{ + /** + * Cache of loaded search specs. + * + * @var array + */ + protected $searchSpecs = array(); + + /** + * Return search specs + * + * @param string $filename config file name + * + * @return array + */ + public function get($filename) + { + // 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'); + + // Determine full configuration file path: + $fullpath = Reader::getBaseConfigPath($filename); + $local = Reader::getLocalConfigPath($filename); + + // Generate cache key: + $key = $filename . '-' . filemtime($fullpath); + if (!empty($local)) { + $key .= '-local-' . filemtime($local); + } + $key = md5($key); + + // Generate data if not found in cache: + if (!$cache || !($results = $cache->getItem($key))) { + $results = Yaml::load(file_get_contents($fullpath)); + if (!empty($local)) { + $localResults = Yaml::load(file_get_contents($local)); + foreach ($localResults as $key => $value) { + $results[$key] = $value; + } + } + if ($cache) { + $cache->setItem($key, $results); + } + } + $this->searchSpecs[$filename] = $results; + } + + return $this->searchSpecs[$filename]; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Connection/Solr.php b/module/VuFind/src/VuFind/Connection/Solr.php index 4f2580bc829e1f4115fb4cae4764dcfdccfe952c..326c91716edf7f52b7c0f381de429f0251c20331 100644 --- a/module/VuFind/src/VuFind/Connection/Solr.php +++ b/module/VuFind/src/VuFind/Connection/Solr.php @@ -186,8 +186,8 @@ class Solr implements ServiceLocatorAwareInterface { // Only load specs once: if ($this->searchSpecs === false) { - $this->searchSpecs - = ConfigReader::getSearchSpecs($this->searchSpecsFile); + $this->searchSpecs = $this->getServiceLocator()->get('SearchSpecsReader') + ->get($this->searchSpecsFile); } // Special case -- null $handler means we want all search specs. diff --git a/module/VuFind/src/VuFind/Tests/TestCase.php b/module/VuFind/src/VuFind/Tests/TestCase.php index 2405631f12f70b3e6c05cbb8e94ecd798fdee330..bda0284a754e43c1efc5e327303d57508561bec5 100644 --- a/module/VuFind/src/VuFind/Tests/TestCase.php +++ b/module/VuFind/src/VuFind/Tests/TestCase.php @@ -122,6 +122,9 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase $this->serviceManager->setService( 'RecordDriverPluginManager', $recordDriverFactory ); + $this->serviceManager->setService( + 'SearchSpecsReader', new \VuFind\Config\SearchSpecsReader() + ); \VuFind\Connection\Manager::setServiceLocator($this->serviceManager); } return $this->serviceManager; diff --git a/module/VuFind/tests/Config/ReaderTest.php b/module/VuFind/tests/Config/ReaderTest.php index 1f96542ef81c37c4977a1bbf73f92862baeb0d63..f0747b6949d94c11b34e338f0070c1bbde5ef645 100644 --- a/module/VuFind/tests/Config/ReaderTest.php +++ b/module/VuFind/tests/Config/ReaderTest.php @@ -64,20 +64,4 @@ class ReaderTest extends \VuFind\Tests\TestCase $config = Reader::getConfig('sms'); $this->assertTrue(isset($config->Carriers) && count($config->Carriers) > 0); } - - /** - * Test loading of a YAML file. - * - * @return void - */ - public function testSearchSpecsRead() - { - // The searchspecs.yaml file should define author dismax fields (among many - // other things). - $specs = Reader::getSearchSpecs('searchspecs.yaml'); - $this->assertTrue( - isset($specs['Author']['DismaxFields']) - && !empty($specs['Author']['DismaxFields']) - ); - } } \ No newline at end of file diff --git a/module/VuFind/tests/Config/SearchSpecsReaderTest.php b/module/VuFind/tests/Config/SearchSpecsReaderTest.php new file mode 100644 index 0000000000000000000000000000000000000000..42c2d1466990bf500b75a1e3319c203e969191ad --- /dev/null +++ b/module/VuFind/tests/Config/SearchSpecsReaderTest.php @@ -0,0 +1,59 @@ +<?php +/** + * Config SearchSpecsReader Test Class + * + * 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 Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/unit_tests Wiki + */ +namespace VuFind\Tests\Config; +use VuFind\Config\SearchSpecsReader; + +/** + * Config SearchSpecsReader Test Class + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @author Chris Hallberg <challber@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/unit_tests Wiki + */ +class SearchSpecsReaderTest extends \VuFind\Tests\TestCase +{ + /** + * Test loading of a YAML file. + * + * @return void + */ + public function testSearchSpecsRead() + { + // The searchspecs.yaml file should define author dismax fields (among many + // other things). + $reader = $this->getServiceManager()->get('SearchSpecsReader'); + $specs = $reader->get('searchspecs.yaml'); + $this->assertTrue( + isset($specs['Author']['DismaxFields']) + && !empty($specs['Author']['DismaxFields']) + ); + } +} \ No newline at end of file