File indexing completed on 2024-05-26 06:02:48

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_Json
0029  */
0030 // require_once 'Zend/Config/Json.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_Json extends Zend_Config_Writer_FileAbstract
0039 {
0040     /**
0041      * If we need to pretty-print JSON data
0042      *
0043      * @var boolean
0044      */
0045     protected $_prettyPrint = false;
0046 
0047     /**
0048      * Get prettyPrint flag
0049      *
0050      * @return the prettyPrint flag
0051      */
0052     public function prettyPrint()
0053     {
0054         return $this->_prettyPrint;
0055     }
0056 
0057     /**
0058      * Set prettyPrint flag
0059      *
0060      * @param  bool $prettyPrint PrettyPrint flag
0061      * @return Zend_Config_Writer_Json
0062      */
0063     public function setPrettyPrint($flag)
0064     {
0065         $this->_prettyPrint = (bool) $flag;
0066         return $this;
0067     }
0068 
0069     /**
0070      * Render a Zend_Config into a JSON config string.
0071      *
0072      * @since 1.10
0073      * @return string
0074      */
0075     public function render()
0076     {
0077         $data        = $this->_config->toArray();
0078         $sectionName = $this->_config->getSectionName();
0079         $extends     = $this->_config->getExtends();
0080 
0081         if (is_string($sectionName)) {
0082             $data = array($sectionName => $data);
0083         }
0084 
0085         foreach ($extends as $section => $parentSection) {
0086             $data[$section][Zend_Config_Json::EXTENDS_NAME] = $parentSection;
0087         }
0088 
0089         // Ensure that each "extends" section actually exists
0090         foreach ($data as $section => $sectionData) {
0091             if (is_array($sectionData) && isset($sectionData[Zend_Config_Json::EXTENDS_NAME])) {
0092                 $sectionExtends = $sectionData[Zend_Config_Json::EXTENDS_NAME];
0093                 if (!isset($data[$sectionExtends])) {
0094                     // Remove "extends" declaration if section does not exist
0095                     unset($data[$section][Zend_Config_Json::EXTENDS_NAME]);
0096                 }
0097             }
0098         }
0099 
0100         $out = Zend_Json::encode($data);
0101         if ($this->prettyPrint()) {
0102              $out = Zend_Json::prettyPrint($out);
0103         }
0104         return $out;
0105     }
0106 }