Преглед изворни кода

ZF-6111 - Merged behaviour to write INI options completly into global namespace with Zend_Config_Writer_Ini

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18867 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei пре 16 година
родитељ
комит
eb22fce51d

+ 6 - 13
documentation/manual/en/module_specs/Zend_Config_Writer.xml

@@ -6,7 +6,7 @@
         <classname>Zend_Config_Writer</classname> gives you the ability to write config
         files out of <classname>Zend_Config</classname> objects. It works with an
         adapter-less system and thus is very easy to use. By default
-        <classname>Zend_Config_Writer</classname> ships with four adapters, which are all
+        <classname>Zend_Config_Writer</classname> ships with three adapters, which are all
         file-based. You instantiate a writer with specific options, which
         can be <emphasis>filename</emphasis> and <emphasis>config</emphasis>. Then
         you call the <methodname>write()</methodname> method of the writer and the config
@@ -29,27 +29,20 @@
         </listitem>
         <listitem>
             <para>
-                <classname>Zend_Config_Writer_SimpleIni</classname>
-            </para>
-        </listitem>
-        <listitem>
-            <para>
                 <classname>Zend_Config_Writer_Xml</classname>
             </para>
         </listitem>
     </itemizedlist>
 
     <para>
-        The difference between SimpleIni and Ini writer is their handling
-        with regard to sections. The Ini writer always writes the top-level
-        config elements into section names. The SimpleIni writer in contrast
-        writes out a config file without and section. All options are written
-        into the global namespace of the INI file.
+        The Ini writer has two modes for rendering with regard to sections.
+        By default the top-level configuration is always written into section names.
+        By calling <code>$writer->setRenderWithoutSections()</code> all options are written
+        into the global namespace of the INI file and no sections are applied.
     </para>
 
     <para>
-        As an addition the two INI writers <classname>Zend_Config_Writer_Ini</classname>
-        and <classname>Zend_Config_Writer_SimpleINi</classname> have an additional
+        As an addition <classname>Zend_Config_Writer_Ini</classname> has an additional
         option parameter <emphasis>nestSeparator</emphasis>, which defines with which
         character the single nodes are separated. The default is a single dot,
         like it is accepted by <classname>Zend_Config_Ini</classname> by default.

+ 25 - 1
library/Zend/Config/Writer/Ini.php

@@ -40,6 +40,13 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer_FileAbstract
     protected $_nestSeparator = '.';
 
     /**
+     * If true the ini string is rendered in the global namespace without sections.
+     *
+     * @var bool
+     */
+    protected $_renderWithoutSections = false;
+
+    /**
      * Set the nest separator
      *
      * @param  string $filename
@@ -53,6 +60,21 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer_FileAbstract
     }
 
     /**
+     * Set if rendering should occour without sections or not.
+     *
+     * If set to true, the INI file is rendered without sections completely
+     * into the global namespace of the INI file.
+     *
+     * @param  bool $withoutSections
+     * @return Zend_Config_Writer_Ini
+     */
+    public function setRenderWithoutSections($withoutSections=true)
+    {
+        $this->_renderWithoutSections = (bool)$withoutSections;
+        return $this;
+    }
+
+    /**
      * Render a Zend_Config into a INI config string.
      *
      * @since 1.10
@@ -64,7 +86,9 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer_FileAbstract
         $extends     = $this->_config->getExtends();
         $sectionName = $this->_config->getSectionName();
 
-        if (is_string($sectionName)) {
+        if($this->_renderWithoutSections == true) {
+            $iniString .= $this->_addBranch($this->_config);
+        } else if (is_string($sectionName)) {
             $iniString .= '[' . $sectionName . ']' . "\n"
                        .  $this->_addBranch($this->_config)
                        .  "\n";

+ 0 - 47
library/Zend/Config/Writer/SimpleIni.php

@@ -1,47 +0,0 @@
-<?php
-/**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category   Zend
- * @package    Zend_Config
- * @subpackage Writer
- * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license    http://framework.zend.com/license/new-bsd     New BSD License
- */
-
-/**
- * @see Zend_Config_Writer
- */
-require_once 'Zend/Config/Writer/Ini.php';
-
-/**
- * Write a INI configuration without sections.
- *
- * @category   Zend
- * @package    Zend_Config
- * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license    http://framework.zend.com/license/new-bsd     New BSD License
- */
-class Zend_Config_Writer_SimpleIni extends Zend_Config_Writer_Ini
-{
-    /**
-     * Render a Zend_Config into a INI config string.
-     *
-     * @since 1.10
-     * @return string
-     */
-    public function render()
-    {
-        return $this->_addBranch($this->_config);
-    }
-}

+ 0 - 2
tests/Zend/Config/Writer/AllTests.php

@@ -31,7 +31,6 @@ if (!defined('PHPUnit_MAIN_METHOD')) {
 
 require_once 'Zend/Config/Writer/ArrayTest.php';
 require_once 'Zend/Config/Writer/IniTest.php';
-require_once 'Zend/Config/Writer/SimpleIniTest.php';
 require_once 'Zend/Config/Writer/XmlTest.php';
 
 /**
@@ -56,7 +55,6 @@ class Zend_Config_Writer_AllTests
 
         $suite->addTestSuite('Zend_Config_Writer_ArrayTest');
         $suite->addTestSuite('Zend_Config_Writer_IniTest');
-        $suite->addTestSuite('Zend_Config_Writer_SimpleIniTest');
         $suite->addTestSuite('Zend_Config_Writer_XmlTest');
 
         return $suite;

+ 49 - 0
tests/Zend/Config/Writer/IniTest.php

@@ -184,4 +184,53 @@ test = "foo"
 ECS;
         $this->assertEquals($expected, $iniString);
     }
+
+    public function testRenderWithoutSections()
+    {
+        $config = new Zend_Config(array('test' => 'foo', 'test2' => array('test3' => 'bar')));
+
+        $writer = new Zend_Config_Writer_Ini();
+        $writer->setRenderWithoutSections();
+        $iniString = $writer->setConfig($config)->render();
+
+        $expected = <<<ECS
+test = "foo"
+test2.test3 = "bar"
+
+ECS;
+        $this->assertEquals($expected, $iniString);
+    }
+
+    public function testRenderWithoutSections2()
+    {
+        $config = new Zend_Config_Ini(dirname(__FILE__) . '/files/allsections.ini', null, array('skipExtends' => true));
+
+        $writer = new Zend_Config_Writer_Ini();
+        $writer->setRenderWithoutSections();
+        $iniString = $writer->setConfig($config)->render();
+
+        $expected = <<<ECS
+all.hostname = "all"
+all.name = "thisname"
+all.db.host = "127.0.0.1"
+all.db.user = "username"
+all.db.pass = "password"
+all.db.name = "live"
+all.one.two.three = "multi"
+staging.hostname = "staging"
+staging.db.name = "dbstaging"
+staging.debug = ""
+debug.hostname = "debug"
+debug.debug = "1"
+debug.values.changed = "1"
+debug.db.name = "dbdebug"
+debug.special.no = ""
+debug.special.null = ""
+debug.special.false = ""
+other_staging.only_in = "otherStaging"
+other_staging.db.pass = "anotherpwd"
+
+ECS;
+        $this->assertEquals($expected, $iniString);
+    }
 }

+ 0 - 73
tests/Zend/Config/Writer/SimpleIniTest.php

@@ -1,73 +0,0 @@
-<?php
-
-/**
- * Test helper
- */
-require_once dirname(__FILE__) . '/../../../TestHelper.php';
-
-/**
- * Zend_Config
- */
-require_once 'Zend/Config.php';
-
-/**
- * Zend_Config_Ini
- */
-require_once 'Zend/Config/Ini.php';
-
-/**
- * Zend_Config_Writer_Ini
- */
-require_once 'Zend/Config/Writer/Ini.php';
-
-require_once "Zend/Config/Writer/SimpleIni.php";
-
-class Zend_Config_Writer_SimpleIniTest extends PHPUnit_Framework_TestCase
-{
-    public function testRender()
-    {
-        $config = new Zend_Config(array('test' => 'foo', 'test2' => array('test3' => 'bar')));
-
-        $writer = new Zend_Config_Writer_SimpleIni();
-        $iniString = $writer->setConfig($config)->render();
-
-        $expected = <<<ECS
-test = "foo"
-test2.test3 = "bar"
-
-ECS;
-        $this->assertEquals($expected, $iniString);
-    }
-
-    public function testRender2()
-    {
-        $config = new Zend_Config_Ini(dirname(__FILE__) . '/files/allsections.ini', null, array('skipExtends' => true));
-
-        $writer = new Zend_Config_Writer_SimpleIni();
-        $iniString = $writer->setConfig($config)->render();
-
-        $expected = <<<ECS
-all.hostname = "all"
-all.name = "thisname"
-all.db.host = "127.0.0.1"
-all.db.user = "username"
-all.db.pass = "password"
-all.db.name = "live"
-all.one.two.three = "multi"
-staging.hostname = "staging"
-staging.db.name = "dbstaging"
-staging.debug = ""
-debug.hostname = "debug"
-debug.debug = "1"
-debug.values.changed = "1"
-debug.db.name = "dbdebug"
-debug.special.no = ""
-debug.special.null = ""
-debug.special.false = ""
-other_staging.only_in = "otherStaging"
-other_staging.db.pass = "anotherpwd"
-
-ECS;
-        $this->assertEquals($expected, $iniString);
-    }
-}