Configurator.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 Symfony\Bundle\WebConfiguratorBundle;
  11. use Symfony\Bundle\WebConfiguratorBundle\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. * Renders parameters as a string.
  87. *
  88. * @return string
  89. */
  90. public function render()
  91. {
  92. $lines[] = "[parameters]\n";
  93. foreach ($this->parameters as $key => $value) {
  94. if (is_integer($value) || is_float($value)) {
  95. } elseif (is_bool($value)) {
  96. $value = $value ? 'true' : 'false';
  97. } elseif (false === strpos($value, '"')) {
  98. $value = '"'.$value.'"';
  99. } else {
  100. throw new \RuntimeException('A value in an ini file can not contain double quotes (").');
  101. }
  102. $lines[] = sprintf(" %s=%s\n", $key, $value);
  103. }
  104. return implode('', $lines);
  105. }
  106. /**
  107. * Writes parameters to parameters.ini or temporary in the cache directory.
  108. *
  109. * @return boolean
  110. */
  111. public function write()
  112. {
  113. $filename = $this->isFileWritable() ? $this->filename : $this->getCacheFilename();
  114. return file_put_contents($filename, $this->render());
  115. }
  116. /**
  117. * Reads parameters from file.
  118. *
  119. * @return array
  120. */
  121. protected function read()
  122. {
  123. $filename = $this->filename;
  124. if (!$this->isFileWritable() && file_exists($this->getCacheFilename())) {
  125. $filename = $this->getCacheFilename();
  126. }
  127. $ret = parse_ini_file($filename, true);
  128. if (false === $ret || array() === $ret) {
  129. throw new \InvalidArgumentException(sprintf('The %s file is not valid.', $filename));
  130. }
  131. if (isset($ret['parameters']) && is_array($ret['parameters'])) {
  132. return $ret['parameters'];
  133. } else {
  134. return array();
  135. }
  136. }
  137. /**
  138. * getCacheFilename
  139. *
  140. * @return string
  141. */
  142. protected function getCacheFilename()
  143. {
  144. return $this->kernelDir.'/cache/parameters.ini';
  145. }
  146. }