From fb28c9bf4c8ee68711cb233321696cd01383c957 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 8 Oct 2012 14:44:37 -0400
Subject: [PATCH] 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).

---
 module/VuFind/src/VuFind/Db/Table/Gateway.php | 14 +++++-
 .../src/VuFind/Db/Table/PluginManager.php     | 44 +++++++++----------
 2 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/module/VuFind/src/VuFind/Db/Table/Gateway.php b/module/VuFind/src/VuFind/Db/Table/Gateway.php
index f212d764794..b05e8436947 100644
--- a/module/VuFind/src/VuFind/Db/Table/Gateway.php
+++ b/module/VuFind/src/VuFind/Db/Table/Gateway.php
@@ -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();
diff --git a/module/VuFind/src/VuFind/Db/Table/PluginManager.php b/module/VuFind/src/VuFind/Db/Table/PluginManager.php
index a9c5642c966..eb287316f93 100644
--- a/module/VuFind/src/VuFind/Db/Table/PluginManager.php
+++ b/module/VuFind/src/VuFind/Db/Table/PluginManager.php
@@ -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
-- 
GitLab