ApplicationConfigFile.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Tool_Project_Context_Filesystem_File
  24. */
  25. require_once 'Zend/Tool/Project/Context/Filesystem/File.php';
  26. /**
  27. * This class is the front most class for utilizing Zend_Tool_Project
  28. *
  29. * A profile is a hierarchical set of resources that keep track of
  30. * items within a specific project.
  31. *
  32. * @category Zend
  33. * @package Zend_Tool
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Tool_Project_Context_Zf_ApplicationConfigFile extends Zend_Tool_Project_Context_Filesystem_File
  38. {
  39. /**
  40. * @var string
  41. */
  42. protected $_filesystemName = 'application.ini';
  43. /**
  44. * @var string
  45. */
  46. protected $_content = null;
  47. /**
  48. * getName()
  49. *
  50. * @return string
  51. */
  52. public function getName()
  53. {
  54. return 'ApplicationConfigFile';
  55. }
  56. /**
  57. * init()
  58. *
  59. * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
  60. */
  61. public function init()
  62. {
  63. $this->_type = $this->_resource->getAttribute('type');
  64. parent::init();
  65. return $this;
  66. }
  67. /**
  68. * getPersistentAttributes()
  69. *
  70. * @return array
  71. */
  72. public function getPersistentAttributes()
  73. {
  74. return array('type' => $this->_type);
  75. }
  76. /**
  77. * getContents()
  78. *
  79. * @return string
  80. */
  81. public function getContents()
  82. {
  83. if ($this->_content === null) {
  84. if (file_exists($this->getPath())) {
  85. $this->_content = file_get_contents($this->getPath());
  86. } else {
  87. $this->_content = $this->_getDefaultContents();
  88. }
  89. }
  90. return $this->_content;
  91. }
  92. public function getAsZendConfig($section = 'production')
  93. {
  94. return new Zend_Config_Ini($this->getPath(), $section);
  95. }
  96. /**
  97. * addStringItem()
  98. *
  99. * @param string $key
  100. * @param string $value
  101. * @param string $section
  102. * @param bool $quoteValue
  103. * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
  104. */
  105. public function addStringItem($key, $value, $section = 'production', $quoteValue = true)
  106. {
  107. // null quote value means to auto-detect
  108. if ($quoteValue === null) {
  109. $quoteValue = preg_match('#[\"\']#', $value) ? false : true;
  110. }
  111. if ($quoteValue == true) {
  112. $value = '"' . $value . '"';
  113. }
  114. $contentLines = preg_split('#[\n\r]#', $this->getContents());
  115. $newLines = array();
  116. $insideSection = false;
  117. foreach ($contentLines as $contentLineIndex => $contentLine) {
  118. if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
  119. $insideSection = true;
  120. }
  121. $newLines[] = $contentLine;
  122. if ($insideSection) {
  123. // if its blank, or a section heading
  124. if (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[') {
  125. $newLines[] = $key . ' = ' . $value;
  126. $insideSection = null;
  127. } else if (!isset($contentLines[$contentLineIndex + 1])){
  128. $newLines[] = $key . ' = ' . $value;
  129. $insideSection = null;
  130. }
  131. }
  132. }
  133. $this->_content = implode("\n", $newLines);
  134. return $this;
  135. }
  136. /**
  137. *
  138. * @param array $item
  139. * @param string $section
  140. * @param bool $quoteValue
  141. * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
  142. */
  143. public function addItem($item, $section = 'production', $quoteValue = true)
  144. {
  145. $stringItems = array();
  146. $stringValues = array();
  147. $configKeyNames = array();
  148. $rii = new RecursiveIteratorIterator(
  149. new RecursiveArrayIterator($item),
  150. RecursiveIteratorIterator::SELF_FIRST
  151. );
  152. $lastDepth = 0;
  153. // loop through array structure recursively to create proper keys
  154. foreach ($rii as $name => $value) {
  155. $lastDepth = $rii->getDepth();
  156. if (is_array($value)) {
  157. array_push($configKeyNames, $name);
  158. } else {
  159. $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
  160. $stringValues[] = $value;
  161. }
  162. }
  163. foreach ($stringItems as $stringItemIndex => $stringItem) {
  164. $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue);
  165. }
  166. return $this;
  167. }
  168. public function removeStringItem($key, $section = 'production')
  169. {
  170. $contentLines = file($this->getPath());
  171. $newLines = array();
  172. $insideSection = false;
  173. foreach ($contentLines as $contentLineIndex => $contentLine) {
  174. if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
  175. $insideSection = true;
  176. }
  177. if ($insideSection) {
  178. // if its blank, or a section heading
  179. if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) {
  180. $insideSection = null;
  181. }
  182. }
  183. if (!preg_match('#' . $key . '\s?=.*#', $contentLine)) {
  184. $newLines[] = $contentLine;
  185. }
  186. }
  187. $this->_content = implode('', $newLines);
  188. }
  189. public function removeItem($item, $section = 'production')
  190. {
  191. $stringItems = array();
  192. $stringValues = array();
  193. $configKeyNames = array();
  194. $rii = new RecursiveIteratorIterator(
  195. new RecursiveArrayIterator($item),
  196. RecursiveIteratorIterator::SELF_FIRST
  197. );
  198. $lastDepth = 0;
  199. // loop through array structure recursively to create proper keys
  200. foreach ($rii as $name => $value) {
  201. $lastDepth = $rii->getDepth();
  202. if (is_array($value)) {
  203. array_push($configKeyNames, $name);
  204. } else {
  205. $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
  206. $stringValues[] = $value;
  207. }
  208. }
  209. foreach ($stringItems as $stringItemIndex => $stringItem) {
  210. $this->removeStringItem($stringItem, $section);
  211. }
  212. return $this;
  213. }
  214. protected function _getDefaultContents()
  215. {
  216. $contents =<<<EOS
  217. [production]
  218. phpSettings.display_startup_errors = 0
  219. phpSettings.display_errors = 0
  220. includePaths.library = APPLICATION_PATH "/../library"
  221. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  222. bootstrap.class = "Bootstrap"
  223. appnamespace = "Application"
  224. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  225. resources.frontController.params.displayExceptions = 0
  226. [staging : production]
  227. [testing : production]
  228. phpSettings.display_startup_errors = 1
  229. phpSettings.display_errors = 1
  230. [development : production]
  231. phpSettings.display_startup_errors = 1
  232. phpSettings.display_errors = 1
  233. resources.frontController.params.displayExceptions = 1
  234. EOS;
  235. return $contents;
  236. }
  237. }