File indexing completed on 2025-03-02 05:29:14

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Config
0017  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Config_Writer
0024  */
0025 // require_once 'Zend/Config/Writer/FileAbstract.php';
0026 
0027 /**
0028  * @see Zend_Config_Yaml
0029  */
0030 // require_once 'Zend/Config/Yaml.php';
0031 
0032 /**
0033  * @category   Zend
0034  * @package    Zend_Config
0035  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Config_Writer_Yaml extends Zend_Config_Writer_FileAbstract
0039 {
0040     /**
0041      * What to call when we need to decode some YAML?
0042      *
0043      * @var callable
0044      */
0045     protected $_yamlEncoder = array('Zend_Config_Writer_Yaml', 'encode');
0046 
0047     /**
0048      * Get callback for decoding YAML
0049      *
0050      * @return callable
0051      */
0052     public function getYamlEncoder()
0053     {
0054         return $this->_yamlEncoder;
0055     }
0056 
0057     /**
0058      * Set callback for decoding YAML
0059      *
0060      * @param  callable $yamlEncoder the decoder to set
0061      * @return Zend_Config_Yaml
0062      */
0063     public function setYamlEncoder($yamlEncoder)
0064     {
0065         if (!is_callable($yamlEncoder)) {
0066             // require_once 'Zend/Config/Exception.php';
0067             throw new Zend_Config_Exception('Invalid parameter to setYamlEncoder - must be callable');
0068         }
0069 
0070         $this->_yamlEncoder = $yamlEncoder;
0071         return $this;
0072     }
0073 
0074     /**
0075      * Render a Zend_Config into a YAML config string.
0076      *
0077      * @since 1.10
0078      * @return string
0079      */
0080     public function render()
0081     {
0082         $data        = $this->_config->toArray();
0083         $sectionName = $this->_config->getSectionName();
0084         $extends     = $this->_config->getExtends();
0085 
0086         if (is_string($sectionName)) {
0087             $data = array($sectionName => $data);
0088         }
0089 
0090         foreach ($extends as $section => $parentSection) {
0091             $data[$section][Zend_Config_Yaml::EXTENDS_NAME] = $parentSection;
0092         }
0093 
0094         // Ensure that each "extends" section actually exists
0095         foreach ($data as $section => $sectionData) {
0096             if (is_array($sectionData) && isset($sectionData[Zend_Config_Yaml::EXTENDS_NAME])) {
0097                 $sectionExtends = $sectionData[Zend_Config_Yaml::EXTENDS_NAME];
0098                 if (!isset($data[$sectionExtends])) {
0099                     // Remove "extends" declaration if section does not exist
0100                     unset($data[$section][Zend_Config_Yaml::EXTENDS_NAME]);
0101                 }
0102             }
0103         }
0104 
0105         return call_user_func($this->getYamlEncoder(), $data);
0106     }
0107 
0108     /**
0109      * Very dumb YAML encoder
0110      *
0111      * Until we have Zend_Yaml...
0112      *
0113      * @param array $data YAML data
0114      * @return string
0115      */
0116     public static function encode($data)
0117     {
0118         return self::_encodeYaml(0, $data);
0119     }
0120 
0121     /**
0122      * Service function for encoding YAML
0123      *
0124      * @param int $indent Current indent level
0125      * @param array $data Data to encode
0126      * @return string
0127      */
0128     protected static function _encodeYaml($indent, $data)
0129     {
0130         reset($data);
0131         $result = "";
0132         $numeric = is_numeric(key($data));
0133 
0134         foreach($data as $key => $value) {
0135             if(is_array($value)) {
0136                 $encoded = "\n".self::_encodeYaml($indent+1, $value);
0137             } else {
0138                 $encoded = (string)$value."\n";
0139             }
0140             $result .= str_repeat("  ", $indent).($numeric?"- ":"$key: ").$encoded;
0141         }
0142         return $result;
0143     }
0144 }