Skip to content
Snippets Groups Projects
Commit 219f114b authored by Demian Katz's avatar Demian Katz Committed by Robert Lange
Browse files

Eliminate static DB adapter factory.

parent 1d8c8223
Branches
Tags
No related merge requests found
......@@ -392,7 +392,7 @@ $config = [
'VuFind\Validator\Csrf' => 'VuFind\Validator\CsrfFactory',
'VuFindHttp\HttpService' => 'VuFind\Service\Factory::getHttp',
'VuFindSearch\Service' => 'VuFind\Service\Factory::getSearchService',
'Zend\Db\Adapter\Adapter' => 'VuFind\Service\Factory::getDbAdapter',
'Zend\Db\Adapter\Adapter' => 'VuFind\Db\AdapterFactory',
'Zend\Mvc\I18n\Translator' => 'VuFind\I18n\Translator\TranslatorFactory',
'Zend\Session\SessionManager' => 'VuFind\Session\ManagerFactory',
],
......
<?php
/**
* Database utility class.
* Database utility class. May be used as a service or as a standard
* Zend Framework factory.
*
* PHP version 7
*
......@@ -27,10 +28,13 @@
*/
namespace VuFind\Db;
use Interop\Container\ContainerInterface;
use Zend\Config\Config;
use Zend\Db\Adapter\Adapter;
/**
* Database utility class.
* Database utility class. May be used as a service or as a standard
* Zend Framework factory.
*
* @category VuFind
* @package Db
......@@ -38,23 +42,49 @@ use Zend\Db\Adapter\Adapter;
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
class AdapterFactory
class AdapterFactory implements \Zend\ServiceManager\Factory\FactoryInterface
{
/**
* VuFind configuration
*
* @var \Zend\Config\Config
* @var Config
*/
protected $config;
/**
* Constructor
*
* @param \Zend\Config\Config $config VuFind configuration
* @param Config $config VuFind configuration (provided when used as service;
* omitted when used as factory)
*/
public function __construct(\Zend\Config\Config $config)
public function __construct(Config $config = null)
{
$this->config = $config;
$this->config = $config ?: new Config([]);
}
/**
* Create an object (glue code for FactoryInterface compliance)
*
* @param ContainerInterface $container Service manager
* @param string $requestedName Service being created
* @param null|array $options Extra options (optional)
*
* @return object
*
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function __invoke(ContainerInterface $container, $requestedName,
array $options = null
) {
if (!empty($options)) {
throw new \Exception('Unexpected options sent to factory!');
}
$this->config = $container->get(\VuFind\Config\PluginManager::class)
->get('config');
return $this->getAdapter();
}
/**
......@@ -70,6 +100,9 @@ class AdapterFactory
public function getAdapter($overrideUser = null, $overridePass = null)
{
// Parse details from connection string:
if (!isset($this->config->Database->database)) {
throw new \Exception('"database" setting missing');
}
return $this->getAdapterFromConnectionString(
$this->config->Database->database, $overrideUser, $overridePass
);
......
......@@ -42,18 +42,6 @@ use Zend\ServiceManager\ServiceManager;
*/
class Factory
{
/**
* Construct the date converter.
*
* @param ServiceManager $sm Service manager.
*
* @return \Zend\Db\Adapter\Adapter
*/
public static function getDbAdapter(ServiceManager $sm)
{
return $sm->get('VuFind\Db\AdapterFactory')->getAdapter();
}
/**
* Construct the HTTP service.
*
......
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