Configurator.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sensio\Bundle\DistributionBundle\Configurator;
  11. use Sensio\Bundle\DistributionBundle\Configurator\Step\StepInterface;
  12. /**
  13. * Configurator.
  14. *
  15. * @author Marc Weistroff <marc.weistroff@gmail.com>
  16. */
  17. class Configurator
  18. {
  19. protected $filename;
  20. protected $steps;
  21. protected $parameters;
  22. public function __construct($kernelDir)
  23. {
  24. $this->kernelDir = $kernelDir;
  25. $this->filename = $kernelDir.'/config/parameters.ini';
  26. $this->steps = array();
  27. $this->parameters = $this->read();
  28. }
  29. public function isFileWritable()
  30. {
  31. return is_writable($this->filename);
  32. }
  33. public function clean()
  34. {
  35. if (file_exists($this->getCacheFilename())) {
  36. @unlink($this->getCacheFilename());
  37. }
  38. }
  39. /**
  40. * @param StepInterface $step
  41. */
  42. public function addStep(StepInterface $step)
  43. {
  44. $this->steps[] = $step;
  45. }
  46. /**
  47. * @param integer $index
  48. *
  49. * @return StepInterface
  50. */
  51. public function getStep($index)
  52. {
  53. if (isset($this->steps[$index])) {
  54. return $this->steps[$index];
  55. }
  56. }
  57. /**
  58. * @return array
  59. */
  60. public function getSteps()
  61. {
  62. return $this->steps;
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function getParameters()
  68. {
  69. return $this->parameters;
  70. }
  71. /**
  72. * @return integer
  73. */
  74. public function getStepCount()
  75. {
  76. return count($this->steps);
  77. }
  78. /**
  79. * @param array $parameters
  80. */
  81. public function mergeParameters($parameters)
  82. {
  83. $this->parameters = array_merge($this->parameters, $parameters);
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function getRequirements()
  89. {
  90. $majors = array();
  91. foreach ($this->steps as $step) {
  92. foreach ($step->checkRequirements() as $major) {
  93. $majors[] = $major;
  94. }
  95. }
  96. return $majors;
  97. }
  98. /**
  99. * @return array
  100. */
  101. public function getOptionalSettings()
  102. {
  103. $minors = array();
  104. foreach ($this->steps as $step) {
  105. foreach ($step->checkOptionalSettings() as $minor) {
  106. $minors[] = $minor;
  107. }
  108. }
  109. return $minors;
  110. }
  111. /**
  112. * Renders parameters as a string.
  113. *
  114. * @return string
  115. */
  116. public function render()
  117. {
  118. $lines[] = "[parameters]\n";
  119. foreach ($this->parameters as $key => $value) {
  120. if (is_integer($value) || is_float($value)) {
  121. } elseif (is_bool($value)) {
  122. $value = $value ? 'true' : 'false';
  123. } elseif (false === strpos($value, '"')) {
  124. $value = '"'.$value.'"';
  125. } else {
  126. throw new \RuntimeException('A value in an ini file can not contain double quotes (").');
  127. }
  128. $lines[] = sprintf(" %s=%s\n", $key, $value);
  129. }
  130. return implode('', $lines);
  131. }
  132. /**
  133. * Writes parameters to parameters.ini or temporary in the cache directory.
  134. *
  135. * @return boolean
  136. */
  137. public function write()
  138. {
  139. $filename = $this->isFileWritable() ? $this->filename : $this->getCacheFilename();
  140. return file_put_contents($filename, $this->render());
  141. }
  142. /**
  143. * Reads parameters from file.
  144. *
  145. * @return array
  146. */
  147. protected function read()
  148. {
  149. $filename = $this->filename;
  150. if (!$this->isFileWritable() && file_exists($this->getCacheFilename())) {
  151. $filename = $this->getCacheFilename();
  152. }
  153. $ret = parse_ini_file($filename, true);
  154. if (false === $ret || array() === $ret) {
  155. throw new \InvalidArgumentException(sprintf('The %s file is not valid.', $filename));
  156. }
  157. if (isset($ret['parameters']) && is_array($ret['parameters'])) {
  158. return $ret['parameters'];
  159. } else {
  160. return array();
  161. }
  162. }
  163. /**
  164. * getCacheFilename
  165. *
  166. * @return string
  167. */
  168. protected function getCacheFilename()
  169. {
  170. return $this->kernelDir.'/cache/parameters.ini';
  171. }
  172. }