ApplicationConfigFile.php 7.6 KB

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