ApplicationConfigFile.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-2009 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-2009 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 ((trim($contentLine) == null) || ($contentLines[$contentLineIndex + 1][0] == '[')) {
  115. $newLines[] = $key . ' = ' . $value . "\n";
  116. $insideSection = null;
  117. }
  118. }
  119. $newLines[] = $contentLine;
  120. }
  121. $this->_content = implode('', $newLines);
  122. return $this;
  123. }
  124. /**
  125. *
  126. * @param array $item
  127. * @param string $section
  128. * @param bool $quoteValue
  129. * @return Zend_Tool_Project_Context_Zf_ApplicationConfigFile
  130. */
  131. public function addItem($item, $section = 'production', $quoteValue = true)
  132. {
  133. $stringItems = array();
  134. $stringValues = array();
  135. $configKeyNames = array();
  136. $rii = new RecursiveIteratorIterator(
  137. new RecursiveArrayIterator($item),
  138. RecursiveIteratorIterator::SELF_FIRST
  139. );
  140. $lastDepth = 0;
  141. // loop through array structure recursively to create proper keys
  142. foreach ($rii as $name => $value) {
  143. $lastDepth = $rii->getDepth();
  144. if (is_array($value)) {
  145. array_push($configKeyNames, $name);
  146. } else {
  147. $stringItems[] = implode('.', $configKeyNames) . '.' . $name;
  148. $stringValues[] = $value;
  149. }
  150. }
  151. foreach ($stringItems as $stringItemIndex => $stringItem) {
  152. $this->addStringItem($stringItem, $stringValues[$stringItemIndex], $section, $quoteValue);
  153. }
  154. return $this;
  155. }
  156. // public function removeStringItem($key, $section = 'production')
  157. // {
  158. //
  159. // }
  160. //
  161. // public function removeItem($item, $section = 'production')
  162. // {
  163. //
  164. // }
  165. protected function _getDefaultContents()
  166. {
  167. // resources.log.zendmonitor.writerName = "ZendMonitor"
  168. $contents =<<<EOS
  169. [production]
  170. phpSettings.display_startup_errors = 0
  171. phpSettings.display_errors = 0
  172. includePaths.library = APPLICATION_PATH "/../library"
  173. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  174. bootstrap.class = "Bootstrap"
  175. appnamespace = "Application"
  176. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  177. resources.frontController.params.displayExceptions = 0
  178. [staging : production]
  179. [testing : production]
  180. phpSettings.display_startup_errors = 1
  181. phpSettings.display_errors = 1
  182. [development : production]
  183. phpSettings.display_startup_errors = 1
  184. phpSettings.display_errors = 1
  185. resources.frontController.params.displayExceptions = 1
  186. EOS;
  187. return $contents;
  188. }
  189. }