web/lib/Zend/Config/Writer/Ini.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: Ini.php 21983 2010-04-25 08:09:09Z jan $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Config_Writer
       
    24  */
       
    25 require_once 'Zend/Config/Writer/FileAbstract.php';
       
    26 
       
    27 /**
       
    28  * @category   Zend
       
    29  * @package    Zend_Config
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Config_Writer_Ini extends Zend_Config_Writer_FileAbstract
       
    34 {
       
    35     /**
       
    36      * String that separates nesting levels of configuration data identifiers
       
    37      *
       
    38      * @var string
       
    39      */
       
    40     protected $_nestSeparator = '.';
       
    41 
       
    42     /**
       
    43      * If true the ini string is rendered in the global namespace without sections.
       
    44      *
       
    45      * @var bool
       
    46      */
       
    47     protected $_renderWithoutSections = false;
       
    48 
       
    49     /**
       
    50      * Set the nest separator
       
    51      *
       
    52      * @param  string $filename
       
    53      * @return Zend_Config_Writer_Ini
       
    54      */
       
    55     public function setNestSeparator($separator)
       
    56     {
       
    57         $this->_nestSeparator = $separator;
       
    58 
       
    59         return $this;
       
    60     }
       
    61 
       
    62     /**
       
    63      * Set if rendering should occour without sections or not.
       
    64      *
       
    65      * If set to true, the INI file is rendered without sections completely
       
    66      * into the global namespace of the INI file.
       
    67      *
       
    68      * @param  bool $withoutSections
       
    69      * @return Zend_Config_Writer_Ini
       
    70      */
       
    71     public function setRenderWithoutSections($withoutSections=true)
       
    72     {
       
    73         $this->_renderWithoutSections = (bool)$withoutSections;
       
    74         return $this;
       
    75     }
       
    76 
       
    77     /**
       
    78      * Render a Zend_Config into a INI config string.
       
    79      *
       
    80      * @since 1.10
       
    81      * @return string
       
    82      */
       
    83     public function render()
       
    84     {
       
    85         $iniString   = '';
       
    86         $extends     = $this->_config->getExtends();
       
    87         $sectionName = $this->_config->getSectionName();
       
    88 
       
    89         if($this->_renderWithoutSections == true) {
       
    90             $iniString .= $this->_addBranch($this->_config);
       
    91         } else if (is_string($sectionName)) {
       
    92             $iniString .= '[' . $sectionName . ']' . "\n"
       
    93                        .  $this->_addBranch($this->_config)
       
    94                        .  "\n";
       
    95         } else {
       
    96             $config = $this->_sortRootElements($this->_config);
       
    97             foreach ($config as $sectionName => $data) {
       
    98                 if (!($data instanceof Zend_Config)) {
       
    99                     $iniString .= $sectionName
       
   100                                .  ' = '
       
   101                                .  $this->_prepareValue($data)
       
   102                                .  "\n";
       
   103                 } else {
       
   104                     if (isset($extends[$sectionName])) {
       
   105                         $sectionName .= ' : ' . $extends[$sectionName];
       
   106                     }
       
   107 
       
   108                     $iniString .= '[' . $sectionName . ']' . "\n"
       
   109                                .  $this->_addBranch($data)
       
   110                                .  "\n";
       
   111                 }
       
   112             }
       
   113         }
       
   114 
       
   115         return $iniString;
       
   116     }
       
   117 
       
   118     /**
       
   119      * Add a branch to an INI string recursively
       
   120      *
       
   121      * @param  Zend_Config $config
       
   122      * @return void
       
   123      */
       
   124     protected function _addBranch(Zend_Config $config, $parents = array())
       
   125     {
       
   126         $iniString = '';
       
   127 
       
   128         foreach ($config as $key => $value) {
       
   129             $group = array_merge($parents, array($key));
       
   130 
       
   131             if ($value instanceof Zend_Config) {
       
   132                 $iniString .= $this->_addBranch($value, $group);
       
   133             } else {
       
   134                 $iniString .= implode($this->_nestSeparator, $group)
       
   135                            .  ' = '
       
   136                            .  $this->_prepareValue($value)
       
   137                            .  "\n";
       
   138             }
       
   139         }
       
   140 
       
   141         return $iniString;
       
   142     }
       
   143 
       
   144     /**
       
   145      * Prepare a value for INI
       
   146      *
       
   147      * @param  mixed $value
       
   148      * @return string
       
   149      */
       
   150     protected function _prepareValue($value)
       
   151     {
       
   152         if (is_integer($value) || is_float($value)) {
       
   153             return $value;
       
   154         } elseif (is_bool($value)) {
       
   155             return ($value ? 'true' : 'false');
       
   156         } elseif (strpos($value, '"') === false) {
       
   157             return '"' . $value .  '"';
       
   158         } else {
       
   159             /** @see Zend_Config_Exception */
       
   160             require_once 'Zend/Config/Exception.php';
       
   161             throw new Zend_Config_Exception('Value can not contain double quotes "');
       
   162         }
       
   163     }
       
   164     
       
   165     /**
       
   166      * Root elements that are not assigned to any section needs to be
       
   167      * on the top of config.
       
   168      * 
       
   169      * @see    http://framework.zend.com/issues/browse/ZF-6289
       
   170      * @param  Zend_Config
       
   171      * @return Zend_Config
       
   172      */
       
   173     protected function _sortRootElements(Zend_Config $config)
       
   174     {
       
   175         $configArray = $config->toArray();
       
   176         $sections = array();
       
   177         
       
   178         // remove sections from config array
       
   179         foreach ($configArray as $key => $value) {
       
   180             if (is_array($value)) {
       
   181                 $sections[$key] = $value;
       
   182                 unset($configArray[$key]);
       
   183             }
       
   184         }
       
   185         
       
   186         // readd sections to the end
       
   187         foreach ($sections as $key => $value) {
       
   188             $configArray[$key] = $value;
       
   189         }
       
   190         
       
   191         return new Zend_Config($configArray);
       
   192     }
       
   193 }