ApplicationConfigFile.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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-2010 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-2010 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. if ($insideSection) {
  122. // if its blank, or a section heading
  123. if ((trim($contentLine) == null) || (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[')) {
  124. $newLines[] = $key . ' = ' . $value;
  125. $insideSection = null;
  126. }
  127. }
  128. $newLines[] = $contentLine;
  129. }
  130. $this->_content = implode("\n", $newLines);
  131. return $this;
  132. }
  133. /**
  134. *
  135. * @param array $item
  136. * @param string $section
  137. * @param bool $quoteValue
  138. * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
  139. */
  140. public function addItem($item, $section = 'production', $quoteValue = true)
  141. {
  142. $stringItems = array();
  143. $stringValues = array();
  144. $configKeyNames = array();
  145. $rii = new RecursiveIteratorIterator(
  146. new RecursiveArrayIterator($item),
  147. RecursiveIteratorIterator::SELF_FIRST
  148. );
  149. $lastDepth = 0;
  150. // loop through array structure recursively to create proper keys
  151. foreach ($rii as $name => $value) {
  152. $lastDepth = $rii->getDepth();
  153. if (is_array($value)) {
  154. array_push($configKeyNames, $name);
  155. } else {
  156. $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
  157. $stringValues[] = $value;
  158. }
  159. }
  160. foreach ($stringItems as $stringItemIndex => $stringItem) {
  161. $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue);
  162. }
  163. return $this;
  164. }
  165. public function removeStringItem($key, $section = 'production')
  166. {
  167. $contentLines = file($this->getPath());
  168. $newLines = array();
  169. $insideSection = false;
  170. foreach ($contentLines as $contentLineIndex => $contentLine) {
  171. if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
  172. $insideSection = true;
  173. }
  174. if ($insideSection) {
  175. // if its blank, or a section heading
  176. if ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) {
  177. $insideSection = null;
  178. }
  179. }
  180. if (!preg_match('#' . $key . '\s?=.*#', $contentLine)) {
  181. $newLines[] = $contentLine;
  182. }
  183. }
  184. $this->_content = implode('', $newLines);
  185. }
  186. public function removeItem($item, $section = 'production')
  187. {
  188. $stringItems = array();
  189. $stringValues = array();
  190. $configKeyNames = array();
  191. $rii = new RecursiveIteratorIterator(
  192. new RecursiveArrayIterator($item),
  193. RecursiveIteratorIterator::SELF_FIRST
  194. );
  195. $lastDepth = 0;
  196. // loop through array structure recursively to create proper keys
  197. foreach ($rii as $name => $value) {
  198. $lastDepth = $rii->getDepth();
  199. if (is_array($value)) {
  200. array_push($configKeyNames, $name);
  201. } else {
  202. $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
  203. $stringValues[] = $value;
  204. }
  205. }
  206. foreach ($stringItems as $stringItemIndex => $stringItem) {
  207. $this->removeStringItem($stringItem, $section);
  208. }
  209. return $this;
  210. }
  211. protected function _getDefaultContents()
  212. {
  213. $contents =<<<EOS
  214. [production]
  215. phpSettings.display_startup_errors = 0
  216. phpSettings.display_errors = 0
  217. includePaths.library = APPLICATION_PATH "/../library"
  218. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  219. bootstrap.class = "Bootstrap"
  220. appnamespace = "Application"
  221. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  222. resources.frontController.params.displayExceptions = 0
  223. [staging : production]
  224. [testing : production]
  225. phpSettings.display_startup_errors = 1
  226. phpSettings.display_errors = 1
  227. [development : production]
  228. phpSettings.display_startup_errors = 1
  229. phpSettings.display_errors = 1
  230. resources.frontController.params.displayExceptions = 1
  231. EOS;
  232. return $contents;
  233. }
  234. }