From 219f114b41469a4aa0655730df9e56ca94d3f998 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Mon, 28 Jan 2019 13:36:54 -0500 Subject: [PATCH] Eliminate static DB adapter factory. --- module/VuFind/config/module.config.php | 2 +- .../VuFind/src/VuFind/Db/AdapterFactory.php | 47 ++++++++++++++++--- module/VuFind/src/VuFind/Service/Factory.php | 12 ----- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 75694cf65b8..833a1fc623a 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -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', ], diff --git a/module/VuFind/src/VuFind/Db/AdapterFactory.php b/module/VuFind/src/VuFind/Db/AdapterFactory.php index 71f74665ced..79b7393706a 100644 --- a/module/VuFind/src/VuFind/Db/AdapterFactory.php +++ b/module/VuFind/src/VuFind/Db/AdapterFactory.php @@ -1,6 +1,7 @@ <?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 ); diff --git a/module/VuFind/src/VuFind/Service/Factory.php b/module/VuFind/src/VuFind/Service/Factory.php index ea0f5da40c4..fc435745ae5 100644 --- a/module/VuFind/src/VuFind/Service/Factory.php +++ b/module/VuFind/src/VuFind/Service/Factory.php @@ -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. * -- GitLab