initial setup of framework code
[cacert-mgr.git] / manager / library / config / Config_Writer_Db.php
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: Config_Writer_Db.php 43 2009-12-21 14:12:34Z markus $
20 */
21
22 /**
23 * Usage:
24 * require_once(LIBRARY_PATH . '/config/Config_Writer_Db.php');
25 * $writer = new Config_Writer_Db();
26 * $writer->setTableName('system_config');
27 * $writer->write(Zend_Registry::get('config_dbc'), Zend_Registry::get('config'));
28 *
29 * $writer = new Config_Writer_Db();
30 * $writer->setTableName('dnssec_org_param');
31 * $writer->write(Zend_Registry::get('config_dbc'), dnssec_org_conf, 'dnssec_org_id="2"');
32 */
33
34 /**
35 * @see Zend_Config_Writer
36 */
37 require_once 'Zend/Config/Writer.php';
38
39 /**
40 * @category Zend
41 * @package Zend_Config
42 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
43 * @license http://framework.zend.com/license/new-bsd New BSD License
44 */
45 class Config_Writer_Db extends Zend_Config_Writer
46 {
47 /**
48 * String that separates nesting levels of configuration data identifiers
49 *
50 * @var string
51 */
52 protected $_nestSeparator = '.';
53
54 protected $_set = null;
55
56 protected $_tableName = null;
57
58 /**
59 * Set the nest separator
60 *
61 * @param string $filename
62 * @return Zend_Config_Writer_Ini
63 */
64 public function setNestSeparator($separator)
65 {
66 $this->_nestSeparator = $separator;
67
68 return $this;
69 }
70
71 public function setTableName($name)
72 {
73 $this->_tableName = $name;
74
75 return $this;
76 }
77
78 /**
79 * Defined by Zend_Config_Writer
80 *
81 * use set to limit impact when a shared config file is used (i.e. config per item using foreign keys)
82 *
83 * @param string $filename
84 * @param Config_Db $config
85 * @param string $set
86 * @return void
87 */
88 public function write($db = null, $config = null, $set = null) {
89 $this->_set = $set;
90
91 // this method is specialized for writing back Config objects (which hold config_db objects)
92 if ($config !== null) {
93 if ($config instanceof Config)
94 $this->setConfig($config->getConfig());
95 else {
96 $this->setConfig($config);
97 }
98 }
99
100 if ($this->_config === null) {
101 require_once 'Zend/Config/Exception.php';
102 throw new Zend_Config_Exception('No config was set');
103 }
104
105 if ($db === null) {
106 require_once 'Zend/Config/Exception.php';
107 throw new Zend_Config_Exception('No db was set');
108 }
109
110 $sql = array();
111
112 $string = 'delete from ' . $this->_tableName;
113 if ($this->_set !== null) {
114 $string .= ' where ' . $this->_set;
115 }
116
117 $sql[] = $string;
118
119 $iniString = '';
120 $extends = $this->_config->getExtends();
121 $sectionName = $this->_config->getSectionName();
122
123 foreach ($this->_config as $key => $data) {
124 $sql= array_merge($sql, $this->addEntry($sectionName, $key, $data));
125 }
126
127 try {
128 $db->beginTransaction();
129 foreach ($sql as $command) {
130 #Log::Log()->debug($command);
131 $db->query($command);
132 }
133 $db->commit();
134 } catch (Exception $e) {
135 $db->rollBack();
136 Log::Log()->err($e);
137 throw $e;
138 }
139 }
140
141 /**
142 * build key value pairs, key is created by recursively adding section names, delimited by "."
143 * @param string $prefix
144 * @param string $key
145 * @param mixed $data
146 */
147 protected function addEntry($prefix, $key, $data) {
148 $sql = array();
149
150 if ($data instanceof Zend_Config) {
151 if ($prefix != '')
152 $prefix .= '.';
153 $prefix .= $key;
154 foreach ($data as $k => $v) {
155 $sql = array_merge($sql, $this->addEntry($prefix, $k, $v));
156 }
157 }
158 else {
159 $string = 'insert into ' . $this->_tableName . ' set ';
160 $pkey = $prefix;
161 if ($pkey != '')
162 $pkey .= '.';
163 $pkey .= $key;
164 $string .= 'config_key=' . $this->_prepareValue($pkey) . ', ';
165 $string .= 'config_value=' . $this->_prepareValue($data);
166 if ($this->_set !== null)
167 $string .= ', ' . $this->_set;
168
169 $sql[] = $string;
170 }
171
172 return $sql;
173 }
174
175 /**
176 * Add a branch to an INI string recursively
177 *
178 * @param Zend_Config $config
179 * @return void
180 */
181 protected function _addBranch(Zend_Config $config, $parents = array())
182 {
183 $iniString = '';
184
185 foreach ($config as $key => $value) {
186 $group = array_merge($parents, array($key));
187
188 if ($value instanceof Zend_Config) {
189 $iniString .= $this->_addBranch($value, $group);
190 } else {
191 $iniString .= implode($this->_nestSeparator, $group)
192 . ' = '
193 . $this->_prepareValue($value)
194 . "\n";
195 }
196 }
197
198 return $iniString;
199 }
200
201 /**
202 * Prepare a value for INI
203 *
204 * @param mixed $value
205 * @return string
206 */
207 protected function _prepareValue($value)
208 {
209 if (is_integer($value) || is_float($value)) {
210 return $value;
211 } elseif (is_bool($value)) {
212 return ($value ? 'true' : 'false');
213 } else {
214 return '"' . addslashes($value) . '"';
215 }
216 }
217 }