From 69f523839dd4cc4d3c07c84445c9d3b816d23ba4 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 7 Sep 2012 13:26:18 -0400 Subject: [PATCH] Set up plugin manager for record drivers; made VuFind\Search\Solr\Results::getRecords work with search manager. This revision temporarily breaks related record modules -- they will be fixed in the next commit. --- module/VuFind/config/module.config.php | 13 +++++ module/VuFind/src/VuFind/Bootstrap.php | 3 +- .../Controller/MyResearchController.php | 3 +- module/VuFind/src/VuFind/Record/Loader.php | 4 +- .../src/VuFind/RecordDriver/PluginFactory.php | 48 +++++++++++++++++ .../src/VuFind/RecordDriver/PluginManager.php | 51 +++++++++++++++++++ .../VuFind/src/VuFind/Search/Solr/Results.php | 28 ++++------ .../src/VuFind/Search/SolrAuth/Results.php | 3 +- .../VuFind/Search/SolrReserves/Results.php | 3 +- .../src/VuFind/Search/Summon/Results.php | 3 +- .../src/VuFind/Search/WorldCat/Results.php | 3 +- 11 files changed, 136 insertions(+), 26 deletions(-) create mode 100644 module/VuFind/src/VuFind/RecordDriver/PluginFactory.php create mode 100644 module/VuFind/src/VuFind/RecordDriver/PluginManager.php diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 89e6be48e0a..86353a6fc17 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -187,6 +187,19 @@ $config = array( 'worldcatterms' => 'VuFind\Recommend\WorldCatTerms', ), ), + 'recorddriver_plugin_manager' => array( + 'abstract_factories' => array('VuFind\RecordDriver\PluginFactory'), + 'invokables' => array( + 'missing' => 'VuFind\RecordDriver\Missing', + 'solrauth' => 'VuFind\RecordDriver\SolrAuth', + 'solrdefault' => 'VuFind\RecordDriver\SolrDefault', + 'solrmarc' => 'VuFind\RecordDriver\SolrMarc', + 'solrreserves' => 'VuFind\RecordDriver\SolrReserves', + 'solrvudl' => 'VuFind\RecordDriver\SolrVudl', + 'summon' => 'VuFind\RecordDriver\Summon', + 'worldcat' => 'VuFind\RecordDriver\WorldCat', + ), + ), 'search_manager' => array( 'default_namespace' => 'VuFind\Search', 'namespaces_by_id' => array( diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php index a913c73d2a5..fa72423dbe5 100644 --- a/module/VuFind/src/VuFind/Bootstrap.php +++ b/module/VuFind/src/VuFind/Bootstrap.php @@ -91,7 +91,8 @@ class Bootstrap // Use naming conventions to set up a bunch of services based on namespace: $namespaces = array( - 'Auth', 'Autocomplete', 'ILS\Driver', 'Recommend', 'Session' + 'Auth', 'Autocomplete', 'ILS\Driver', 'Recommend', 'RecordDriver', + 'Session' ); foreach ($namespaces as $ns) { $serviceName = str_replace('\\', '', $ns) . 'PluginManager'; diff --git a/module/VuFind/src/VuFind/Controller/MyResearchController.php b/module/VuFind/src/VuFind/Controller/MyResearchController.php index ff7779acd4e..538d8a37948 100644 --- a/module/VuFind/src/VuFind/Controller/MyResearchController.php +++ b/module/VuFind/src/VuFind/Controller/MyResearchController.php @@ -732,7 +732,8 @@ class MyResearchController extends AbstractBase $record = $this->getSearchManager()->setSearchClassId('Solr') ->getResults()->getRecord($current['id']); } catch (RecordMissingException $e) { - $record = new \VuFind\RecordDriver\Missing(); + $factory = $this->getServiceLocator()->get('RecordDriverPluginManager'); + $record = clone($factory->get('Missing')); $record->setRawData( array('id' => isset($current['id']) ? $current['id'] : null) ); diff --git a/module/VuFind/src/VuFind/Record/Loader.php b/module/VuFind/src/VuFind/Record/Loader.php index 2f31d6072b7..d945607bb3b 100644 --- a/module/VuFind/src/VuFind/Record/Loader.php +++ b/module/VuFind/src/VuFind/Record/Loader.php @@ -140,7 +140,9 @@ class Loader implements ServiceLocatorAwareInterface $fields = isset($details['extra_fields']) ? $details['extra_fields'] : array(); $fields['id'] = $details['id']; - $retVal[$i] = new \VuFind\RecordDriver\Missing(); + $factory = $this->getServiceLocator() + ->get('RecordDriverPluginManager'); + $retVal[$i] = clone($factory->get('Missing')); $retVal[$i]->setRawData($fields); } } diff --git a/module/VuFind/src/VuFind/RecordDriver/PluginFactory.php b/module/VuFind/src/VuFind/RecordDriver/PluginFactory.php new file mode 100644 index 00000000000..a53f12045bf --- /dev/null +++ b/module/VuFind/src/VuFind/RecordDriver/PluginFactory.php @@ -0,0 +1,48 @@ +<?php +/** + * Record driver plugin factory + * + * 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 RecordDrivers + * @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/creating_a_session_handler Wiki + */ +namespace VuFind\RecordDriver; + +/** + * Record driver plugin factory + * + * @category VuFind2 + * @package RecordDrivers + * @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/creating_a_session_handler Wiki + */ +class PluginFactory extends \VuFind\ServiceManager\AbstractPluginFactory +{ + /** + * Constructor + */ + public function __construct() + { + $this->defaultNamespace = 'VuFind\RecordDriver'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/RecordDriver/PluginManager.php b/module/VuFind/src/VuFind/RecordDriver/PluginManager.php new file mode 100644 index 00000000000..6c61443e1c0 --- /dev/null +++ b/module/VuFind/src/VuFind/RecordDriver/PluginManager.php @@ -0,0 +1,51 @@ +<?php +/** + * Record driver plugin manager + * + * 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 RecordDrivers + * @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/creating_a_session_handler Wiki + */ +namespace VuFind\RecordDriver; + +/** + * Record driver plugin manager + * + * @category VuFind2 + * @package RecordDrivers + * @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/creating_a_session_handler Wiki + */ +class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager +{ + /** + * Return the name of the base class or interface that plug-ins must conform + * to. + * + * @return string + */ + protected function getExpectedInterface() + { + return 'VuFind\RecordDriver\AbstractBase'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Search/Solr/Results.php b/module/VuFind/src/VuFind/Search/Solr/Results.php index 140275657b1..21d060797f1 100644 --- a/module/VuFind/src/VuFind/Search/Solr/Results.php +++ b/module/VuFind/src/VuFind/Search/Solr/Results.php @@ -484,7 +484,8 @@ class Results extends BaseResults { // Figure out how many records to retrieve at the same time -- // we'll use either 100 or the ID request limit, whichever is smaller. - $params = new Params(); + $sm = $this->getServiceLocator()->get('SearchManager'); + $params = $sm->setSearchClassId('Solr')->getParams(); $pageSize = $params->getQueryIDLimit(); if ($pageSize < 1 || $pageSize > 100) { $pageSize = 100; @@ -496,7 +497,7 @@ class Results extends BaseResults $currentPage = array_splice($ids, 0, $pageSize, array()); $params->setQueryIDs($currentPage); $params->setLimit($pageSize); - $results = new Results($params); + $results = $sm->setSearchClassId('Solr')->getResults($params); $retVal = array_merge($retVal, $results->getResults()); } @@ -536,26 +537,15 @@ class Results extends BaseResults */ protected function initRecordDriver($data) { - // Remember bad classes to prevent unnecessary file accesses. - static $badClasses = array(); + $factory = $this->getServiceLocator()->get('RecordDriverPluginManager'); - // Determine driver path based on record type: - $driver = 'VuFind\RecordDriver\Solr' . ucwords($data['recordtype']); - - // If we can't load the driver, fall back to the default, index-based one: - if (isset($badClasses[$driver]) || !@class_exists($driver)) { - $badClasses[$driver] = 1; - $driver = 'VuFind\RecordDriver\SolrDefault'; - } + $key = 'Solr' . ucwords($data['recordtype']); + $recordType = $factory->has($key) ? $key : 'SolrDefault'; // Build the object: - if (class_exists($driver)) { - $obj = new $driver(); - $obj->setRawData($data); - return $obj; - } - - throw new \Exception('Cannot find record driver -- ' . $driver); + $driver = clone($factory->get($recordType)); + $driver->setRawData($data); + return $driver; } /** diff --git a/module/VuFind/src/VuFind/Search/SolrAuth/Results.php b/module/VuFind/src/VuFind/Search/SolrAuth/Results.php index 9a3c5bad9af..37f4554d914 100644 --- a/module/VuFind/src/VuFind/Search/SolrAuth/Results.php +++ b/module/VuFind/src/VuFind/Search/SolrAuth/Results.php @@ -76,7 +76,8 @@ class Results extends SolrResults */ protected function initRecordDriver($data) { - $driver = new \VuFind\RecordDriver\SolrAuth(); + $factory = $this->getServiceLocator()->get('RecordDriverPluginManager'); + $driver = clone($factory->get('SolrAuth')); $driver->setRawData($data); return $driver; } diff --git a/module/VuFind/src/VuFind/Search/SolrReserves/Results.php b/module/VuFind/src/VuFind/Search/SolrReserves/Results.php index b84b487999b..104bcef35dd 100644 --- a/module/VuFind/src/VuFind/Search/SolrReserves/Results.php +++ b/module/VuFind/src/VuFind/Search/SolrReserves/Results.php @@ -65,7 +65,8 @@ class Results extends \VuFind\Search\Solr\Results */ protected function initRecordDriver($data) { - $driver = new \VuFind\RecordDriver\SolrReserves(); + $factory = $this->getServiceLocator()->get('RecordDriverPluginManager'); + $driver = clone($factory->get('SolrReserves')); $driver->setRawData($data); return $driver; } diff --git a/module/VuFind/src/VuFind/Search/Summon/Results.php b/module/VuFind/src/VuFind/Search/Summon/Results.php index 50c2968f877..b1637af9534 100644 --- a/module/VuFind/src/VuFind/Search/Summon/Results.php +++ b/module/VuFind/src/VuFind/Search/Summon/Results.php @@ -158,7 +158,8 @@ class Results extends BaseResults */ protected function initRecordDriver($data) { - $driver = new \VuFind\RecordDriver\Summon(); + $factory = $this->getServiceLocator()->get('RecordDriverPluginManager'); + $driver = clone($factory->get('Summon')); $driver->setRawData($data); return $driver; } diff --git a/module/VuFind/src/VuFind/Search/WorldCat/Results.php b/module/VuFind/src/VuFind/Search/WorldCat/Results.php index c1f1d1c8c3d..9f3bc4e9937 100644 --- a/module/VuFind/src/VuFind/Search/WorldCat/Results.php +++ b/module/VuFind/src/VuFind/Search/WorldCat/Results.php @@ -127,7 +127,8 @@ class Results extends BaseResults */ protected function initRecordDriver($data) { - $driver = new \VuFind\RecordDriver\WorldCat(); + $factory = $this->getServiceLocator()->get('RecordDriverPluginManager'); + $driver = clone($factory->get('WorldCat')); $driver->setRawData($data); return $driver; } -- GitLab