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