From fcd48244313790a114b317a9ba4e08ccb9856ea9 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 1 Mar 2013 14:28:07 -0500
Subject: [PATCH] Moved Config\Reader functionality into Config\PluginFactory;
 adjusted corresponding test case.

---
 .../src/VuFind/Config/PluginFactory.php       |  81 +++++++++-
 module/VuFind/src/VuFind/Config/Reader.php    | 141 ------------------
 .../{ReaderTest.php => PluginFactoryTest.php} |  46 +++++-
 3 files changed, 117 insertions(+), 151 deletions(-)
 delete mode 100644 module/VuFind/src/VuFind/Config/Reader.php
 rename module/VuFind/tests/unit-tests/src/Config/{ReaderTest.php => PluginFactoryTest.php} (83%)

diff --git a/module/VuFind/src/VuFind/Config/PluginFactory.php b/module/VuFind/src/VuFind/Config/PluginFactory.php
index bf5f3c3ae7e..cc2aec3054f 100644
--- a/module/VuFind/src/VuFind/Config/PluginFactory.php
+++ b/module/VuFind/src/VuFind/Config/PluginFactory.php
@@ -26,7 +26,8 @@
  * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
  */
 namespace VuFind\Config;
-use Zend\ServiceManager\AbstractFactoryInterface,
+use Zend\Config\Config, Zend\Config\Reader\Ini as IniReader,
+    Zend\ServiceManager\AbstractFactoryInterface,
     Zend\ServiceManager\ServiceLocatorInterface;
 
 /**
@@ -40,6 +41,82 @@ use Zend\ServiceManager\AbstractFactoryInterface,
  */
 class PluginFactory implements AbstractFactoryInterface
 {
+    /**
+     * .ini reader
+     *
+     * @var IniReader
+     */
+    protected $iniReader;
+
+    /**
+     * Constructor
+     */
+    public function __construct()
+    {
+        // Use ASCII 0 as a nest separator; otherwise some of the unusual key names
+        // we have (i.e. in WorldCat.ini search options) will get parsed in
+        // unexpected ways.
+        $this->iniReader = new IniReader();
+        $this->iniReader->setNestSeparator(chr(0));
+    }
+
+    /**
+     * Load the specified configuration file.
+     *
+     * @param string $filename config file name
+     * @param string $path     path relative to VuFind base (optional; defaults
+     * to config/vufind
+     *
+     * @return \Zend\Config\Config
+     */
+    protected function loadConfigFile($filename, $path = 'config/vufind')
+    {
+        $configs = array();
+
+        $fullpath = Locator::getConfigPath($filename, $path);
+
+        // Retrieve and parse at least one configuration file, and possibly a whole
+        // chain of them if the Parent_Config setting is used:
+        do {
+            $configs[]
+                = new Config($this->iniReader->fromFile($fullpath), true);
+
+            $i = count($configs) - 1;
+            $fullpath = isset($configs[$i]->Parent_Config->path)
+                ? $configs[$i]->Parent_Config->path : false;
+        } while ($fullpath);
+
+        // The last element in the array will be the top of the inheritance tree.
+        // Let's establish a baseline:
+        $config = array_pop($configs);
+
+        // Now we'll pull all the children down one at a time and override settings
+        // as appropriate:
+        while (!is_null($child = array_pop($configs))) {
+            $overrideSections = isset($child->Parent_Config->override_full_sections)
+                ? explode(
+                    ',', str_replace(
+                        ' ', '', $child->Parent_Config->override_full_sections
+                    )
+                )
+                : array();
+            foreach ($child as $section => $contents) {
+                if (in_array($section, $overrideSections)
+                    || !isset($config->$section)
+                ) {
+                    $config->$section = $child->$section;
+                } else {
+                    foreach ($contents as $key => $value) {
+                        $config->$section->$key = $child->$section->$key;
+                    }
+                }
+            }
+        }
+
+        $config->setReadOnly();
+        return $config;
+    }
+
     /**
      * Can we create a service for the specified name?
      *
@@ -68,6 +145,6 @@ class PluginFactory implements AbstractFactoryInterface
     public function createServiceWithName(ServiceLocatorInterface $serviceLocator,
         $name, $requestedName
     ) {
-        return \VuFind\Config\Reader::getConfig($requestedName, true);
+        return $this->loadConfigFile($requestedName . '.ini');
     }
 }
diff --git a/module/VuFind/src/VuFind/Config/Reader.php b/module/VuFind/src/VuFind/Config/Reader.php
deleted file mode 100644
index db47b01fd3f..00000000000
--- a/module/VuFind/src/VuFind/Config/Reader.php
+++ /dev/null
@@ -1,141 +0,0 @@
-<?php
-/**
- * VF Configuration Reader
- *
- * 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  Config
- * @author   Demian Katz <demian.katz@villanova.edu>
- * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     http://vufind.org   Main Site
- */
-namespace VuFind\Config;
-use Zend\Config\Config, Zend\Config\Reader\Ini as IniReader;
-
-/**
- * Class to digest VuFind configuration settings
- *
- * @category VuFind2
- * @package  Config
- * @author   Demian Katz <demian.katz@villanova.edu>
- * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     http://vufind.org   Main Site
- */
-class Reader
-{
-    protected static $configs = array();
-
-    /**
-     * Load the proper config file
-     *
-     * @param string $name        Config file name (no .ini; null for main config)
-     * @param bool   $forceReload Reload config from disk even if already in cache
-     *
-     * @return \Zend\Config\Config
-     */
-    public static function getConfig($name = null, $forceReload = false)
-    {
-        if (is_null($name)) {
-            $name = 'config';
-        }
-        // Not already cached?  Load it now.
-        if ($forceReload || !isset(self::$configs[$name])) {
-            self::$configs[$name] = self::loadConfigFile($name . '.ini');
-        }
-        return self::$configs[$name];
-    }
-
-    /**
-     * Get the Ini Reader.
-     *
-     * @return \Zend\Config\Reader\Ini
-     */
-    protected static function getIniReader()
-    {
-        static $iniReader = false;
-
-        // Set up reader if it is not already present in the static variable:
-        if (!$iniReader) {
-            // Use ASCII 0 as a nest separator; otherwise some of the unusual
-            // key names we have (i.e. in WorldCat.ini search options) will get
-            // parsed in unexpected ways.
-            $iniReader = new IniReader();
-            $iniReader->setNestSeparator(chr(0));
-        }
-
-        return $iniReader;
-    }
-
-    /**
-     * Load the specified configuration file.
-     *
-     * @param string $filename config file name
-     * @param string $path     path relative to VuFind base (optional; defaults
-     * to config/vufind
-     *
-     * @return \Zend\Config\Config
-     */
-    public static function loadConfigFile($filename, $path = 'config/vufind')
-    {
-        $configs = array();
-
-        $fullpath = Locator::getConfigPath($filename, $path);
-
-        // Retrieve and parse at least one configuration file, and possibly a whole
-        // chain of them if the Parent_Config setting is used:
-        do {
-            $configs[]
-                = new Config(static::getIniReader()->fromFile($fullpath), true);
-
-            $i = count($configs) - 1;
-            $fullpath = isset($configs[$i]->Parent_Config->path)
-                ? $configs[$i]->Parent_Config->path : false;
-        } while ($fullpath);
-
-        // The last element in the array will be the top of the inheritance tree.
-        // Let's establish a baseline:
-        $config = array_pop($configs);
-
-        // Now we'll pull all the children down one at a time and override settings
-        // as appropriate:
-        while (!is_null($child = array_pop($configs))) {
-            $overrideSections = isset($child->Parent_Config->override_full_sections)
-                ? explode(
-                    ',', str_replace(
-                        ' ', '', $child->Parent_Config->override_full_sections
-                    )
-                )
-                : array();
-            foreach ($child as $section => $contents) {
-                if (in_array($section, $overrideSections)
-                    || !isset($config->$section)
-                ) {
-                    $config->$section = $child->$section;
-                } else {
-                    foreach ($contents as $key => $value) {
-                        $config->$section->$key = $child->$section->$key;
-                    }
-                }
-            }
-        }
-
-        $config->setReadOnly();
-        return $config;
-    }
-}
\ No newline at end of file
diff --git a/module/VuFind/tests/unit-tests/src/Config/ReaderTest.php b/module/VuFind/tests/unit-tests/src/Config/PluginFactoryTest.php
similarity index 83%
rename from module/VuFind/tests/unit-tests/src/Config/ReaderTest.php
rename to module/VuFind/tests/unit-tests/src/Config/PluginFactoryTest.php
index d34bd7aba14..36817e66d61 100644
--- a/module/VuFind/tests/unit-tests/src/Config/ReaderTest.php
+++ b/module/VuFind/tests/unit-tests/src/Config/PluginFactoryTest.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * Config Reader Test Class
+ * Config Factory Test Class
  *
  * PHP version 5
  *
@@ -26,10 +26,10 @@
  * @link     http://vufind.org/wiki/vufind2:unit_tests Wiki
  */
 namespace VuFindTest\Config;
-use VuFind\Config\Locator, VuFind\Config\Reader;
+use VuFind\Config\Locator;
 
 /**
- * Config Reader Test Class
+ * Config Factory Test Class
  *
  * @category VuFind2
  * @package  Tests
@@ -38,7 +38,7 @@ use VuFind\Config\Locator, VuFind\Config\Reader;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/vufind2:unit_tests Wiki
  */
-class ReaderTest extends \VuFindTest\Unit\TestCase
+class PluginFactoryTest extends \VuFindTest\Unit\TestCase
 {
     /**
      * Flag -- did writing config files fail?
@@ -54,6 +54,13 @@ class ReaderTest extends \VuFindTest\Unit\TestCase
      */
     protected static $filesToDelete = array();
 
+    /**
+     * Plugin factory instance.
+     *
+     * @var \VuFind\Config\PluginFactory
+     */
+    protected $factory;
+
     /**
      * Standard setup method.
      *
@@ -91,6 +98,29 @@ class ReaderTest extends \VuFindTest\Unit\TestCase
         self::$filesToDelete = array($parentPath, $childPath);
     }
 
+    /**
+     * Constructor
+     */
+    public function __construct()
+    {
+        $this->factory = new \VuFind\Config\PluginFactory();
+    }
+
+    /**
+     * Wrapper around factory
+     *
+     * @param string $name Configuration to load
+     *
+     * @return \Zend\Config\Config
+     */
+    protected function getConfig($name)
+    {
+        return $this->factory->createServiceWithName(
+            $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'),
+            $name, $name
+        );
+    }
+
     /**
      * Test basic config.ini loading.
      *
@@ -100,7 +130,7 @@ class ReaderTest extends \VuFindTest\Unit\TestCase
     {
         // This should retrieve config.ini, which should have "Library Catalog"
         // set as the default system title.
-        $config = Reader::getConfig();
+        $config = $this->getConfig('config');
         $this->assertEquals('Library Catalog', $config->Site->title);
     }
 
@@ -112,7 +142,7 @@ class ReaderTest extends \VuFindTest\Unit\TestCase
     public function testCustomRead()
     {
         // This should retrieve sms.ini, which should include a Carriers array.
-        $config = Reader::getConfig('sms');
+        $config = $this->getConfig('sms');
         $this->assertTrue(isset($config->Carriers) && count($config->Carriers) > 0);
     }
 
@@ -128,7 +158,7 @@ class ReaderTest extends \VuFindTest\Unit\TestCase
         }
 
         // Make sure load succeeds:
-        $config = Reader::getConfig('unit-test-child');
+        $config = $this->getConfig('unit-test-child');
         $this->assertTrue(is_object($config));
 
         // Make sure Section 1 was overridden; values from parent should not be
@@ -155,7 +185,7 @@ class ReaderTest extends \VuFindTest\Unit\TestCase
         if (self::$writeFailed) {
             $this->markTestSkipped('Could not write test configurations.');
         }
-        $config = Reader::getConfig('unit-test-parent');
+        $config = $this->getConfig('unit-test-parent');
         $this->setExpectedException('Zend\Config\Exception\RuntimeException');
         $config->Section1->z = 'bad';
     }
-- 
GitLab