|
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: Yaml.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_Yaml |
|
29 */ |
|
30 require_once 'Zend/Config/Yaml.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_Yaml extends Zend_Config_Writer_FileAbstract |
|
39 { |
|
40 /** |
|
41 * What to call when we need to decode some YAML? |
|
42 * |
|
43 * @var callable |
|
44 */ |
|
45 protected $_yamlEncoder = array('Zend_Config_Writer_Yaml', 'encode'); |
|
46 |
|
47 /** |
|
48 * Get callback for decoding YAML |
|
49 * |
|
50 * @return callable |
|
51 */ |
|
52 public function getYamlEncoder() |
|
53 { |
|
54 return $this->_yamlEncoder; |
|
55 } |
|
56 |
|
57 /** |
|
58 * Set callback for decoding YAML |
|
59 * |
|
60 * @param $yamlEncoder the decoder to set |
|
61 * @return Zend_Config_Yaml |
|
62 */ |
|
63 public function setYamlEncoder($yamlEncoder) |
|
64 { |
|
65 if (!is_callable($yamlEncoder)) { |
|
66 require_once 'Zend/Config/Exception.php'; |
|
67 throw new Zend_Config_Exception('Invalid parameter to setYamlEncoder - must be callable'); |
|
68 } |
|
69 |
|
70 $this->_yamlEncoder = $yamlEncoder; |
|
71 return $this; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Render a Zend_Config into a YAML config string. |
|
76 * |
|
77 * @since 1.10 |
|
78 * @return string |
|
79 */ |
|
80 public function render() |
|
81 { |
|
82 $data = $this->_config->toArray(); |
|
83 $sectionName = $this->_config->getSectionName(); |
|
84 $extends = $this->_config->getExtends(); |
|
85 |
|
86 if (is_string($sectionName)) { |
|
87 $data = array($sectionName => $data); |
|
88 } |
|
89 |
|
90 foreach ($extends as $section => $parentSection) { |
|
91 $data[$section][Zend_Config_Yaml::EXTENDS_NAME] = $parentSection; |
|
92 } |
|
93 |
|
94 // Ensure that each "extends" section actually exists |
|
95 foreach ($data as $section => $sectionData) { |
|
96 if (is_array($sectionData) && isset($sectionData[Zend_Config_Yaml::EXTENDS_NAME])) { |
|
97 $sectionExtends = $sectionData[Zend_Config_Yaml::EXTENDS_NAME]; |
|
98 if (!isset($data[$sectionExtends])) { |
|
99 // Remove "extends" declaration if section does not exist |
|
100 unset($data[$section][Zend_Config_Yaml::EXTENDS_NAME]); |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 return call_user_func($this->getYamlEncoder(), $data); |
|
106 } |
|
107 |
|
108 /** |
|
109 * Very dumb YAML encoder |
|
110 * |
|
111 * Until we have Zend_Yaml... |
|
112 * |
|
113 * @param array $data YAML data |
|
114 * @return string |
|
115 */ |
|
116 public static function encode($data) |
|
117 { |
|
118 return self::_encodeYaml(0, $data); |
|
119 } |
|
120 |
|
121 /** |
|
122 * Service function for encoding YAML |
|
123 * |
|
124 * @param int $indent Current indent level |
|
125 * @param array $data Data to encode |
|
126 * @return string |
|
127 */ |
|
128 protected static function _encodeYaml($indent, $data) |
|
129 { |
|
130 reset($data); |
|
131 $result = ""; |
|
132 $numeric = is_numeric(key($data)); |
|
133 |
|
134 foreach($data as $key => $value) { |
|
135 if(is_array($value)) { |
|
136 $encoded = "\n".self::_encodeYaml($indent+1, $value); |
|
137 } else { |
|
138 $encoded = (string)$value."\n"; |
|
139 } |
|
140 $result .= str_repeat(" ", $indent).($numeric?"- ":"$key: ").$encoded; |
|
141 } |
|
142 return $result; |
|
143 } |
|
144 } |