web/lib/Zend/Config/Writer/Xml.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Config
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Xml.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Config_Writer
       
    24  */
       
    25 require_once 'Zend/Config/Writer/FileAbstract.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Config_Xml
       
    29  */
       
    30 require_once 'Zend/Config/Xml.php';
       
    31 
       
    32 /**
       
    33  * @category   Zend
       
    34  * @package    Zend_Config
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Config_Writer_Xml extends Zend_Config_Writer_FileAbstract
       
    39 {
       
    40     /**
       
    41      * Render a Zend_Config into a XML config string.
       
    42      *
       
    43      * @since 1.10
       
    44      * @return string
       
    45      */
       
    46     public function render()
       
    47     {
       
    48         $xml         = new SimpleXMLElement('<zend-config xmlns:zf="' . Zend_Config_Xml::XML_NAMESPACE . '"/>');
       
    49         $extends     = $this->_config->getExtends();
       
    50         $sectionName = $this->_config->getSectionName();
       
    51 
       
    52         if (is_string($sectionName)) {
       
    53             $child = $xml->addChild($sectionName);
       
    54 
       
    55             $this->_addBranch($this->_config, $child, $xml);
       
    56         } else {
       
    57             foreach ($this->_config as $sectionName => $data) {
       
    58                 if (!($data instanceof Zend_Config)) {
       
    59                     $xml->addChild($sectionName, (string) $data);
       
    60                 } else {
       
    61                     $child = $xml->addChild($sectionName);
       
    62 
       
    63                     if (isset($extends[$sectionName])) {
       
    64                         $child->addAttribute('zf:extends', $extends[$sectionName], Zend_Config_Xml::XML_NAMESPACE);
       
    65                     }
       
    66 
       
    67                     $this->_addBranch($data, $child, $xml);
       
    68                 }
       
    69             }
       
    70         }
       
    71 
       
    72         $dom = dom_import_simplexml($xml)->ownerDocument;
       
    73         $dom->formatOutput = true;
       
    74 
       
    75         $xmlString = $dom->saveXML();
       
    76 
       
    77         return $xmlString;
       
    78     }
       
    79 
       
    80     /**
       
    81      * Add a branch to an XML object recursively
       
    82      *
       
    83      * @param  Zend_Config      $config
       
    84      * @param  SimpleXMLElement $xml
       
    85      * @param  SimpleXMLElement $parent
       
    86      * @return void
       
    87      */
       
    88     protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent)
       
    89     {
       
    90         $branchType = null;
       
    91 
       
    92         foreach ($config as $key => $value) {
       
    93             if ($branchType === null) {
       
    94                 if (is_numeric($key)) {
       
    95                     $branchType = 'numeric';
       
    96                     $branchName = $xml->getName();
       
    97                     $xml        = $parent;
       
    98 
       
    99                     unset($parent->{$branchName});
       
   100                 } else {
       
   101                     $branchType = 'string';
       
   102                 }
       
   103             } else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) {
       
   104                 require_once 'Zend/Config/Exception.php';
       
   105                 throw new Zend_Config_Exception('Mixing of string and numeric keys is not allowed');
       
   106             }
       
   107 
       
   108             if ($branchType === 'numeric') {
       
   109                 if ($value instanceof Zend_Config) {
       
   110                     $child = $parent->addChild($branchName);
       
   111 
       
   112                     $this->_addBranch($value, $child, $parent);
       
   113                 } else {
       
   114                     $parent->addChild($branchName, (string) $value);
       
   115                 }
       
   116             } else {
       
   117                 if ($value instanceof Zend_Config) {
       
   118                     $child = $xml->addChild($key);
       
   119 
       
   120                     $this->_addBranch($value, $child, $xml);
       
   121                 } else {
       
   122                     $xml->addChild($key, (string) $value);
       
   123                 }
       
   124             }
       
   125         }
       
   126     }
       
   127 }