|
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-2009 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Json.php 23294 2010-11-05 00:27:34Z ramon $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Config_Writer |
|
24 */ |
|
25 require_once 'Zend/Config/Writer/FileAbstract.php'; |
|
26 |
|
27 /** |
|
28 * @see Zend_Config_Json |
|
29 */ |
|
30 require_once 'Zend/Config/Json.php'; |
|
31 |
|
32 /** |
|
33 * @category Zend |
|
34 * @package Zend_Config |
|
35 * @copyright Copyright (c) 2005-2009 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_Json extends Zend_Config_Writer_FileAbstract |
|
39 { |
|
40 /** |
|
41 * If we need to pretty-print JSON data |
|
42 * |
|
43 * @var boolean |
|
44 */ |
|
45 protected $_prettyPrint = false; |
|
46 |
|
47 /** |
|
48 * Get prettyPrint flag |
|
49 * |
|
50 * @return the prettyPrint flag |
|
51 */ |
|
52 public function prettyPrint() |
|
53 { |
|
54 return $this->_prettyPrint; |
|
55 } |
|
56 |
|
57 /** |
|
58 * Set prettyPrint flag |
|
59 * |
|
60 * @param bool $prettyPrint PrettyPrint flag |
|
61 * @return Zend_Config_Writer_Json |
|
62 */ |
|
63 public function setPrettyPrint($flag) |
|
64 { |
|
65 $this->_prettyPrint = (bool) $flag; |
|
66 return $this; |
|
67 } |
|
68 |
|
69 /** |
|
70 * Render a Zend_Config into a JSON config string. |
|
71 * |
|
72 * @since 1.10 |
|
73 * @return string |
|
74 */ |
|
75 public function render() |
|
76 { |
|
77 $data = $this->_config->toArray(); |
|
78 $sectionName = $this->_config->getSectionName(); |
|
79 $extends = $this->_config->getExtends(); |
|
80 |
|
81 if (is_string($sectionName)) { |
|
82 $data = array($sectionName => $data); |
|
83 } |
|
84 |
|
85 foreach ($extends as $section => $parentSection) { |
|
86 $data[$section][Zend_Config_Json::EXTENDS_NAME] = $parentSection; |
|
87 } |
|
88 |
|
89 // Ensure that each "extends" section actually exists |
|
90 foreach ($data as $section => $sectionData) { |
|
91 if (is_array($sectionData) && isset($sectionData[Zend_Config_Json::EXTENDS_NAME])) { |
|
92 $sectionExtends = $sectionData[Zend_Config_Json::EXTENDS_NAME]; |
|
93 if (!isset($data[$sectionExtends])) { |
|
94 // Remove "extends" declaration if section does not exist |
|
95 unset($data[$section][Zend_Config_Json::EXTENDS_NAME]); |
|
96 } |
|
97 } |
|
98 } |
|
99 |
|
100 $out = Zend_Json::encode($data); |
|
101 if ($this->prettyPrint()) { |
|
102 $out = Zend_Json::prettyPrint($out); |
|
103 } |
|
104 return $out; |
|
105 } |
|
106 } |