From fe87fc1dc06f8783d6e8b6c694ceaa1c343e0d86 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 6 Feb 2015 13:23:01 -0500
Subject: [PATCH] Eliminated dependency on service manager.

---
 module/VuFind/config/module.config.php        |  2 +-
 module/VuFind/src/VuFind/Db/Row/User.php      | 33 ++++++++++++++-----
 module/VuFind/src/VuFind/Db/Table/Factory.php | 14 ++++++++
 module/VuFind/src/VuFind/Db/Table/Gateway.php | 22 ++++++++++---
 module/VuFind/src/VuFind/Db/Table/User.php    | 24 +++++++++++++-
 5 files changed, 79 insertions(+), 16 deletions(-)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 0d574b0958f..6a6e4e7147b 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -322,6 +322,7 @@ $config = array(
                 'abstract_factories' => array('VuFind\Db\Table\PluginFactory'),
                 'factories' => array(
                     'resource' => 'VuFind\Db\Table\Factory::getResource',
+                    'user' => 'VuFind\Db\Table\Factory::getUser',
                 ),
                 'invokables' => array(
                     'changetracker' => 'VuFind\Db\Table\ChangeTracker',
@@ -331,7 +332,6 @@ $config = array(
                     'search' => 'VuFind\Db\Table\Search',
                     'session' => 'VuFind\Db\Table\Session',
                     'tags' => 'VuFind\Db\Table\Tags',
-                    'user' => 'VuFind\Db\Table\User',
                     'userlist' => 'VuFind\Db\Table\UserList',
                     'userresource' => 'VuFind\Db\Table\UserResource',
                     'userstats' => 'VuFind\Db\Table\UserStats',
diff --git a/module/VuFind/src/VuFind/Db/Row/User.php b/module/VuFind/src/VuFind/Db/Row/User.php
index e64d61e3669..5cfe5fd889b 100644
--- a/module/VuFind/src/VuFind/Db/Row/User.php
+++ b/module/VuFind/src/VuFind/Db/Row/User.php
@@ -58,6 +58,13 @@ class User extends ServiceLocatorAwareGateway
      */
     protected $encryptionKey = null;
 
+    /**
+     * VuFind configuration
+     *
+     * @var \Zend\Config\Config
+     */
+    protected $config = null;
+
     /**
      * Constructor
      *
@@ -68,6 +75,18 @@ class User extends ServiceLocatorAwareGateway
         parent::__construct('id', 'user', $adapter);
     }
 
+    /**
+     * Configuration setter
+     *
+     * @param \Zend\Config\Config $config VuFind configuration
+     *
+     * @return void
+     */
+    public function setConfig(\Zend\Config\Config $config)
+    {
+        $this->config = $config;
+    }
+
     /**
      * Reset ILS login credentials.
      *
@@ -124,11 +143,9 @@ class User extends ServiceLocatorAwareGateway
     protected function passwordEncryptionEnabled()
     {
         if (null === $this->encryptionEnabled) {
-            $config = $this->getServiceLocator()->getServiceLocator()
-                ->get('VuFind\Config')->get('config');
             $this->encryptionEnabled
-                = isset($config->Authentication->encrypt_ils_password)
-                ? $config->Authentication->encrypt_ils_password : false;
+                = isset($this->config->Authentication->encrypt_ils_password)
+                ? $this->config->Authentication->encrypt_ils_password : false;
         }
         return $this->encryptionEnabled;
     }
@@ -153,16 +170,14 @@ class User extends ServiceLocatorAwareGateway
 
         // Load encryption key from configuration if not already present:
         if (null === $this->encryptionKey) {
-            $config = $this->getServiceLocator()->getServiceLocator()
-                ->get('VuFind\Config')->get('config');
-            if (!isset($config->Authentication->ils_encryption_key)
-                || empty($config->Authentication->ils_encryption_key)
+            if (!isset($this->config->Authentication->ils_encryption_key)
+                || empty($this->config->Authentication->ils_encryption_key)
             ) {
                 throw new \VuFind\Exception\PasswordSecurity(
                     'ILS password encryption on, but no key set.'
                 );
             }
-            $this->encryptionKey = $config->Authentication->ils_encryption_key;
+            $this->encryptionKey = $this->config->Authentication->ils_encryption_key;
         }
 
         // Perform encryption:
diff --git a/module/VuFind/src/VuFind/Db/Table/Factory.php b/module/VuFind/src/VuFind/Db/Table/Factory.php
index bc308970714..5497763a6ca 100644
--- a/module/VuFind/src/VuFind/Db/Table/Factory.php
+++ b/module/VuFind/src/VuFind/Db/Table/Factory.php
@@ -51,4 +51,18 @@ class Factory
     {
         return new Resource($sm->getServiceLocator()->get('VuFind\DateConverter'));
     }
+
+    /**
+     * Construct the User table.
+     *
+     * @param ServiceManager $sm Service manager.
+     *
+     * @return User
+     */
+    public static function getUser(ServiceManager $sm)
+    {
+        return new User(
+            $sm->getServiceLocator()->get('VuFind\Config')->get('config')
+        );
+    }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Db/Table/Gateway.php b/module/VuFind/src/VuFind/Db/Table/Gateway.php
index d1d314c0658..05b5698241c 100644
--- a/module/VuFind/src/VuFind/Db/Table/Gateway.php
+++ b/module/VuFind/src/VuFind/Db/Table/Gateway.php
@@ -105,12 +105,24 @@ class Gateway extends AbstractTableGateway implements ServiceLocatorAwareInterfa
         parent::initialize();
         if (null !== $this->rowClass) {
             $resultSetPrototype = $this->getResultSetPrototype();
-            $prototype = new $this->rowClass($this->getAdapter());
-            if ($prototype instanceof ServiceLocatorAwareInterface) {
-                $prototype->setServiceLocator($this->getServiceLocator());
-            }
-            $resultSetPrototype->setArrayObjectPrototype($prototype);
+            $resultSetPrototype->setArrayObjectPrototype(
+                $this->initializeRowPrototype()
+            );
+        }
+    }
+
+    /**
+     * Construct the prototype for rows.
+     *
+     * @return object
+     */
+    protected function initializeRowPrototype()
+    {
+        $prototype = new $this->rowClass($this->getAdapter());
+        if ($prototype instanceof ServiceLocatorAwareInterface) {
+            $prototype->setServiceLocator($this->getServiceLocator());
         }
+        return $prototype;
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Db/Table/User.php b/module/VuFind/src/VuFind/Db/Table/User.php
index b65942e52a7..9978af7f188 100644
--- a/module/VuFind/src/VuFind/Db/Table/User.php
+++ b/module/VuFind/src/VuFind/Db/Table/User.php
@@ -38,12 +38,22 @@ namespace VuFind\Db\Table;
  */
 class User extends Gateway
 {
+    /**
+     * VuFind configuration
+     *
+     * @var \Zend\Config\Config
+     */
+    protected $config;
+
     /**
      * Constructor
+     *
+     * @param \Zend\Config\Config $config VuFind configuration
      */
-    public function __construct()
+    public function __construct(\Zend\Config\Config $config)
     {
         parent::__construct('user', 'VuFind\Db\Row\User');
+        $this->config = $config;
     }
 
     /**
@@ -94,6 +104,18 @@ class User extends Gateway
         return $this->select($callback);
     }
 
+    /**
+     * Construct the prototype for rows.
+     *
+     * @return object
+     */
+    protected function initializeRowPrototype()
+    {
+        $prototype = parent::initializeRowPrototype();
+        $prototype->setConfig($this->config);
+        return $prototype;
+    }
+
     /**
      * Return a row by a verification hash
      *
-- 
GitLab