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

Use initializer instead of custom get method to set up table gateway objects...

Use initializer instead of custom get method to set up table gateway objects (more framework-friendly approach) and inject DB adapter from outside (more flexible/comprehensible).
parent 4a168c8c
No related merge requests found
......@@ -63,6 +63,18 @@ class Gateway extends AbstractTableGateway implements ServiceLocatorAwareInterfa
$this->rowClass = $rowClass;
}
/**
* Set database adapter
*
* @param \Zend\Db\Adapter\Adapter $adapter Database adapter
*
* @return void
*/
public function setAdapter(\Zend\Db\Adapter\Adapter $adapter)
{
$this->adapter = $adapter;
}
/**
* Initialize
*
......@@ -73,8 +85,6 @@ class Gateway extends AbstractTableGateway implements ServiceLocatorAwareInterfa
if ($this->isInitialized) {
return;
}
$this->adapter = $this->getServiceLocator()->getServiceLocator()
->get('DBAdapter');
parent::initialize();
if (null !== $this->rowClass) {
$resultSetPrototype = $this->getResultSetPrototype();
......
......@@ -39,37 +39,33 @@ namespace VuFind\Db\Table;
class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
{
/**
* Return the name of the base class or interface that plug-ins must conform
* to.
* Constructor
*
* @return string
* Make sure table gateways are properly initialized.
*
* @param null|ConfigInterface $configuration
*/
protected function getExpectedInterface()
{
return 'VuFind\Db\Table\Gateway';
public function __construct(
\Zend\ServiceManager\ConfigInterface $configuration = null
) {
parent::__construct($configuration);
$self = $this;
$initializer = function ($instance) use ($self) {
$instance->setAdapter($self->getServiceLocator()->get('DBAdapter'));
$instance->initialize();
};
$this->addInitializer($initializer);
}
/**
* Retrieve a service from the manager by name
*
* Allows passing an array of options to use when creating the instance.
* createFromInvokable() will use these and pass them to the instance
* constructor if not null and a non-empty array.
*
* @param string $name Service name
* @param array $options Options array
* @param bool $usePeeringServiceManagers Use peering service managers switch
* Return the name of the base class or interface that plug-ins must conform
* to.
*
* @return object
* @return string
*/
public function get($name, $options = array(), $usePeeringServiceManagers = true)
protected function getExpectedInterface()
{
// Obtain the object from the parent:
$obj = parent::get($name, $options, $usePeeringServiceManagers);
// Make sure it is properly initialized:
$obj->initialize();
return $obj;
return 'VuFind\Db\Table\Gateway';
}
}
\ 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