From c353d98213c32aed12fe75435d477b0bd823554a Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 12 Sep 2012 13:40:37 -0400
Subject: [PATCH] Eliminated \VuFind\Record\Loader::getInstance() -- code now
 uses the service manager.

---
 module/VuFind/src/VuFind/Bootstrap.php        |  3 +-
 module/VuFind/src/VuFind/Cart.php             | 56 ++++++++++++++++++-
 .../src/VuFind/Controller/AbstractBase.php    | 10 ++++
 .../src/VuFind/Controller/AjaxController.php  | 13 ++---
 .../src/VuFind/Controller/CartController.php  |  9 ++-
 .../Controller/MyResearchController.php       |  7 +--
 .../VuFind/Controller/UpgradeController.php   |  3 +-
 .../VuFind/src/VuFind/Db/Table/Resource.php   |  5 +-
 module/VuFind/src/VuFind/Record/Loader.php    | 15 -----
 .../src/VuFind/Search/Favorites/Results.php   |  4 +-
 .../src/VuFind/Search/MixedList/Results.php   |  5 +-
 .../VuFind/src/VuFind/Search/Tags/Results.php |  5 +-
 12 files changed, 89 insertions(+), 46 deletions(-)

diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php
index 2b20aded9ad..0db9ff1b98e 100644
--- a/module/VuFind/src/VuFind/Bootstrap.php
+++ b/module/VuFind/src/VuFind/Bootstrap.php
@@ -114,9 +114,8 @@ class Bootstrap
         $manager->setServiceLocator($serviceManager);
         $serviceManager->setService('SearchManager', $manager);
 
-        // TODO: factor out static connection manager and record loader.
+        // TODO: factor out static connection manager.
         \VuFind\Connection\Manager::setServiceLocator($serviceManager);
-        \VuFind\Record\Loader::getInstance()->setServiceLocator($serviceManager);
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Cart.php b/module/VuFind/src/VuFind/Cart.php
index 3e0a3c25487..2d2c9e0fb67 100644
--- a/module/VuFind/src/VuFind/Cart.php
+++ b/module/VuFind/src/VuFind/Cart.php
@@ -26,7 +26,9 @@
  * @link     http://vufind.org/wiki/system_classes Wiki
  */
 namespace VuFind;
-use VuFind\Config\Reader as ConfigReader, VuFind\Record\Loader as RecordLoader;
+use VuFind\Config\Reader as ConfigReader,
+    Zend\ServiceManager\ServiceLocatorAwareInterface,
+    Zend\ServiceManager\ServiceLocatorInterface;
 
 /**
  * Cart Class
@@ -39,12 +41,36 @@ use VuFind\Config\Reader as ConfigReader, VuFind\Record\Loader as RecordLoader;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/system_classes Wiki
  */
-class Cart
+class Cart implements ServiceLocatorAwareInterface
 {
+    /**
+     * Cart contents.
+     *
+     * @var array
+     */
     protected $items;
+
+    /**
+     * Maximum number of items allowed in cart.
+     *
+     * @var int
+     */
     protected $maxSize = 100;
+
+    /**
+     * Is the cart currently activated?
+     *
+     * @var bool
+     */
     protected $active = false;
 
+    /**
+     * Service locator
+     *
+     * @var ServiceLocatorInterface
+     */
+    protected $serviceLocator;
+
     const CART_COOKIE =  'vufind_cart';
     const CART_COOKIE_SOURCES = 'vufind_cart_src';
     const CART_COOKIE_DELIM = "\t";
@@ -267,6 +293,30 @@ class Cart
      */
     public function getRecordDetails()
     {
-        return RecordLoader::getInstance()->loadBatch($this->items);
+        return $this->getServiceLocator()
+            ->get('RecordLoader')->loadBatch($this->items);
+    }
+
+    /**
+     * Set the service locator.
+     *
+     * @param ServiceLocatorInterface $serviceLocator Locator to register
+     *
+     * @return Manager
+     */
+    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
+    {
+        $this->serviceLocator = $serviceLocator;
+        return $this;
+    }
+
+    /**
+     * Get the service locator.
+     *
+     * @return \Zend\ServiceManager\ServiceLocatorInterface
+     */
+    public function getServiceLocator()
+    {
+        return $this->serviceLocator;
     }
 }
diff --git a/module/VuFind/src/VuFind/Controller/AbstractBase.php b/module/VuFind/src/VuFind/Controller/AbstractBase.php
index 952c043db2e..0e3cbcbc726 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractBase.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractBase.php
@@ -189,6 +189,16 @@ class AbstractBase extends AbstractActionController
         return $patron;
     }
 
+    /**
+     * Get the record loader
+     *
+     * @return \VuFind\Record\Loader
+     */
+    protected function getRecordLoader()
+    {
+        return $this->getServiceLocator()->get('RecordLoader');
+    }
+
     /**
      * Get the search manager.
      *
diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php
index 8aa102642b9..67c3d73dbe9 100644
--- a/module/VuFind/src/VuFind/Controller/AjaxController.php
+++ b/module/VuFind/src/VuFind/Controller/AjaxController.php
@@ -29,8 +29,7 @@ namespace VuFind\Controller;
 use VuFind\Config\Reader as ConfigReader,
     VuFind\Connection\Manager as ConnectionManager,
     VuFind\Exception\Auth as AuthException, VuFind\Export, VuFind\Mailer,
-    VuFind\Mailer\SMS, VuFind\Record\Loader as RecordLoader,
-    VuFind\Translator\Translator;
+    VuFind\Mailer\SMS, VuFind\Translator\Translator;
 
 /**
  * This controller handles global AJAX functionality
@@ -606,7 +605,7 @@ class AjaxController extends AbstractBase
         }
         // empty tag
         try {
-            $driver = RecordLoader::getInstance()->load(
+            $driver = $this->getRecordLoader()->load(
                 $this->params()->fromPost('id'),
                 $this->params()->fromPost('source', 'VuFind')
             );
@@ -829,7 +828,7 @@ class AjaxController extends AbstractBase
             );
         }
 
-        $driver = RecordLoader::getInstance()->load(
+        $driver = $this->getRecordLoader()->load(
             $this->params()->fromPost('id'),
             $this->params()->fromPost('source', 'VuFind')
         );
@@ -937,7 +936,7 @@ class AjaxController extends AbstractBase
     {
         // Attempt to send the email:
         try {
-            $record = RecordLoader::getInstance()->load(
+            $record = $this->getRecordLoader()->load(
                 $this->params()->fromPost('id'),
                 $this->params()->fromPost('source', 'VuFind')
             );
@@ -966,7 +965,7 @@ class AjaxController extends AbstractBase
     {
         // Attempt to send the email:
         try {
-            $record = RecordLoader::getInstance()->load(
+            $record = $this->getRecordLoader()->load(
                 $this->params()->fromPost('id'),
                 $this->params()->fromPost('source', 'VuFind')
             );
@@ -1126,7 +1125,7 @@ class AjaxController extends AbstractBase
      */
     public function getRecordCommentsAsHTML()
     {
-        $driver = RecordLoader::getInstance()->load(
+        $driver = $this->getRecordLoader()->load(
             $this->params()->fromQuery('id'),
             $this->params()->fromQuery('source', 'VuFind')
         );
diff --git a/module/VuFind/src/VuFind/Controller/CartController.php b/module/VuFind/src/VuFind/Controller/CartController.php
index 889c34157a3..3ac4864703c 100644
--- a/module/VuFind/src/VuFind/Controller/CartController.php
+++ b/module/VuFind/src/VuFind/Controller/CartController.php
@@ -27,7 +27,6 @@
  */
 namespace VuFind\Controller;
 use VuFind\Exception\Mail as MailException, VuFind\Export, VuFind\Mailer,
-    VuFind\Record\Loader as RecordLoader,
     VuFind\Translator\Translator, Zend\Session\Container as SessionContainer;
 
 /**
@@ -174,7 +173,7 @@ class CartController extends AbstractBase
             return $this->redirectToSource('error', 'bulk_noitems_advice');
         }
         $view = $this->createViewModel();
-        $view->records = RecordLoader::getInstance()->loadBatch($ids);
+        $view->records = $this->getRecordLoader()->loadBatch($ids);
 
         // Process form submission:
         if ($this->params()->fromPost('submit')) {
@@ -258,7 +257,7 @@ class CartController extends AbstractBase
 
         // Load the records:
         $view = $this->createViewModel();
-        $view->records = RecordLoader::getInstance()->loadBatch($ids);
+        $view->records = $this->getRecordLoader()->loadBatch($ids);
 
         // Assign the list of legal export options.  We'll filter them down based
         // on what the selected records actually support.
@@ -305,7 +304,7 @@ class CartController extends AbstractBase
 
 
         // Actually export the records
-        $records = RecordLoader::getInstance()->loadBatch($ids);
+        $records = $this->getRecordLoader()->loadBatch($ids);
         $recordHelper = $this->getViewRenderer()->plugin('record');
         $parts = array();
         foreach ($records as $record) {
@@ -356,7 +355,7 @@ class CartController extends AbstractBase
         // Pass record and list information to view:
         return $this->createViewModel(
             array(
-                'records' => RecordLoader::getInstance()->loadBatch($ids),
+                'records' => $this->getRecordLoader()->loadBatch($ids),
                 'lists' => $user->getLists()
             )
         );
diff --git a/module/VuFind/src/VuFind/Controller/MyResearchController.php b/module/VuFind/src/VuFind/Controller/MyResearchController.php
index 4e396c444b0..6464876b4dc 100644
--- a/module/VuFind/src/VuFind/Controller/MyResearchController.php
+++ b/module/VuFind/src/VuFind/Controller/MyResearchController.php
@@ -32,8 +32,7 @@ use VuFind\Config\Reader as ConfigReader,
     VuFind\Exception\Auth as AuthException,
     VuFind\Exception\ListPermission as ListPermissionException,
     VuFind\Exception\RecordMissing as RecordMissingException,
-    VuFind\Record\Loader as RecordLoader, VuFind\Record\Router as RecordRouter,
-    Zend\Stdlib\Parameters;
+    VuFind\Record\Router as RecordRouter, Zend\Stdlib\Parameters;
 
 /**
  * Controller for the user account area.
@@ -321,7 +320,7 @@ class MyResearchController extends AbstractBase
         return $this->createViewModel(
             array(
                 'list' => $list, 'deleteIDS' => $ids,
-                'records' => RecordLoader::getInstance()->loadBatch($ids)
+                'records' => $this->getRecordLoader()->loadBatch($ids)
             )
         );
     }
@@ -424,7 +423,7 @@ class MyResearchController extends AbstractBase
         $source = $this->params()->fromPost(
             'source', $this->params()->fromQuery('source', 'VuFind')
         );
-        $driver = RecordLoader::getInstance()->load($id, $source);
+        $driver = $this->getRecordLoader()->load($id, $source);
         $listID = $this->params()->fromPost(
             'list_id', $this->params()->fromQuery('list_id', null)
         );
diff --git a/module/VuFind/src/VuFind/Controller/UpgradeController.php b/module/VuFind/src/VuFind/Controller/UpgradeController.php
index f9dafa37a87..672580226c5 100644
--- a/module/VuFind/src/VuFind/Controller/UpgradeController.php
+++ b/module/VuFind/src/VuFind/Controller/UpgradeController.php
@@ -30,7 +30,6 @@ use ArrayObject, VuFind\Cache\Manager as CacheManager,
     VuFind\Config\Reader as ConfigReader, VuFind\Cookie\Container as CookieContainer,
     VuFind\Db\AdapterFactory,
     VuFind\Exception\RecordMissing as RecordMissingException,
-    VuFind\Record\Loader as RecordLoader,
     Zend\Db\TableGateway\Feature\GlobalAdapterFeature as DbGlobalAdapter,
     Zend\Session\Container as SessionContainer;
 
@@ -411,7 +410,7 @@ class UpgradeController extends AbstractBase
         if (strlen($this->params()->fromPost('submit', '')) > 0) {
             foreach ($problems as $problem) {
                 try {
-                    $driver = RecordLoader::getInstance()
+                    $driver = $this->getRecordLoader()
                         ->load($problem->record_id, $problem->source);
                     $problem->assignMetadata($driver)->save();
                 } catch (RecordMissingException $e) {
diff --git a/module/VuFind/src/VuFind/Db/Table/Resource.php b/module/VuFind/src/VuFind/Db/Table/Resource.php
index b4a483c7bc5..200fdc86bc3 100644
--- a/module/VuFind/src/VuFind/Db/Table/Resource.php
+++ b/module/VuFind/src/VuFind/Db/Table/Resource.php
@@ -26,7 +26,7 @@
  * @link     http://vufind.org   Main Site
  */
 namespace VuFind\Db\Table;
-use VuFind\Record\Loader as RecordLoader, Zend\Db\Sql\Expression;
+use Zend\Db\Sql\Expression;
 
 /**
  * Table Definition for resource
@@ -79,7 +79,8 @@ class Resource extends Gateway
 
             // Load record if it was not provided:
             if (is_null($driver)) {
-                $driver = RecordLoader::getInstance()->load($id, $source);
+                $driver = $this->getServiceLocator()->getServiceLocator()
+                    ->get('RecordLoader')->load($id, $source);
             }
 
             // Load metadata into the database for sorting/failback purposes:
diff --git a/module/VuFind/src/VuFind/Record/Loader.php b/module/VuFind/src/VuFind/Record/Loader.php
index 8c68f0e3a4d..15ad4762023 100644
--- a/module/VuFind/src/VuFind/Record/Loader.php
+++ b/module/VuFind/src/VuFind/Record/Loader.php
@@ -47,21 +47,6 @@ class Loader implements ServiceLocatorAwareInterface
      */
     protected $serviceLocator;
 
-    /**
-     * Temporary method to get instance of object -- TODO: eliminate in place of
-     * service manager after further refactoring.
-     *
-     * @return Loader
-     */
-    static public function getInstance()
-    {
-        static $instance = false;
-        if (!$instance) {
-            $instance = new Loader();
-        }
-        return $instance;
-    }
-
     /**
      * Given a record source, return the search object that can load that type of
      * record.
diff --git a/module/VuFind/src/VuFind/Search/Favorites/Results.php b/module/VuFind/src/VuFind/Search/Favorites/Results.php
index 637b1964dbb..992f8e6c9e1 100644
--- a/module/VuFind/src/VuFind/Search/Favorites/Results.php
+++ b/module/VuFind/src/VuFind/Search/Favorites/Results.php
@@ -27,7 +27,6 @@
  */
 namespace VuFind\Search\Favorites;
 use VuFind\Exception\ListPermission as ListPermissionException,
-    VuFind\Record\Loader as RecordLoader,
     VuFind\Search\Base\Results as BaseResults,
     VuFind\Translator\Translator;
 
@@ -170,7 +169,8 @@ class Results extends BaseResults
                 )
             );
         }
-        $this->results = RecordLoader::getInstance()->loadBatch($recordsToRequest);
+        $this->results = $this->getServiceLocator()->get('RecordLoader')
+            ->loadBatch($recordsToRequest);
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Search/MixedList/Results.php b/module/VuFind/src/VuFind/Search/MixedList/Results.php
index 0bfbafc168d..14f1d82c1da 100644
--- a/module/VuFind/src/VuFind/Search/MixedList/Results.php
+++ b/module/VuFind/src/VuFind/Search/MixedList/Results.php
@@ -26,7 +26,7 @@
  * @link     http://vufind.org   Main Site
  */
 namespace VuFind\Search\MixedList;
-use VuFind\Record\Loader as RecordLoader, VuFind\Search\Base\Results as BaseResults;
+use VuFind\Search\Base\Results as BaseResults;
 
 /**
  * Search Mixed List Results
@@ -62,7 +62,8 @@ class Results extends BaseResults
     protected function performSearch()
     {
         $recordsToRequest = $this->getParams()->getRecordsToRequest();
-        $this->results = RecordLoader::getInstance()->loadBatch($recordsToRequest);
+        $this->results = $this->getServiceLocator()->get('RecordLoader')
+            ->loadBatch($recordsToRequest);
         $this->resultTotal = count($this->results);
     }
 
diff --git a/module/VuFind/src/VuFind/Search/Tags/Results.php b/module/VuFind/src/VuFind/Search/Tags/Results.php
index 98f36461565..b319238addf 100644
--- a/module/VuFind/src/VuFind/Search/Tags/Results.php
+++ b/module/VuFind/src/VuFind/Search/Tags/Results.php
@@ -26,7 +26,7 @@
  * @link     http://vufind.org   Main Site
  */
 namespace VuFind\Search\Tags;
-use VuFind\Record\Loader as RecordLoader, VuFind\Search\Base\Results as BaseResults;
+use VuFind\Search\Base\Results as BaseResults;
 
 /**
  * Search Tags Results
@@ -73,7 +73,8 @@ class Results extends BaseResults
             $recordsToRequest[]
                 = array('id' => $row->record_id, 'source' => $row->source);
         }
-        $this->results = RecordLoader::getInstance()->loadBatch($recordsToRequest);
+        $this->results = $this->getServiceLocator()->get('RecordLoader')
+            ->loadBatch($recordsToRequest);
     }
 
     /**
-- 
GitLab