/library/Zend/Tool/Project/Context/Zf/ApplicationConfigFile.php

https://github.com/jtai/zf2 · PHP · 286 lines · 153 code · 49 blank · 84 comment · 26 complexity · e29312aeff50ba32d5a0053a44cf2535 MD5 · raw file

  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. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Tool\Project\Context\Zf;
  25. /**
  26. * This class is the front most class for utilizing Zend\Tool\Project
  27. *
  28. * A profile is a hierarchical set of resources that keep track of
  29. * items within a specific project.
  30. *
  31. * @uses RecursiveArrayIterator
  32. * @uses RecursiveIteratorIterator
  33. * @uses \Zend\Config\Ini
  34. * @uses \Zend\Tool\Project\Context\Filesystem\File
  35. * @category Zend
  36. * @package Zend_Tool
  37. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class ApplicationConfigFile extends \Zend\Tool\Project\Context\Filesystem\File
  41. {
  42. /**
  43. * @var string
  44. */
  45. protected $_filesystemName = 'application.ini';
  46. /**
  47. * @var string
  48. */
  49. protected $_content = null;
  50. /**
  51. * getName()
  52. *
  53. * @return string
  54. */
  55. public function getName()
  56. {
  57. return 'ApplicationConfigFile';
  58. }
  59. /**
  60. * init()
  61. *
  62. * @return \Zend\Tool\Project\Context\Zf\ApplicationConfigFile
  63. */
  64. public function init()
  65. {
  66. $this->_type = $this->_resource->getAttribute('type');
  67. parent::init();
  68. return $this;
  69. }
  70. /**
  71. * getPersistentAttributes()
  72. *
  73. * @return array
  74. */
  75. public function getPersistentAttributes()
  76. {
  77. return array('type' => $this->_type);
  78. }
  79. /**
  80. * getContents()
  81. *
  82. * @return string
  83. */
  84. public function getContents()
  85. {
  86. if ($this->_content === null) {
  87. if (file_exists($this->getPath())) {
  88. $this->_content = file_get_contents($this->getPath());
  89. } else {
  90. $this->_content = $this->_getDefaultContents();
  91. }
  92. }
  93. return $this->_content;
  94. }
  95. public function getAsZendConfig($section = 'production')
  96. {
  97. return new \Zend\Config\Ini($this->getPath(), $section);
  98. }
  99. /**
  100. * addStringItem()
  101. *
  102. * @param string $key
  103. * @param string $value
  104. * @param string $section
  105. * @param bool $quoteValue
  106. * @return \Zend\Tool\Project\Context\Zf\ApplicationConfigFile
  107. */
  108. public function addStringItem($key, $value, $section = 'production', $quoteValue = true)
  109. {
  110. // null quote value means to auto-detect
  111. if ($quoteValue === null) {
  112. $quoteValue = preg_match('#[\"\']#', $value) ? false : true;
  113. }
  114. if ($quoteValue == true) {
  115. $value = '"' . $value . '"';
  116. }
  117. $contentLines = preg_split('#[\n\r]#', $this->getContents());
  118. $newLines = array();
  119. $insideSection = false;
  120. foreach ($contentLines as $contentLineIndex => $contentLine) {
  121. if ($insideSection === false && preg_match('#^\[' . $section . '#', $contentLine)) {
  122. $insideSection = true;
  123. }
  124. if ($insideSection) {
  125. // if its blank, or a section heading
  126. if ((trim($contentLine) == null) || (isset($contentLines[$contentLineIndex + 1]{0}) && $contentLines[$contentLineIndex + 1]{0} == '[')) {
  127. $newLines[] = $key . ' = ' . $value;
  128. $insideSection = null;
  129. }
  130. }
  131. $newLines[] = $contentLine;
  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. }