From 0c34784f62813d1a403e1e5fc0ba364ce940230e Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 4 Jan 2013 14:18:42 -0500
Subject: [PATCH] Moved theme-related code into a separate module.

---
 config/application.config.php                 |  2 +-
 module/VuFind/config/module.config.php        |  6 --
 module/VuFind/src/VuFind/Bootstrap.php        |  4 +-
 module/VuFind/src/VuFind/Cover/Loader.php     | 12 +--
 .../src/VuFind/Theme/Root/Helper/HeadLink.php |  2 +-
 .../VuFind/Theme/Root/Helper/HeadScript.php   |  2 +-
 .../Theme/Root/Helper/HeadThemeResources.php  |  2 +-
 .../VuFind/Theme/Root/Helper/ImageLink.php    |  2 +-
 .../VuFind/Theme/Root/Helper/MobileUrl.php    |  8 +-
 module/VuFindTheme/Module.php                 | 97 +++++++++++++++++++
 .../src/VuFindTheme}/Initializer.php          | 23 +++--
 .../VuFindTheme}/InjectTemplateListener.php   |  2 +-
 .../src/VuFindTheme}/Mobile.php               | 31 ++++--
 .../src/VuFindTheme}/ResourceContainer.php    | 29 +++++-
 .../src/VuFindTheme}/ThemeInfo.php            |  2 +-
 15 files changed, 181 insertions(+), 43 deletions(-)
 create mode 100644 module/VuFindTheme/Module.php
 rename module/{VuFind/src/VuFind/Theme => VuFindTheme/src/VuFindTheme}/Initializer.php (96%)
 rename module/{VuFind/src/VuFind/Mvc/View/Http => VuFindTheme/src/VuFindTheme}/InjectTemplateListener.php (98%)
 rename module/{VuFind/src/VuFind => VuFindTheme/src/VuFindTheme}/Mobile.php (81%)
 rename module/{VuFind/src/VuFind/Theme => VuFindTheme/src/VuFindTheme}/ResourceContainer.php (89%)
 rename module/{VuFind/src/VuFind/Theme => VuFindTheme/src/VuFindTheme}/ThemeInfo.php (99%)

diff --git a/config/application.config.php b/config/application.config.php
index c7800de4654..bd737f1eb2b 100644
--- a/config/application.config.php
+++ b/config/application.config.php
@@ -1,7 +1,7 @@
 <?php
 $config = array(
     'modules' => array(
-        'VuFindHttp', 'VuFind',
+        'VuFindHttp', 'VuFindTheme', 'VuFind',
     ),
     'module_listener_options' => array( 
         'config_glob_paths'    => array(
diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 60c3c7cb84a..73685d986ac 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -480,11 +480,6 @@ $config = array(
                     $sm->get('VuFind\Http')->createClient()
                 );
             },
-            'VuFindTheme\ThemeInfo' => function ($sm) {
-                return new \VuFind\Theme\ThemeInfo(
-                    realpath(__DIR__ . '/../../../themes'), 'blueprint'
-                );
-            }
         ),
         'invokables' => array(
             'VuFind\AuthManager' => 'VuFind\Auth\Manager',
@@ -494,7 +489,6 @@ $config = array(
             'VuFind\SearchSpecsReader' => 'VuFind\Config\SearchSpecsReader',
             'VuFind\SessionManager' => 'Zend\Session\SessionManager',
             'VuFind\WorldCatUtils' => 'VuFind\Connection\WorldCatUtils',
-            'VuFindTheme\ResourceContainer' => 'VuFind\Theme\ResourceContainer',
         ),
         'initializers' => array(
             array('VuFind\ServiceManager\Initializer', 'initInstance'),
diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php
index 63b21e44c91..3f0d23c8155 100644
--- a/module/VuFind/src/VuFind/Bootstrap.php
+++ b/module/VuFind/src/VuFind/Bootstrap.php
@@ -301,14 +301,14 @@ class Bootstrap
 
         // Attach template injection configuration to the route event:
         $this->events->attach(
-            'route', array('VuFind\Theme\Initializer', 'configureTemplateInjection')
+            'route', array('VuFindTheme\Initializer', 'configureTemplateInjection')
         );
 
         // Attach remaining theme configuration to the dispatch event at high
         // priority (TODO: use priority constant once defined by framework):
         $config = $this->config->Site;
         $callback = function ($event) use ($config) {
-            $theme = new \VuFind\Theme\Initializer($config, $event);
+            $theme = new \VuFindTheme\Initializer($config, $event);
             $theme->init();
         };
         $this->events->attach('dispatch.error', $callback, 10000);
diff --git a/module/VuFind/src/VuFind/Cover/Loader.php b/module/VuFind/src/VuFind/Cover/Loader.php
index 74fed7a3283..3f03e9cc237 100644
--- a/module/VuFind/src/VuFind/Cover/Loader.php
+++ b/module/VuFind/src/VuFind/Cover/Loader.php
@@ -121,20 +121,20 @@ class Loader implements \Zend\Log\LoggerAwareInterface
     /**
      * Theme tools
      *
-     * @var \VuFind\Theme\ThemeInfo
+     * @var \VuFindTheme\ThemeInfo
      */
     protected $themeTools;
 
     /**
      * Constructor
      *
-     * @param \Zend\Config\Config     $config  VuFind configuration
-     * @param \VuFind\Theme\ThemeInfo $theme   VuFind theme tools
-     * @param \Zend\Http\Client       $client  HTTP client
-     * @param string                  $baseDir Directory to store downloaded images
+     * @param \Zend\Config\Config    $config  VuFind configuration
+     * @param \VuFindTheme\ThemeInfo $theme   VuFind theme tools
+     * @param \Zend\Http\Client      $client  HTTP client
+     * @param string                 $baseDir Directory to store downloaded images
      * (set to system temp dir if not otherwise specified)
      */
-    public function __construct($config, \VuFind\Theme\ThemeInfo $theme,
+    public function __construct($config, \VuFindTheme\ThemeInfo $theme,
         \Zend\Http\Client $client, $baseDir = null
     ) {
         $this->config = $config;
diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/HeadLink.php b/module/VuFind/src/VuFind/Theme/Root/Helper/HeadLink.php
index 5beeac2f1df..ad7910271b7 100644
--- a/module/VuFind/src/VuFind/Theme/Root/Helper/HeadLink.php
+++ b/module/VuFind/src/VuFind/Theme/Root/Helper/HeadLink.php
@@ -76,7 +76,7 @@ class HeadLink extends \Zend\View\Helper\HeadLink
     /**
      * Get the theme tools.
      *
-     * @return \VuFind\Theme\ThemeInfo
+     * @return \VuFindTheme\ThemeInfo
      */
     public function getThemeInfo()
     {
diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/HeadScript.php b/module/VuFind/src/VuFind/Theme/Root/Helper/HeadScript.php
index ba7994c6468..830240c5561 100644
--- a/module/VuFind/src/VuFind/Theme/Root/Helper/HeadScript.php
+++ b/module/VuFind/src/VuFind/Theme/Root/Helper/HeadScript.php
@@ -76,7 +76,7 @@ class HeadScript extends \Zend\View\Helper\HeadScript
     /**
      * Get the theme tools.
      *
-     * @return \VuFind\Theme\ThemeInfo
+     * @return \VuFindTheme\ThemeInfo
      */
     public function getThemeInfo()
     {
diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/HeadThemeResources.php b/module/VuFind/src/VuFind/Theme/Root/Helper/HeadThemeResources.php
index fa8fb9229a2..31abed9e5db 100644
--- a/module/VuFind/src/VuFind/Theme/Root/Helper/HeadThemeResources.php
+++ b/module/VuFind/src/VuFind/Theme/Root/Helper/HeadThemeResources.php
@@ -42,7 +42,7 @@ class HeadThemeResources extends AbstractServiceLocator
     /**
      * Get the theme resource container.
      *
-     * @return \VuFind\Theme\ResourceContainer
+     * @return \VuFindTheme\ResourceContainer
      */
     public function getThemeResourceContainer()
     {
diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/ImageLink.php b/module/VuFind/src/VuFind/Theme/Root/Helper/ImageLink.php
index 1c05d7fa6d2..a73ad8836f9 100644
--- a/module/VuFind/src/VuFind/Theme/Root/Helper/ImageLink.php
+++ b/module/VuFind/src/VuFind/Theme/Root/Helper/ImageLink.php
@@ -42,7 +42,7 @@ class ImageLink extends AbstractServiceLocator
     /**
      * Get the theme tools.
      *
-     * @return \VuFind\Theme\ThemeInfo
+     * @return \VuFindTheme\ThemeInfo
      */
     public function getThemeInfo()
     {
diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/MobileUrl.php b/module/VuFind/src/VuFind/Theme/Root/Helper/MobileUrl.php
index 68652496a5f..546b54943a5 100644
--- a/module/VuFind/src/VuFind/Theme/Root/Helper/MobileUrl.php
+++ b/module/VuFind/src/VuFind/Theme/Root/Helper/MobileUrl.php
@@ -26,8 +26,6 @@
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
 namespace VuFind\Theme\Root\Helper;
-use VuFind\Mobile,
-    Zend\View\Helper\AbstractHelper;
 
 /**
  * Mobile URL view helper
@@ -38,7 +36,7 @@ use VuFind\Mobile,
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class MobileUrl extends AbstractHelper
+class MobileUrl extends AbstractServiceLocator
 {
     /**
      * Return the mobile version of the current URL if the user is on a mobile device
@@ -48,9 +46,11 @@ class MobileUrl extends AbstractHelper
      */
     public function __invoke()
     {
+        $mobile = $this->getServiceLocator()->get('VuFindTheme\Mobile');
+
         // Do nothing special if we're not on a mobile device or no mobile theme is
         // enabled:
-        if (!Mobile::enabled() || !Mobile::detect()) {
+        if (!$mobile->enabled() || !$mobile->detect()) {
             return false;
         }
 
diff --git a/module/VuFindTheme/Module.php b/module/VuFindTheme/Module.php
new file mode 100644
index 00000000000..05c3f22b868
--- /dev/null
+++ b/module/VuFindTheme/Module.php
@@ -0,0 +1,97 @@
+<?php
+/**
+ * ZF2 module definition for the VuFind theme system.
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2013.
+ *
+ * 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  Theme
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://github.com/dmj/vf2-proxy
+ */
+namespace VuFindTheme;
+
+/**
+ * ZF2 module definition for the VuFind theme system.
+ *
+ * @category VuFind2
+ * @package  Theme
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://github.com/dmj/vf2-proxy
+ */
+class Module
+{
+    /**
+     * Get autoloader configuration
+     *
+     * @return void
+     */
+    public function getAutoloaderConfig()
+    {
+        return array(
+            'Zend\Loader\StandardAutoloader' => array(
+                'namespaces' => array(
+                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
+                ),
+            ),
+        );
+    }
+
+    /**
+     * Return service configuration.
+     *
+     * @return array
+     */
+    public function getServiceConfig()
+    {
+        return array(
+            'factories' => array(
+                'VuFindTheme\ThemeInfo' => function ($sm) {
+                    return new \VuFindTheme\ThemeInfo(
+                        realpath(__DIR__ . '/../../themes'), 'blueprint'
+                    );
+                }
+            ),
+            'invokables' => array(
+                'VuFindTheme\Mobile' => 'VuFindTheme\Mobile',
+                'VuFindTheme\ResourceContainer' => 'VuFindTheme\ResourceContainer',
+            ),
+        );
+    }
+
+    /**
+     * Get view helper configuration.
+     *
+     * @return array
+     */
+    public function getViewHelperConfig()
+    {
+        return array();
+    }
+
+    /**
+     * Perform initialization
+     *
+     * @return void
+     */
+    public function init()
+    {
+    }
+}
diff --git a/module/VuFind/src/VuFind/Theme/Initializer.php b/module/VuFindTheme/src/VuFindTheme/Initializer.php
similarity index 96%
rename from module/VuFind/src/VuFind/Theme/Initializer.php
rename to module/VuFindTheme/src/VuFindTheme/Initializer.php
index 9688b240dae..0e3a1de337c 100644
--- a/module/VuFind/src/VuFind/Theme/Initializer.php
+++ b/module/VuFindTheme/src/VuFindTheme/Initializer.php
@@ -25,10 +25,8 @@
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org   Main Site
  */
-namespace VuFind\Theme;
-use VuFind\Mobile,
-    VuFind\Mvc\View\Http\InjectTemplateListener,
-    Zend\Config\Config,
+namespace VuFindTheme;
+use Zend\Config\Config,
     Zend\Mvc\MvcEvent,
     Zend\Stdlib\RequestInterface as Request;
 
@@ -67,10 +65,17 @@ class Initializer
     /**
      * Theme tools object
      *
-     * @var \VuFind\Theme\ThemeInfo
+     * @var \VuFindTheme\ThemeInfo
      */
     protected $tools;
 
+    /**
+     * Mobile interface detector
+     *
+     * @var \VuFindTheme\Mobile
+     */
+    protected $mobile;
+
     /**
      * Constructor
      *
@@ -100,6 +105,10 @@ class Initializer
 
         // Get base directory from tools object:
         $this->tools = $this->serviceManager->get('VuFindTheme\ThemeInfo');
+
+        // Set up mobile device detector:
+        $this->mobile = $this->serviceManager->get('VuFindTheme\Mobile');
+        $this->mobile->enable(isset($this->config->mobile_theme));
     }
 
     /**
@@ -193,7 +202,7 @@ class Initializer
     {
         // Load standard configuration options:
         $standardTheme = $this->config->theme;
-        $mobileTheme = isset($this->config->mobile_theme)
+        $mobileTheme = $this->mobile->enabled()
             ? $this->config->mobile_theme : false;
 
         // Find out if the user has a saved preference in the POST, URL or cookies:
@@ -204,7 +213,7 @@ class Initializer
             )
         );
         if (empty($selectedUI)) {
-            $selectedUI = ($mobileTheme && Mobile::detect())
+            $selectedUI = ($mobileTheme && $this->mobile->detect())
                 ? 'mobile' : 'standard';
         }
 
diff --git a/module/VuFind/src/VuFind/Mvc/View/Http/InjectTemplateListener.php b/module/VuFindTheme/src/VuFindTheme/InjectTemplateListener.php
similarity index 98%
rename from module/VuFind/src/VuFind/Mvc/View/Http/InjectTemplateListener.php
rename to module/VuFindTheme/src/VuFindTheme/InjectTemplateListener.php
index 53dce086128..d8a6e60c9bb 100644
--- a/module/VuFind/src/VuFind/Mvc/View/Http/InjectTemplateListener.php
+++ b/module/VuFindTheme/src/VuFindTheme/InjectTemplateListener.php
@@ -25,7 +25,7 @@
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org   Main Site
  */
-namespace VuFind\Mvc\View\Http;
+namespace VuFindTheme;
 
 /**
  * VuFind "Inject Template" Listener -- this extends the core ZF2 class to adjust
diff --git a/module/VuFind/src/VuFind/Mobile.php b/module/VuFindTheme/src/VuFindTheme/Mobile.php
similarity index 81%
rename from module/VuFind/src/VuFind/Mobile.php
rename to module/VuFindTheme/src/VuFindTheme/Mobile.php
index d8869fe9015..8433c90ef10 100644
--- a/module/VuFind/src/VuFind/Mobile.php
+++ b/module/VuFindTheme/src/VuFindTheme/Mobile.php
@@ -26,31 +26,37 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * @category VuFind2
- * @package  Mobile
+ * @package  Theme
  * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://code.google.com/p/mobileesp/ MobileESP Project
  */
-namespace VuFind;
-use VuFind\Config\Reader as ConfigReader;
+namespace VuFindTheme;
 
 /**
  * Mobile Device Detection Wrapper
  *
  * @category VuFind2
- * @package  Mobile
+ * @package  Theme
  * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://code.google.com/p/mobileesp/ MobileESP Project
  */
 class Mobile
 {
+    /**
+     * Are mobile themes enabled?
+     *
+     * @var bool
+     */
+    protected $enabled = false;
+
     /**
      * Function to detect if a mobile device is being used.
      *
      * @return bool
      */
-    public static function detect()
+    public function detect()
     {
         // Do the most exhaustive device detection possible; other method calls
         // may be used instead of DetectMobileLong if you want to target a narrower
@@ -59,14 +65,23 @@ class Mobile
         return $mobile->DetectMobileLong();
     }
 
+    /**
+     * Function to set enabled status of mobile themes.
+     *
+     * @param bool $enabled Are mobile themes enabled?
+     */
+    public function enable($enabled = true)
+    {
+        $this->enabled = $enabled;
+    }
+
     /**
      * Function to check whether mobile theme is configured.
      *
      * @return bool
      */
-    public static function enabled()
+    public function enabled()
     {
-        $config = ConfigReader::getConfig();
-        return isset($config->Site->mobile_theme);
+        return $this->enabled;
     }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Theme/ResourceContainer.php b/module/VuFindTheme/src/VuFindTheme/ResourceContainer.php
similarity index 89%
rename from module/VuFind/src/VuFind/Theme/ResourceContainer.php
rename to module/VuFindTheme/src/VuFindTheme/ResourceContainer.php
index 82a63aa11e5..2fcef78e932 100644
--- a/module/VuFind/src/VuFind/Theme/ResourceContainer.php
+++ b/module/VuFindTheme/src/VuFindTheme/ResourceContainer.php
@@ -25,7 +25,7 @@
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org   Main Site
  */
-namespace VuFind\Theme;
+namespace VuFindTheme;
 
 /**
  * VuFind Theme Public Resource Handler (for CSS, JS, etc.)
@@ -38,9 +38,32 @@ namespace VuFind\Theme;
  */
 class ResourceContainer
 {
+    /**
+     * CSS files
+     *
+     * @var array
+     */
     protected $css = array();
+
+    /**
+     * Javascript files
+     *
+     * @var array
+     */
     protected $js = array();
+
+    /**
+     * Favicon
+     *
+     * @var string
+     */
     protected $favicon = null;
+
+    /**
+     * Encoding type
+     *
+     * @var string
+     */
     protected $encoding = 'UTF-8';
 
     /**
@@ -53,7 +76,7 @@ class ResourceContainer
      */
     public function addCss($css)
     {
-        if (!is_array($css) && !is_a($css, '\Traversable')) {
+        if (!is_array($css) && !is_a($css, 'Traversable')) {
             $css = array($css);
         }
         foreach ($css as $current) {
@@ -71,7 +94,7 @@ class ResourceContainer
      */
     public function addJs($js)
     {
-        if (!is_array($js) && !is_a($js, '\Traversable')) {
+        if (!is_array($js) && !is_a($js, 'Traversable')) {
             $js = array($js);
         }
         foreach ($js as $current) {
diff --git a/module/VuFind/src/VuFind/Theme/ThemeInfo.php b/module/VuFindTheme/src/VuFindTheme/ThemeInfo.php
similarity index 99%
rename from module/VuFind/src/VuFind/Theme/ThemeInfo.php
rename to module/VuFindTheme/src/VuFindTheme/ThemeInfo.php
index aff13d6fb81..254c25f1a51 100644
--- a/module/VuFind/src/VuFind/Theme/ThemeInfo.php
+++ b/module/VuFindTheme/src/VuFindTheme/ThemeInfo.php
@@ -25,7 +25,7 @@
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org   Main Site
  */
-namespace VuFind\Theme;
+namespace VuFindTheme;
 
 /**
  * Class to represent currently-selected theme and related information.
-- 
GitLab