Переглянути джерело

ZF-6111 - Implemented a Zend_Config_Writer_SimpleIni writer which writes out all config data into the global namespace of the INI file and does not use sections at all.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18863 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei 16 роки тому
батько
коміт
56a7341e14

+ 17 - 3
documentation/manual/en/module_specs/Zend_Config_Writer.xml

@@ -6,8 +6,8 @@
         <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 three adapters, which all
-        work the same. You instantiate a writer with specific options, which
+        <classname>Zend_Config_Writer</classname> ships with four 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
         file is created. You can also give <varname>$filename</varname> and
@@ -29,13 +29,27 @@
         </listitem>
         <listitem>
             <para>
+                <classname>Zend_Config_Writer_SimpleIni</classname>
+            </para>
+        </listitem>
+        <listitem>
+            <para>
                 <classname>Zend_Config_Writer_Xml</classname>
             </para>
         </listitem>
     </itemizedlist>
 
     <para>
-        As an exception, <classname>Zend_Config_Writer_Ini</classname> has an additional
+        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.
+    </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
         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.

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

@@ -0,0 +1,47 @@
+<?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);
+    }
+}

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

@@ -31,6 +31,7 @@ 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';
 
 /**
@@ -55,6 +56,7 @@ 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;

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

@@ -0,0 +1,73 @@
+<?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);
+    }
+}