diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 75694cf65b8b6df498ecc2bc36e2886fbadc59d1..833a1fc623a19a4cb8e612e1516fc2009deb4a89 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 71f74665ced916c7f2d1616464327df58a4a91dd..79b7393706a0b90c81a99c5240fda1f2d58bda7f 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 ea0f5da40c43113987bdcbebcd22a6aa50f1add1..fc435745ae58a7b7c8efadd3172e836cca8c0909 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. *