Skip to content
Snippets Groups Projects
Commit 657e83b2 authored by Demian Katz's avatar Demian Katz
Browse files

Created plugin manager for ILS drivers; refactored \VuFind\ILS\Connection to...

Created plugin manager for ILS drivers; refactored \VuFind\ILS\Connection to use plugin manager and receive injected configuration; created temporary service locator workaround for \VuFind\Connection\Manager until the class can be completely factored out.
parent 6964d584
No related merge requests found
...@@ -138,6 +138,31 @@ $config = array( ...@@ -138,6 +138,31 @@ $config = array(
'result-scroller' => 'VuFind\Controller\Plugin\ResultScroller', 'result-scroller' => 'VuFind\Controller\Plugin\ResultScroller',
) )
), ),
'ils_driver_plugin_manager' => array(
'abstract_factories' => array('VuFind\ILS\Driver\PluginFactory'),
'invokables' => array(
'aleph' => 'VuFind\ILS\Driver\Aleph',
'amicus' => 'VuFind\ILS\Driver\Amicus',
'daia' => 'VuFind\ILS\Driver\DAIA',
'demo' => 'VuFind\ILS\Driver\Demo',
'evergreen' => 'VuFind\ILS\Driver\Evergreen',
'horizon' => 'VuFind\ILS\Driver\Horizon',
'horizonxmlapi' => 'VuFind\ILS\Driver\HorizonXMLAPI',
'innovative' => 'VuFind\ILS\Driver\Innovative',
'koha' => 'VuFind\ILS\Driver\Koha',
'newgenlib' => 'VuFind\ILS\Driver\NewGenLib',
'noils' => 'VuFind\ILS\Driver\NoILS',
'pica' => 'VuFind\ILS\Driver\PICA',
'sample' => 'VuFind\ILS\Driver\Sample',
'symphony' => 'VuFind\ILS\Driver\Symphony',
'unicorn' => 'VuFind\ILS\Driver\Unicorn',
'virtua' => 'VuFind\ILS\Driver\Virtua',
'voyager' => 'VuFind\ILS\Driver\Voyager',
'voyagerrestful' => 'VuFind\ILS\Driver\VoyagerRestful',
'xcncip' => 'VuFind\ILS\Driver\XCNCIP',
'xcncip2' => 'VuFind\ILS\Driver\XCNCIP2',
),
),
'recommend_plugin_manager' => array( 'recommend_plugin_manager' => array(
'abstract_factories' => array('VuFind\Recommend\PluginFactory'), 'abstract_factories' => array('VuFind\Recommend\PluginFactory'),
'invokables' => array( 'invokables' => array(
......
...@@ -90,11 +90,13 @@ class Bootstrap ...@@ -90,11 +90,13 @@ class Bootstrap
$config = $app->getConfig(); $config = $app->getConfig();
// Use naming conventions to set up a bunch of services based on namespace: // Use naming conventions to set up a bunch of services based on namespace:
$namespaces = array('Auth', 'Autocomplete', 'Recommend', 'Session'); $namespaces = array(
'Auth', 'Autocomplete', 'ILS\Driver', 'Recommend', 'Session'
);
foreach ($namespaces as $ns) { foreach ($namespaces as $ns) {
$serviceName = $ns . 'PluginManager'; $serviceName = str_replace('\\', '', $ns) . 'PluginManager';
$className = 'VuFind\\' . $ns . '\PluginManager'; $className = 'VuFind\\' . $ns . '\PluginManager';
$configKey = strtolower($ns) . '_plugin_manager'; $configKey = strtolower(str_replace('\\', '_', $ns)) . '_plugin_manager';
$service = new $className( $service = new $className(
new ServiceManagerConfig($config[$configKey]) new ServiceManagerConfig($config[$configKey])
); );
...@@ -109,6 +111,9 @@ class Bootstrap ...@@ -109,6 +111,9 @@ class Bootstrap
$manager = new \VuFind\Search\Manager($config['search_manager']); $manager = new \VuFind\Search\Manager($config['search_manager']);
$manager->setServiceLocator($serviceManager); $manager->setServiceLocator($serviceManager);
$serviceManager->setService('SearchManager', $manager); $serviceManager->setService('SearchManager', $manager);
// TODO: factor out static connection manager.
\VuFind\Connection\Manager::setServiceLocator($serviceManager);
} }
/** /**
......
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
*/ */
namespace VuFind\Connection; namespace VuFind\Connection;
use VuFind\Config\Reader as ConfigReader, VuFind\ILS\Connection as ILSConnection; use VuFind\Config\Reader as ConfigReader, VuFind\ILS\Connection as ILSConnection,
Zend\ServiceManager\ServiceLocatorInterface;
/** /**
* Central class for connecting to resources used by VuFind. * Central class for connecting to resources used by VuFind.
...@@ -40,6 +41,20 @@ use VuFind\Config\Reader as ConfigReader, VuFind\ILS\Connection as ILSConnection ...@@ -40,6 +41,20 @@ use VuFind\Config\Reader as ConfigReader, VuFind\ILS\Connection as ILSConnection
*/ */
class Manager class Manager
{ {
protected static $serviceLocator;
/**
* Set the service locator.
*
* @param ServiceLocatorInterface $serviceLocator Locator to register
*
* @return void
*/
public static function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
self::$serviceLocator = $serviceLocator;
}
/** /**
* Connect to the catalog. * Connect to the catalog.
* *
...@@ -52,7 +67,10 @@ class Manager ...@@ -52,7 +67,10 @@ class Manager
// remember the old connection and return that instead of starting over. // remember the old connection and return that instead of starting over.
static $catalog = false; static $catalog = false;
if ($catalog === false) { if ($catalog === false) {
$config = ConfigReader::getConfig();
$catalog = new ILSConnection(); $catalog = new ILSConnection();
$catalog->setServiceLocator(self::$serviceLocator);
$catalog->setConfig($config->Catalog);
} }
return $catalog; return $catalog;
......
...@@ -30,7 +30,9 @@ ...@@ -30,7 +30,9 @@
* @link http://vufind.org/wiki/building_an_ils_driver Wiki * @link http://vufind.org/wiki/building_an_ils_driver Wiki
*/ */
namespace VuFind\ILS; namespace VuFind\ILS;
use 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;
/** /**
* Catalog Connection Class * Catalog Connection Class
...@@ -48,52 +50,78 @@ use VuFind\Config\Reader as ConfigReader, VuFind\Exception\ILS as ILSException; ...@@ -48,52 +50,78 @@ use VuFind\Config\Reader as ConfigReader, VuFind\Exception\ILS as ILSException;
class Connection class Connection
{ {
/** /**
* The class of the appropriate driver. * Has the driver been initialized yet?
* *
* @var string * @var bool
*/ */
protected $driverClass; protected $driverInitialized = false;
/** /**
* The object of the appropriate driver. * The object of the appropriate driver.
* *
* @var object * @var object
*/ */
protected $driver = false; protected $driver;
/** /**
* Constructor * The service locator
* *
* @throws ILSException * @var ServiceLocatorInterface
*/
protected $serviceLocator;
/**
* ILS configuration
*
* @var \Zend\Config\Config
*/ */
public function __construct() protected $config;
/**
* Set the configuration of the connection.
*
* @param \Zend\Config\Config $config Configuration representing the [Catalog]
* section of config.ini
*
* @return void
*/
public function setConfig($config)
{ {
$config = ConfigReader::getConfig(); $this->config = $config;
if (!isset($config->Catalog->driver)) {
if (!isset($config->driver)) {
throw new ILSException('ILS driver setting missing.'); throw new ILSException('ILS driver setting missing.');
} }
$class = 'VuFind\ILS\Driver\\' . $config->Catalog->driver; $driverManager = $this->getServiceLocator()->get('ILSDriverPluginManager');
if (!class_exists($class)) { $service = $config->driver;
if (!$driverManager->has($service)) {
// Don't throw ILSException here -- we don't want this to be // Don't throw ILSException here -- we don't want this to be
// treated as a login problem; it's more serious than that! // treated as a login problem; it's more serious than that!
throw new \Exception('ILS driver missing: ' . $class); throw new \Exception('ILS driver missing: ' . $service);
} }
$this->driverClass = $class; $this->driver = $driverManager->get($service);
// If we're configured to fail over to the NoILS driver, we need // If we're configured to fail over to the NoILS driver, we need
// to test if the main driver is working. // to test if the main driver is working.
if (isset($config->Catalog->loadNoILSOnFailure) if (isset($config->loadNoILSOnFailure) && $config->loadNoILSOnFailure) {
&& $config->Catalog->loadNoILSOnFailure
) {
try { try {
$this->getDriver(); $this->getDriver();
} catch (\Exception $e) { } catch (\Exception $e) {
$this->driver = false; $this->driver = $driverManager->get('NoILS');
$this->driverClass = 'VuFind\ILS\Driver\NoILS';
} }
} }
} }
/**
* Get class name of the driver object.
*
* @return string
*/
public function getDriverClass()
{
return get_class($this->driver);
}
/** /**
* Get access to the driver object. * Get access to the driver object.
* *
...@@ -102,10 +130,10 @@ class Connection ...@@ -102,10 +130,10 @@ class Connection
*/ */
public function getDriver() public function getDriver()
{ {
if (!$this->driver) { if (!$this->driverInitialized) {
$this->driver = new $this->driverClass;
$this->driver->setConfig($this->getDriverConfig()); $this->driver->setConfig($this->getDriverConfig());
$this->driver->init(); $this->driver->init();
$this->driverInitialized = true;
} }
return $this->driver; return $this->driver;
} }
...@@ -120,7 +148,7 @@ class Connection ...@@ -120,7 +148,7 @@ class Connection
public function getDriverConfig() public function getDriverConfig()
{ {
// Determine config file name based on class name: // Determine config file name based on class name:
$parts = explode('\\', $this->driverClass); $parts = explode('\\', $this->getDriverClass());
$configFile = end($parts) . '.ini'; $configFile = end($parts) . '.ini';
$configFilePath = ConfigReader::getConfigPath($configFile); $configFilePath = ConfigReader::getConfigPath($configFile);
return file_exists($configFilePath) return file_exists($configFilePath)
...@@ -141,7 +169,7 @@ class Connection ...@@ -141,7 +169,7 @@ class Connection
public function checkFunction($function) public function checkFunction($function)
{ {
// Extract the configuration from the driver if available: // Extract the configuration from the driver if available:
$functionConfig = method_exists($this->driverClass, 'getConfig') $functionConfig = method_exists($this->getDriverClass(), 'getConfig')
? $this->getDriver()->getConfig($function) : false; ? $this->getDriver()->getConfig($function) : false;
// See if we have a corresponding check method to analyze the response: // See if we have a corresponding check method to analyze the response:
...@@ -170,7 +198,7 @@ class Connection ...@@ -170,7 +198,7 @@ class Connection
$response = false; $response = false;
if ($this->getHoldsMode() != "none" if ($this->getHoldsMode() != "none"
&& method_exists($this->driverClass, 'placeHold') && method_exists($this->getDriverClass(), 'placeHold')
&& isset($functionConfig['HMACKeys']) && isset($functionConfig['HMACKeys'])
) { ) {
$response = array('function' => "placeHold"); $response = array('function' => "placeHold");
...@@ -182,7 +210,7 @@ class Connection ...@@ -182,7 +210,7 @@ class Connection
if (isset($functionConfig['extraHoldFields'])) { if (isset($functionConfig['extraHoldFields'])) {
$response['extraHoldFields'] = $functionConfig['extraHoldFields']; $response['extraHoldFields'] = $functionConfig['extraHoldFields'];
} }
} else if (method_exists($this->driverClass, 'getHoldLink')) { } else if (method_exists($this->getDriverClass(), 'getHoldLink')) {
$response = array('function' => "getHoldLink"); $response = array('function' => "getHoldLink");
} }
return $response; return $response;
...@@ -203,14 +231,13 @@ class Connection ...@@ -203,14 +231,13 @@ class Connection
protected function checkMethodcancelHolds($functionConfig) protected function checkMethodcancelHolds($functionConfig)
{ {
$response = false; $response = false;
$config = ConfigReader::getConfig();
if ($config->Catalog->cancel_holds_enabled == true if ($this->config->cancel_holds_enabled == true
&& method_exists($this->driverClass, 'cancelHolds') && method_exists($this->getDriverClass(), 'cancelHolds')
) { ) {
$response = array('function' => "cancelHolds"); $response = array('function' => "cancelHolds");
} else if ($config->Catalog->cancel_holds_enabled == true } else if ($this->config->cancel_holds_enabled == true
&& method_exists($this->driverClass, 'getCancelHoldLink') && method_exists($this->getDriverClass(), 'getCancelHoldLink')
) { ) {
$response = array('function' => "getCancelHoldLink"); $response = array('function' => "getCancelHoldLink");
} }
...@@ -231,14 +258,13 @@ class Connection ...@@ -231,14 +258,13 @@ class Connection
protected function checkMethodRenewals($functionConfig) protected function checkMethodRenewals($functionConfig)
{ {
$response = false; $response = false;
$config = ConfigReader::getConfig();
if ($config->Catalog->renewals_enabled == true if ($this->config->renewals_enabled == true
&& method_exists($this->driverClass, 'renewMyItems') && method_exists($this->getDriverClass(), 'renewMyItems')
) { ) {
$response = array('function' => "renewMyItems"); $response = array('function' => "renewMyItems");
} else if ($config->Catalog->renewals_enabled == true } else if ($this->config->renewals_enabled == true
&& method_exists($this->driverClass, 'renewMyItemsLink') && method_exists($this->getDriverClass(), 'renewMyItemsLink')
) { ) {
$response = array('function' => "renewMyItemsLink"); $response = array('function' => "renewMyItemsLink");
} }
...@@ -259,7 +285,7 @@ class Connection ...@@ -259,7 +285,7 @@ class Connection
*/ */
public function checkRequestIsValid($id, $data, $patron) public function checkRequestIsValid($id, $data, $patron)
{ {
$method = array($this->driverClass, 'checkRequestIsValid'); $method = array($this->getDriverClass(), 'checkRequestIsValid');
if (is_callable($method)) { if (is_callable($method)) {
return $this->getDriver()->checkRequestIsValid($id, $data, $patron); return $this->getDriver()->checkRequestIsValid($id, $data, $patron);
} }
...@@ -294,8 +320,8 @@ class Connection ...@@ -294,8 +320,8 @@ class Connection
public function getOfflineMode() public function getOfflineMode()
{ {
// Graceful degradation -- return false if no method supported. // Graceful degradation -- return false if no method supported.
return method_exists($this->driverClass, 'getOfflineMode') ? return method_exists($this->getDriverClass(), 'getOfflineMode')
$this->getDriver()->getOfflineMode() : false; ? $this->getDriver()->getOfflineMode() : false;
} }
/** /**
...@@ -324,8 +350,8 @@ class Connection ...@@ -324,8 +350,8 @@ class Connection
public function hasHoldings($id) public function hasHoldings($id)
{ {
// Graceful degradation -- return true if no method supported. // Graceful degradation -- return true if no method supported.
return method_exists($this->driverClass, 'hasHoldings') ? return method_exists($this->getDriverClass(), 'hasHoldings')
$this->getDriver()->hasHoldings($id) : true; ? $this->getDriver()->hasHoldings($id) : true;
} }
/** /**
...@@ -338,8 +364,8 @@ class Connection ...@@ -338,8 +364,8 @@ class Connection
public function loginIsHidden() public function loginIsHidden()
{ {
// Graceful degradation -- return false if no method supported. // Graceful degradation -- return false if no method supported.
return method_exists($this->driverClass, 'loginIsHidden') ? return method_exists($this->getDriverClass(), 'loginIsHidden')
$this->getDriver()->loginIsHidden() : false; ? $this->getDriver()->loginIsHidden() : false;
} }
/** /**
...@@ -354,11 +380,34 @@ class Connection ...@@ -354,11 +380,34 @@ class Connection
*/ */
public function __call($methodName, $params) public function __call($methodName, $params)
{ {
if (is_callable(array($this->driverClass, $methodName))) { if (is_callable(array($this->getDriverClass(), $methodName))) {
return call_user_func_array( return call_user_func_array(
array($this->getDriver(), $methodName), $params array($this->getDriver(), $methodName), $params
); );
} }
throw new ILSException('Cannot call method: ' . $methodName); throw new ILSException('Cannot call method: ' . $methodName);
} }
/**
* Set the service locator.
*
* @param ServiceLocatorInterface $serviceLocator Locator to register
*
* @return Connection
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
}
/**
* Get the service locator.
*
* @return \Zend\ServiceManager\ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}
} }
<?php
/**
* ILS 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 ILS_Drivers
* @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\ILS\Driver;
/**
* ILS driver plugin factory
*
* @category VuFind2
* @package ILS_Drivers
* @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\ILS\Driver';
}
}
\ No newline at end of file
<?php
/**
* ILS 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 ILS_Drivers
* @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\ILS\Driver;
/**
* ILS driver plugin manager
*
* @category VuFind2
* @package ILS_Drivers
* @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\ILS\Driver\DriverInterface';
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment