ApplicationConfigFile.php 7.9 KB

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