118 { |
118 { |
119 // null quote value means to auto-detect |
119 // null quote value means to auto-detect |
120 if ($quoteValue === null) { |
120 if ($quoteValue === null) { |
121 $quoteValue = preg_match('#[\"\']#', $value) ? false : true; |
121 $quoteValue = preg_match('#[\"\']#', $value) ? false : true; |
122 } |
122 } |
123 |
123 |
124 if ($quoteValue == true) { |
124 if ($quoteValue == true) { |
125 $value = '"' . $value . '"'; |
125 $value = '"' . $value . '"'; |
126 } |
126 } |
127 |
127 |
128 $contentLines = preg_split('#[\n\r]#', $this->getContents()); |
128 $contentLines = preg_split('#[\n\r]#', $this->getContents()); |
129 |
129 |
130 $newLines = array(); |
130 $newLines = array(); |
131 $insideSection = false; |
131 $insideSection = false; |
132 |
132 |
133 foreach ($contentLines as $contentLineIndex => $contentLine) { |
133 foreach ($contentLines as $contentLineIndex => $contentLine) { |
134 |
134 |
135 if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) { |
135 if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) { |
136 $insideSection = true; |
136 $insideSection = true; |
137 } |
137 } |
138 |
138 |
|
139 $newLines[] = $contentLine; |
139 if ($insideSection) { |
140 if ($insideSection) { |
140 // if its blank, or a section heading |
141 // if its blank, or a section heading |
141 if ((trim($contentLine) == null) || (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[')) { |
142 if (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[') { |
|
143 $newLines[] = $key . ' = ' . $value; |
|
144 $insideSection = null; |
|
145 } else if (!isset($contentLines[$contentLineIndex + 1])){ |
142 $newLines[] = $key . ' = ' . $value; |
146 $newLines[] = $key . ' = ' . $value; |
143 $insideSection = null; |
147 $insideSection = null; |
144 } |
148 } |
145 } |
149 } |
146 |
|
147 $newLines[] = $contentLine; |
|
148 } |
150 } |
149 |
151 |
150 $this->_content = implode("\n", $newLines); |
152 $this->_content = implode("\n", $newLines); |
151 return $this; |
153 return $this; |
152 } |
154 } |
153 |
155 |
154 /** |
156 /** |
155 * |
157 * |
156 * @param array $item |
158 * @param array $item |
157 * @param string $section |
159 * @param string $section |
158 * @param bool $quoteValue |
160 * @param bool $quoteValue |
159 * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile |
161 * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile |
160 */ |
162 */ |
161 public function addItem($item, $section = 'production', $quoteValue = true) |
163 public function addItem($item, $section = 'production', $quoteValue = true) |
162 { |
164 { |
163 $stringItems = array(); |
165 $stringItems = array(); |
164 $stringValues = array(); |
166 $stringValues = array(); |
165 $configKeyNames = array(); |
167 $configKeyNames = array(); |
166 |
168 |
167 $rii = new RecursiveIteratorIterator( |
169 $rii = new RecursiveIteratorIterator( |
168 new RecursiveArrayIterator($item), |
170 new RecursiveArrayIterator($item), |
169 RecursiveIteratorIterator::SELF_FIRST |
171 RecursiveIteratorIterator::SELF_FIRST |
170 ); |
172 ); |
171 |
173 |
172 $lastDepth = 0; |
174 $lastDepth = 0; |
173 |
175 |
174 // loop through array structure recursively to create proper keys |
176 // loop through array structure recursively to create proper keys |
175 foreach ($rii as $name => $value) { |
177 foreach ($rii as $name => $value) { |
176 $lastDepth = $rii->getDepth(); |
178 $lastDepth = $rii->getDepth(); |
177 |
179 |
178 if (is_array($value)) { |
180 if (is_array($value)) { |
179 array_push($configKeyNames, $name); |
181 array_push($configKeyNames, $name); |
180 } else { |
182 } else { |
181 $stringItems[] = implode('.', $configKeyNames) . '.' . $name; |
183 $stringItems[] = implode('.', $configKeyNames) . '.' . $name; |
182 $stringValues[] = $value; |
184 $stringValues[] = $value; |
183 } |
185 } |
184 } |
186 } |
185 |
187 |
186 foreach ($stringItems as $stringItemIndex => $stringItem) { |
188 foreach ($stringItems as $stringItemIndex => $stringItem) { |
187 $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue); |
189 $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue); |
188 } |
190 } |
189 |
191 |
190 return $this; |
192 return $this; |
191 } |
193 } |
192 |
194 |
193 public function removeStringItem($key, $section = 'production') |
195 public function removeStringItem($key, $section = 'production') |
194 { |
196 { |
195 $contentLines = file($this->getPath()); |
197 $contentLines = file($this->getPath()); |
196 |
198 |
197 $newLines = array(); |
199 $newLines = array(); |
198 $insideSection = false; |
200 $insideSection = false; |
199 |
201 |
200 foreach ($contentLines as $contentLineIndex => $contentLine) { |
202 foreach ($contentLines as $contentLineIndex => $contentLine) { |
201 |
203 |
202 if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) { |
204 if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) { |
203 $insideSection = true; |
205 $insideSection = true; |
204 } |
206 } |
205 |
207 |
206 if ($insideSection) { |
208 if ($insideSection) { |
207 // if its blank, or a section heading |
209 // if its blank, or a section heading |
208 if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) { |
210 if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) { |
209 $insideSection = null; |
211 $insideSection = null; |
210 } |
212 } |
211 } |
213 } |
212 |
214 |
213 if (!preg_match('#' . $key . '\s?=.*#', $contentLine)) { |
215 if (!preg_match('#' . $key . '\s?=.*#', $contentLine)) { |
214 $newLines[] = $contentLine; |
216 $newLines[] = $contentLine; |
215 } |
217 } |
216 } |
218 } |
217 |
219 |
218 $this->_content = implode('', $newLines); |
220 $this->_content = implode('', $newLines); |
219 } |
221 } |
220 |
222 |
221 public function removeItem($item, $section = 'production') |
223 public function removeItem($item, $section = 'production') |
222 { |
224 { |
223 $stringItems = array(); |
225 $stringItems = array(); |
224 $stringValues = array(); |
226 $stringValues = array(); |
225 $configKeyNames = array(); |
227 $configKeyNames = array(); |
226 |
228 |
227 $rii = new RecursiveIteratorIterator( |
229 $rii = new RecursiveIteratorIterator( |
228 new RecursiveArrayIterator($item), |
230 new RecursiveArrayIterator($item), |
229 RecursiveIteratorIterator::SELF_FIRST |
231 RecursiveIteratorIterator::SELF_FIRST |
230 ); |
232 ); |
231 |
233 |
232 $lastDepth = 0; |
234 $lastDepth = 0; |
233 |
235 |
234 // loop through array structure recursively to create proper keys |
236 // loop through array structure recursively to create proper keys |
235 foreach ($rii as $name => $value) { |
237 foreach ($rii as $name => $value) { |
236 $lastDepth = $rii->getDepth(); |
238 $lastDepth = $rii->getDepth(); |
237 |
239 |
238 if (is_array($value)) { |
240 if (is_array($value)) { |
239 array_push($configKeyNames, $name); |
241 array_push($configKeyNames, $name); |
240 } else { |
242 } else { |
241 $stringItems[] = implode('.', $configKeyNames) . '.' . $name; |
243 $stringItems[] = implode('.', $configKeyNames) . '.' . $name; |
242 $stringValues[] = $value; |
244 $stringValues[] = $value; |
243 } |
245 } |
244 } |
246 } |
245 |
247 |
246 foreach ($stringItems as $stringItemIndex => $stringItem) { |
248 foreach ($stringItems as $stringItemIndex => $stringItem) { |
247 $this->removeStringItem($stringItem, $section); |
249 $this->removeStringItem($stringItem, $section); |
248 } |
250 } |
249 |
251 |
250 return $this; |
252 return $this; |
251 } |
253 } |
252 |
254 |
253 protected function _getDefaultContents() |
255 protected function _getDefaultContents() |
254 { |
256 { |
255 |
257 |
256 $contents =<<<EOS |
258 $contents =<<<EOS |
257 [production] |
259 [production] |