From c6c1b9f4796da739c13d04d21a136313ae3f5a58 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 7 Jan 2013 13:29:14 -0500
Subject: [PATCH] Eliminated view helpers' dependencies on service locator; use
 dependency injection instead.

---
 .../Helper/Root/AbstractServiceLocator.php    | 76 -------------------
 .../src/VuFind/View/Helper/Root/Auth.php      | 21 ++++-
 .../src/VuFind/View/Helper/Root/Cart.php      | 21 ++++-
 .../Helper/Root/DisplayLanguageOption.php     | 32 ++++----
 .../src/VuFind/View/Helper/Root/Ils.php       | 22 +++++-
 .../VuFind/View/Helper/Root/RecordLink.php    | 22 ++++--
 .../VuFind/View/Helper/Root/SearchOptions.php | 22 +++++-
 themes/root/theme.config.php                  | 38 ++++++++--
 8 files changed, 136 insertions(+), 118 deletions(-)
 delete mode 100644 module/VuFind/src/VuFind/View/Helper/Root/AbstractServiceLocator.php

diff --git a/module/VuFind/src/VuFind/View/Helper/Root/AbstractServiceLocator.php b/module/VuFind/src/VuFind/View/Helper/Root/AbstractServiceLocator.php
deleted file mode 100644
index 5c944ad49fb..00000000000
--- a/module/VuFind/src/VuFind/View/Helper/Root/AbstractServiceLocator.php
+++ /dev/null
@@ -1,76 +0,0 @@
-<?php
-/**
- * Base class for helpers that pull resources from the service locator.
- *
- * PHP version 5
- *
- * Copyright (C) Villanova University 2010.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * @category VuFind2
- * @package  View_Helpers
- * @author   Demian Katz <demian.katz@villanova.edu>
- * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
- */
-namespace VuFind\View\Helper\Root;
-use Zend\ServiceManager\ServiceLocatorInterface,
-    Zend\ServiceManager\ServiceLocatorAwareInterface,
-    Zend\View\Helper\AbstractHelper;
-
-/**
- * Base class for helpers that pull resources from the service locator.
- *
- * @category VuFind2
- * @package  View_Helpers
- * @author   Demian Katz <demian.katz@villanova.edu>
- * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
- */
-abstract class AbstractServiceLocator extends AbstractHelper
-    implements ServiceLocatorAwareInterface
-{
-    /**
-     * Service locator
-     *
-     * @var ServiceLocatorInterface
-     */
-    protected $serviceLocator;
-
-    /**
-     * Set the service locator.
-     *
-     * @param ServiceLocatorInterface $serviceLocator Locator to register
-     *
-     * @return AbstractServiceLocator
-     */
-    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
-    {
-        // The service locator passed in here is a Zend\View\HelperPluginManager;
-        // we want to pull out the main Zend\ServiceManager\ServiceManager.
-        $this->serviceLocator = $serviceLocator->getServiceLocator();
-        return $this;
-    }
-
-    /**
-     * Get the service locator.
-     *
-     * @return \Zend\ServiceManager\ServiceLocatorInterface
-     */
-    public function getServiceLocator()
-    {
-        return $this->serviceLocator;
-    }
-}
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Auth.php b/module/VuFind/src/VuFind/View/Helper/Root/Auth.php
index 52bf19d4d03..85d569007bc 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Auth.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Auth.php
@@ -37,8 +37,25 @@ use Zend\View\Exception\RuntimeException;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class Auth extends AbstractServiceLocator
+class Auth extends \Zend\View\Helper\AbstractHelper
 {
+    /**
+     * Authentication manager
+     *
+     * @var \VuFind\Auth\Manager
+     */
+    protected $manager;
+
+    /**
+     * Constructor
+     *
+     * @param \VuFind\Auth\Manager $manager Authentication manager
+     */
+    public function __construct(\VuFind\Auth\Manager $manager)
+    {
+        $this->manager = $manager;
+    }
+
     /**
      * Render a template within an auth module folder.
      *
@@ -89,7 +106,7 @@ class Auth extends AbstractServiceLocator
      */
     public function getManager()
     {
-        return $this->getServiceLocator()->get('VuFind\AuthManager');
+        return $this->manager;
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Cart.php b/module/VuFind/src/VuFind/View/Helper/Root/Cart.php
index 788bfdf607d..6aca9f50935 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Cart.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Cart.php
@@ -36,8 +36,25 @@ namespace VuFind\View\Helper\Root;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class Cart extends AbstractServiceLocator
+class Cart extends \Zend\View\Helper\AbstractHelper
 {
+    /**
+     * VuFind Cart Model
+     *
+     * @var \VuFind\Cart
+     */
+    protected $cart;
+
+    /**
+     * Constructor
+     *
+     * @param \VuFind\Cart $cart Cart model
+     */
+    public function __construct(\VuFind\Cart $cart)
+    {
+        $this->cart = $cart;
+    }
+
     /**
      * Get the Cart object from the service manager.
      *
@@ -45,6 +62,6 @@ class Cart extends AbstractServiceLocator
      */
     public function __invoke()
     {
-        return $this->getServiceLocator()->get('VuFind\Cart');
+        return $this->cart;
     }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOption.php b/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOption.php
index b4539e01071..6d6278296bf 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOption.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOption.php
@@ -36,7 +36,7 @@ namespace VuFind\View\Helper\Root;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class DisplayLanguageOption extends AbstractServiceLocator
+class DisplayLanguageOption extends \Zend\View\Helper\AbstractHelper
 {
     /**
      * Translator (or null if unavailable)
@@ -46,25 +46,21 @@ class DisplayLanguageOption extends AbstractServiceLocator
     protected $translator = null;
 
     /**
-     * Get translator object.
+     * Constructor
      *
-     * @return \Zend\I18n\Translator\Translator
+     * @param \Zend\I18n\Translator\Translator $translator Main VuFind translator
      */
-    public function getTranslator()
+    public function __construct(\Zend\I18n\Translator\Translator $translator)
     {
-        if (null === $this->translator) {
-            // Clone the translator; we need to switch language for the purposes
-            // of this plugin, but we don't want that change to happen globally.
-            $this->translator
-                = clone($this->getServiceLocator()->get('VuFind\Translator'));
-            $this->translator->addTranslationFile(
-                'ExtendedIni',
-                APPLICATION_PATH  . '/languages/native.ini',
-                'default', 'native'
-            );
-            $this->translator->setLocale('native');
-        }
-        return $this->translator;
+        // Clone the translator; we need to switch language for the purposes
+        // of this plugin, but we don't want that change to happen globally.
+        $this->translator = clone($translator);
+        $this->translator->addTranslationFile(
+            'ExtendedIni',
+            APPLICATION_PATH  . '/languages/native.ini',
+            'default', 'native'
+        );
+        $this->translator->setLocale('native');
     }
 
     /**
@@ -76,6 +72,6 @@ class DisplayLanguageOption extends AbstractServiceLocator
      */
     public function __invoke($str)
     {
-        return $this->view->escapeHtml($this->getTranslator()->translate($str));
+        return $this->view->escapeHtml($this->translator->translate($str));
     }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Ils.php b/module/VuFind/src/VuFind/View/Helper/Root/Ils.php
index d819a28efc9..7b917830445 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Ils.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Ils.php
@@ -26,7 +26,6 @@
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
 namespace VuFind\View\Helper\Root;
-use Zend\View\Helper\AbstractHelper;
 
 /**
  * ILS (integrated library system) view helper
@@ -37,8 +36,25 @@ use Zend\View\Helper\AbstractHelper;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class Ils extends AbstractServiceLocator
+class Ils extends \Zend\View\Helper\AbstractHelper
 {
+    /**
+     * ILS connection
+     *
+     * @var \VuFind\ILS\Connection
+     */
+    protected $connection;
+
+    /**
+     * Constructor
+     *
+     * @param \VuFind\ILS\Connection $connection ILS connection
+     */
+    public function __construct(\VuFind\ILS\Connection $connection)
+    {
+        $this->connection = $connection;
+    }
+
     /**
      * Get the ILS connection object.
      *
@@ -46,6 +62,6 @@ class Ils extends AbstractServiceLocator
      */
     public function __invoke()
     {
-        return $this->getServiceLocator()->get('VuFind\ILSConnection');
+        return $this->connection;
     }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/RecordLink.php b/module/VuFind/src/VuFind/View/Helper/Root/RecordLink.php
index 9060f5a7b38..58870015397 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/RecordLink.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/RecordLink.php
@@ -26,7 +26,6 @@
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
 namespace VuFind\View\Helper\Root;
-use Zend\View\Helper\AbstractHelper;
 
 /**
  * Record link view helper
@@ -37,16 +36,23 @@ use Zend\View\Helper\AbstractHelper;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class RecordLink extends AbstractServiceLocator
+class RecordLink extends \Zend\View\Helper\AbstractHelper
 {
     /**
-     * Get the record router.
+     * Record router
      *
-     * @return \VuFind\Record\Router
+     * @var \VuFind\Record\Router
      */
-    protected function getRecordRouter()
+    protected $router;
+
+    /**
+     * Constructor
+     *
+     * @param \VuFind\Record\Router $router Record router
+     */
+    public function __construct(\VuFind\Record\Router $router)
     {
-        return $this->getServiceLocator()->get('VuFind\RecordRouter');
+        $this->router = $router;
     }
 
     /**
@@ -110,7 +116,7 @@ class RecordLink extends AbstractServiceLocator
     {
         // Build the URL:
         $urlHelper = $this->getView()->plugin('url');
-        $details = $this->getRecordRouter()->getActionRouteDetails($driver, $action);
+        $details = $this->router->getActionRouteDetails($driver, $action);
         return $urlHelper($details['route'], $details['params']);
     }
 
@@ -162,7 +168,7 @@ class RecordLink extends AbstractServiceLocator
     {
         // Build the URL:
         $urlHelper = $this->getView()->plugin('url');
-        $details = $this->getRecordRouter()->getTabRouteDetails($driver, $tab);
+        $details = $this->router->getTabRouteDetails($driver, $tab);
         return $urlHelper($details['route'], $details['params']);
     }
 
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/SearchOptions.php b/module/VuFind/src/VuFind/View/Helper/Root/SearchOptions.php
index d8b68532d23..a10692c6b8f 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/SearchOptions.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/SearchOptions.php
@@ -36,8 +36,25 @@ namespace VuFind\View\Helper\Root;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class SearchOptions extends AbstractServiceLocator
+class SearchOptions extends \Zend\View\Helper\AbstractHelper
 {
+    /**
+     * Search manager
+     *
+     * @var \VuFind\Search\Manager
+     */
+    protected $manager;
+
+    /**
+     * Constructor
+     *
+     * @param \VuFind\Search\Manager $manager Search manager
+     */
+    public function __construct(\VuFind\Search\Manager $manager)
+    {
+        $this->manager = $manager;
+    }
+
     /**
      * Wrapper to the search manager's getOptionsInstance method
      *
@@ -47,7 +64,6 @@ class SearchOptions extends AbstractServiceLocator
      */
     public function __invoke($type = 'Solr')
     {
-        return $this->getServiceLocator()->get('SearchManager')
-            ->setSearchClassId($type)->getOptionsInstance();
+        return $this->manager->setSearchClassId($type)->getOptionsInstance();
     }
 }
\ No newline at end of file
diff --git a/themes/root/theme.config.php b/themes/root/theme.config.php
index 3adaa6c4a68..c0730b447c2 100644
--- a/themes/root/theme.config.php
+++ b/themes/root/theme.config.php
@@ -2,35 +2,61 @@
 return array(
     'extends' => false,
     'helpers' => array(
+        'factories' => array(
+            'auth' => function ($sm) {
+                return new \VuFind\View\Helper\Root\Auth(
+                    $sm->getServiceLocator()->get('VuFind\AuthManager')
+                );
+            },
+            'cart' => function ($sm) {
+                return new \VuFind\View\Helper\Root\Cart(
+                    $sm->getServiceLocator()->get('VuFind\Cart')
+                );
+            },
+            'displaylanguageoption' => function ($sm) {
+                return new VuFind\View\Helper\Root\DisplayLanguageOption(
+                    $sm->getServiceLocator()->get('VuFind\Translator')
+                );
+            },
+            'ils' => function ($sm) {
+                return new \VuFind\View\Helper\Root\Ils(
+                    $sm->getServiceLocator()->get('VuFind\ILSConnection')
+                );
+            },
+            'recordlink' => function ($sm) {
+                return new \VuFind\View\Helper\Root\RecordLink(
+                    $sm->getServiceLocator()->get('VuFind\RecordRouter')
+                );
+            },
+            'searchoptions' => function ($sm) {
+                return new VuFind\View\Helper\Root\SearchOptions(
+                    $sm->getServiceLocator()->get('SearchManager')
+                );
+            },
+        ),
         'invokables' => array(
             'addellipsis' => 'VuFind\View\Helper\Root\AddEllipsis',
             'addthis' => 'VuFind\View\Helper\Root\AddThis',
-            'auth' => 'VuFind\View\Helper\Root\Auth',
             'authornotes' => 'VuFind\View\Helper\Root\AuthorNotes',
             'browse' => 'VuFind\View\Helper\Root\Browse',
-            'cart' => 'VuFind\View\Helper\Root\Cart',
             'citation' => 'VuFind\View\Helper\Root\Citation',
             'context' => 'VuFind\View\Helper\Root\Context',
             'currentpath' => 'VuFind\View\Helper\Root\CurrentPath',
             'datetime' => 'VuFind\View\Helper\Root\DateTime',
-            'displaylanguageoption' => 'VuFind\View\Helper\Root\DisplayLanguageOption',
             'excerpt' => 'VuFind\View\Helper\Root\Excerpt',
             'flashmessages' => 'VuFind\View\Helper\Root\Flashmessages',
             'getlastsearchlink' => 'VuFind\View\Helper\Root\GetLastSearchLink',
             'highlight' => 'VuFind\View\Helper\Root\Highlight',
-            'ils' => 'VuFind\View\Helper\Root\Ils',
             'jqueryvalidation' => 'VuFind\View\Helper\Root\JqueryValidation',
             'openurl' => 'VuFind\View\Helper\Root\OpenUrl',
             'printms' => 'VuFind\View\Helper\Root\Printms',
             'proxyurl' => 'VuFind\View\Helper\Root\ProxyUrl',
             'recommend' => 'VuFind\View\Helper\Root\Recommend',
             'record' => 'VuFind\View\Helper\Root\Record',
-            'recordlink' => 'VuFind\View\Helper\Root\RecordLink',
             'related' => 'VuFind\View\Helper\Root\Related',
             'renderarray' => 'VuFind\View\Helper\Root\RenderArray',
             'resultfeed' => 'VuFind\View\Helper\Root\ResultFeed',
             'reviews' => 'VuFind\View\Helper\Root\Reviews',
-            'searchoptions' => 'VuFind\View\Helper\Root\SearchOptions',
             'safemoneyformat' => 'VuFind\View\Helper\Root\SafeMoneyFormat',
             'sortfacetlist' => 'VuFind\View\Helper\Root\SortFacetList',
             'summon' => 'VuFind\View\Helper\Root\Summon',
-- 
GitLab