Browse Source

ZF-8234 - Refactored Zend_Config_Writer to have a FileAbstract writer which carries all the duplicate code from INI, XML and PHP Array Writers

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18862 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei 16 years ago
parent
commit
f3051ace3d

+ 7 - 0
documentation/manual/en/module_specs/Zend_Config_Writer.xml

@@ -126,6 +126,13 @@ $writer->write();
             <emphasis>skipExtends</emphasis> as option to the constructor.
         </para>
     </note>
+
+    <para>
+        For all the File-Based writers (INI, XML and PHP Array) internally
+        the <methodname>render()</methodname> is used to build the configuration
+        string. This method can be used from the outside also if you need
+        to access the string-representation of the configuration data.
+    </para>
 </sect1>
 <!--
 vim:se ts=4 sw=4 et:

+ 7 - 84
library/Zend/Config/Writer/Array.php

@@ -22,7 +22,7 @@
 /**
  * @see Zend_Config_Writer
  */
-require_once 'Zend/Config/Writer.php';
+require_once 'Zend/Config/Writer/FileAbstract.php';
 
 /**
  * @category   Zend
@@ -30,82 +30,16 @@ require_once 'Zend/Config/Writer.php';
  * @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_Array extends Zend_Config_Writer
+class Zend_Config_Writer_Array extends Zend_Config_Writer_FileAbstract
 {
     /**
-     * Filename to write to
+     * Render a Zend_Config into a PHP Array config string.
      *
-     * @var string
+     * @since 1.10
+     * @return string
      */
-    protected $_filename = null;
-
-    /**
-     * Wether to exclusively lock the file or not
-     *
-     * @var boolean
-     */
-    protected $_exclusiveLock = false;
-
-    /**
-     * Set the target filename
-     *
-     * @param  string $filename
-     * @return Zend_Config_Writer_Array
-     */
-    public function setFilename($filename)
+    public function render()
     {
-        $this->_filename = $filename;
-
-        return $this;
-    }
-
-    /**
-     * Set wether to exclusively lock the file or not
-     *
-     * @param  boolean     $exclusiveLock
-     * @return Zend_Config_Writer_Array
-     */
-    public function setExclusiveLock($exclusiveLock)
-    {
-        $this->_exclusiveLock = $exclusiveLock;
-
-        return $this;
-    }
-
-    /**
-     * Defined by Zend_Config_Writer
-     *
-     * @param  string      $filename
-     * @param  Zend_Config $config
-     * @param  boolean     $exclusiveLock
-     * @throws Zend_Config_Exception When filename was not set
-     * @throws Zend_Config_Exception When filename is not writable
-     * @return void
-     */
-    public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
-    {
-        if ($filename !== null) {
-            $this->setFilename($filename);
-        }
-
-        if ($config !== null) {
-            $this->setConfig($config);
-        }
-
-        if ($exclusiveLock !== null) {
-            $this->setExclusiveLock($exclusiveLock);
-        }
-
-        if ($this->_filename === null) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('No filename was set');
-        }
-
-        if ($this->_config === null) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('No config was set');
-        }
-
         $data        = $this->_config->toArray();
         $sectionName = $this->_config->getSectionName();
 
@@ -116,17 +50,6 @@ class Zend_Config_Writer_Array extends Zend_Config_Writer
         $arrayString = "<?php\n"
                      . "return " . var_export($data, true) . ";\n";
 
-        $flags = 0;
-
-        if ($this->_exclusiveLock) {
-            $flags |= LOCK_EX;
-        }
-
-        $result = @file_put_contents($this->_filename, $arrayString, $flags);
-
-        if ($result === false) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
-        }
+        return $arrayString;
     }
 }

+ 134 - 0
library/Zend/Config/Writer/FileAbstract.php

@@ -0,0 +1,134 @@
+<?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
+ * @package    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
+ */
+
+require_once "Zend/Config/Writer.php";
+
+/**
+ * Abstract File Writer
+ *
+ * @category   Zend
+ * @package    Zend_package
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+class Zend_Config_Writer_FileAbstract extends Zend_Config_Writer
+{
+    /**
+     * Filename to write to
+     *
+     * @var string
+     */
+    protected $_filename = null;
+
+    /**
+     * Wether to exclusively lock the file or not
+     *
+     * @var boolean
+     */
+    protected $_exclusiveLock = false;
+
+    /**
+     * Set the target filename
+     *
+     * @param  string $filename
+     * @return Zend_Config_Writer_Array
+     */
+    public function setFilename($filename)
+    {
+        $this->_filename = $filename;
+
+        return $this;
+    }
+
+    /**
+     * Set wether to exclusively lock the file or not
+     *
+     * @param  boolean     $exclusiveLock
+     * @return Zend_Config_Writer_Array
+     */
+    public function setExclusiveLock($exclusiveLock)
+    {
+        $this->_exclusiveLock = $exclusiveLock;
+
+        return $this;
+    }
+
+    /**
+     * Write configuration to file.
+     *
+     * @param string $filename
+     * @param Zend_Config $config
+     * @param bool $exclusiveLock
+     * @return void
+     */
+    public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
+    {
+        if ($filename !== null) {
+            $this->setFilename($filename);
+        }
+
+        if ($config !== null) {
+            $this->setConfig($config);
+        }
+
+        if ($exclusiveLock !== null) {
+            $this->setExclusiveLock($exclusiveLock);
+        }
+
+        if ($this->_filename === null) {
+            require_once 'Zend/Config/Exception.php';
+            throw new Zend_Config_Exception('No filename was set');
+        }
+
+        if ($this->_config === null) {
+            require_once 'Zend/Config/Exception.php';
+            throw new Zend_Config_Exception('No config was set');
+        }
+
+        $configString = $this->render();
+
+        $flags = 0;
+
+        if ($this->_exclusiveLock) {
+            $flags |= LOCK_EX;
+        }
+
+        $result = @file_put_contents($this->_filename, $configString, $flags);
+
+        if ($result === false) {
+            require_once 'Zend/Config/Exception.php';
+            throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
+        }
+    }
+
+    /**
+     * Render a Zend_Config into a config file string.
+     *
+     * @since 1.10
+     * @todo For 2.0 this should be redone into an abstract method.
+     * @return string
+     */
+    public function render()
+    {
+        return "";
+    }
+}

+ 7 - 84
library/Zend/Config/Writer/Ini.php

@@ -22,7 +22,7 @@
 /**
  * @see Zend_Config_Writer
  */
-require_once 'Zend/Config/Writer.php';
+require_once 'Zend/Config/Writer/FileAbstract.php';
 
 /**
  * @category   Zend
@@ -30,23 +30,9 @@ require_once 'Zend/Config/Writer.php';
  * @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_Ini extends Zend_Config_Writer
+class Zend_Config_Writer_Ini extends Zend_Config_Writer_FileAbstract
 {
     /**
-     * Filename to write to
-     *
-     * @var string
-     */
-    protected $_filename = null;
-
-    /**
-     * Wether to exclusively lock the file or not
-     *
-     * @var boolean
-     */
-    protected $_exclusiveLock = false;
-
-    /**
      * String that separates nesting levels of configuration data identifiers
      *
      * @var string
@@ -54,32 +40,6 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer
     protected $_nestSeparator = '.';
 
     /**
-     * Set the target filename
-     *
-     * @param  string $filename
-     * @return Zend_Config_Writer_Xml
-     */
-    public function setFilename($filename)
-    {
-        $this->_filename = $filename;
-
-        return $this;
-    }
-
-    /**
-     * Set wether to exclusively lock the file or not
-     *
-     * @param  boolean     $exclusiveLock
-     * @return Zend_Config_Writer_Array
-     */
-    public function setExclusiveLock($exclusiveLock)
-    {
-        $this->_exclusiveLock = $exclusiveLock;
-
-        return $this;
-    }
-
-    /**
      * Set the nest separator
      *
      * @param  string $filename
@@ -93,39 +53,13 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer
     }
 
     /**
-     * Defined by Zend_Config_Writer
+     * Render a Zend_Config into a INI config string.
      *
-     * @param  string      $filename
-     * @param  Zend_Config $config
-     * @param  boolean     $exclusiveLock
-     * @throws Zend_Config_Exception When filename was not set
-     * @throws Zend_Config_Exception When filename is not writable
-     * @return void
+     * @since 1.10
+     * @return string
      */
-    public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
+    public function render()
     {
-        if ($filename !== null) {
-            $this->setFilename($filename);
-        }
-
-        if ($config !== null) {
-            $this->setConfig($config);
-        }
-
-        if ($exclusiveLock !== null) {
-            $this->setExclusiveLock($exclusiveLock);
-        }
-
-        if ($this->_filename === null) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('No filename was set');
-        }
-
-        if ($this->_config === null) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('No config was set');
-        }
-
         $iniString   = '';
         $extends     = $this->_config->getExtends();
         $sectionName = $this->_config->getSectionName();
@@ -153,18 +87,7 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer
             }
         }
 
-        $flags = 0;
-
-        if ($this->_exclusiveLock) {
-            $flags |= LOCK_EX;
-        }
-
-        $result = @file_put_contents($this->_filename, $iniString, $flags);
-
-        if ($result === false) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
-        }
+        return $iniString;
     }
 
     /**

+ 7 - 84
library/Zend/Config/Writer/Xml.php

@@ -22,7 +22,7 @@
 /**
  * @see Zend_Config_Writer
  */
-require_once 'Zend/Config/Writer.php';
+require_once 'Zend/Config/Writer/FileAbstract.php';
 
 /**
  * @see Zend_Config_Xml
@@ -35,82 +35,16 @@ require_once 'Zend/Config/Xml.php';
  * @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_Xml extends Zend_Config_Writer
+class Zend_Config_Writer_Xml extends Zend_Config_Writer_FileAbstract
 {
     /**
-     * Filename to write to
+     * Render a Zend_Config into a XML config string.
      *
-     * @var string
+     * @since 1.10
+     * @return string
      */
-    protected $_filename = null;
-
-    /**
-     * Wether to exclusively lock the file or not
-     *
-     * @var boolean
-     */
-    protected $_exclusiveLock = false;
-
-    /**
-     * Set the target filename
-     *
-     * @param  string $filename
-     * @return Zend_Config_Writer_Xml
-     */
-    public function setFilename($filename)
-    {
-        $this->_filename = $filename;
-
-        return $this;
-    }
-
-    /**
-     * Set wether to exclusively lock the file or not
-     *
-     * @param  boolean     $exclusiveLock
-     * @return Zend_Config_Writer_Array
-     */
-    public function setExclusiveLock($exclusiveLock)
-    {
-        $this->_exclusiveLock = $exclusiveLock;
-
-        return $this;
-    }
-
-    /**
-     * Defined by Zend_Config_Writer
-     *
-     * @param  string      $filename
-     * @param  Zend_Config $config
-     * @param  boolean     $exclusiveLock
-     * @throws Zend_Config_Exception When filename was not set
-     * @throws Zend_Config_Exception When filename is not writable
-     * @return void
-     */
-    public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
+    public function render()
     {
-        if ($filename !== null) {
-            $this->setFilename($filename);
-        }
-
-        if ($config !== null) {
-            $this->setConfig($config);
-        }
-
-        if ($exclusiveLock !== null) {
-            $this->setExclusiveLock($exclusiveLock);
-        }
-
-        if ($this->_filename === null) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('No filename was set');
-        }
-
-        if ($this->_config === null) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('No config was set');
-        }
-
         $xml         = new SimpleXMLElement('<zend-config xmlns:zf="' . Zend_Config_Xml::XML_NAMESPACE . '"/>');
         $extends     = $this->_config->getExtends();
         $sectionName = $this->_config->getSectionName();
@@ -140,18 +74,7 @@ class Zend_Config_Writer_Xml extends Zend_Config_Writer
 
         $xmlString = $dom->saveXML();
 
-        $flags = 0;
-
-        if ($this->_exclusiveLock) {
-            $flags |= LOCK_EX;
-        }
-
-        $result = @file_put_contents($this->_filename, $xmlString, $flags);
-
-        if ($result === false) {
-            require_once 'Zend/Config/Exception.php';
-            throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
-        }
+        return $xmlString;
     }
 
     /**

+ 26 - 0
tests/Zend/Config/Writer/ArrayTest.php

@@ -116,4 +116,30 @@ class Zend_Config_Writer_ArrayTest extends PHPUnit_Framework_TestCase
         
         $this->assertEquals('foo', $config->test);
     }
+
+    /**
+     * @group ZF-8234
+     */
+    public function testRender()
+    {
+        $config = new Zend_Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo')));
+
+        $writer = new Zend_Config_Writer_Array();
+        $configString = $writer->setConfig($config)->render();
+
+        $expected = <<<ECS
+<?php
+return array (
+  'test' => 'foo',
+  'bar' => 
+  array (
+    0 => 'baz',
+    1 => 'foo',
+  ),
+);
+
+ECS;
+
+        $this->assertEquals($expected, $configString);
+    }
 }

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

@@ -163,4 +163,25 @@ class Zend_Config_Writer_IniTest extends PHPUnit_Framework_TestCase
         
         $this->assertEquals('foo', $config->default->test);
     }
+
+    /**
+     * @group ZF-8234
+     */
+    public function testRender()
+    {
+        $config = new Zend_Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo')));
+
+        $writer = new Zend_Config_Writer_Ini();
+        $iniString = $writer->setConfig($config)->render();
+
+        $expected = <<<ECS
+test = "foo"
+[bar]
+0 = "baz"
+1 = "foo"
+
+
+ECS;
+        $this->assertEquals($expected, $iniString);
+    }
 }

+ 23 - 0
tests/Zend/Config/Writer/XmlTest.php

@@ -211,4 +211,27 @@ class Zend_Config_Writer_XmlTest extends PHPUnit_Framework_TestCase
         
         $this->assertEquals('foo', $config->default->test);
     }
+
+    /**
+     * @group ZF-8234
+     */
+    public function testRender()
+    {
+        $config = new Zend_Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo')));
+
+        $writer = new Zend_Config_Writer_Xml();
+        $configString = $writer->setConfig($config)->render();
+
+        $expected = <<<ECS
+<?xml version="1.0"?>
+<zend-config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
+  <test>foo</test>
+  <bar>baz</bar>
+  <bar>foo</bar>
+</zend-config>
+
+ECS;
+
+        $this->assertEquals($expected, $configString);
+    }
 }