1 | <?php |
||
26 | class BootstrapByConfigFile |
||
27 | { |
||
28 | /** |
||
29 | * @var string|null $configFile |
||
30 | */ |
||
31 | public $configFile = null; |
||
32 | |||
33 | /** |
||
34 | * @var array $configFileContent |
||
35 | */ |
||
36 | public $configFileContent = null; |
||
37 | |||
38 | /** |
||
39 | * @var array $clusterConfig |
||
40 | */ |
||
41 | public $clusterConfig = []; |
||
42 | |||
43 | /** |
||
44 | * @var array $serverConfig |
||
45 | */ |
||
46 | public $serverConfig = []; |
||
47 | |||
48 | /** |
||
49 | * @var BuilderInterface[] $clusterBuilders |
||
50 | */ |
||
51 | public $clusterBuilders = []; |
||
52 | |||
53 | /** |
||
54 | * @var BuilderInterface[] $serverBuilders |
||
55 | */ |
||
56 | public $serverBuilders = []; |
||
57 | |||
58 | /** |
||
59 | * @param Collection $config |
||
60 | * @param BuilderInterface $builder |
||
61 | */ |
||
62 | 1 | private function executeBuilderMethods(Collection $config, BuilderInterface $builder) |
|
105 | |||
106 | /** |
||
107 | * @throws \RuntimeException |
||
108 | * @return \Deployer\Bootstrap\BootstrapByConfigFile |
||
109 | */ |
||
110 | 3 | public function parseConfig() |
|
111 | { |
||
112 | 3 | $this->configFileContent = Yaml::parse(file_get_contents($this->configFile)); |
|
113 | |||
114 | 3 | if (!is_array($this->configFileContent)) { |
|
115 | throw new \RuntimeException("Error in parsing " . $this->configFile . " file."); |
||
116 | } |
||
117 | |||
118 | 3 | foreach ($this->configFileContent as $key => $cnf) { |
|
119 | 3 | if (isset($cnf['cluster']) && $cnf['cluster']) { |
|
120 | 3 | $this->clusterConfig[$key] = $cnf; |
|
121 | 3 | } else { |
|
122 | 3 | $this->serverConfig[$key] = $cnf; |
|
123 | } |
||
124 | 3 | } |
|
125 | |||
126 | 3 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * @throws \RuntimeException |
||
131 | * @return \Deployer\Bootstrap\BootstrapByConfigFile |
||
132 | */ |
||
133 | 2 | public function initServers() |
|
134 | { |
||
135 | 2 | foreach ((array)$this->serverConfig as $name => $config) { |
|
136 | try { |
||
137 | 1 | if (!is_array($config)) { |
|
138 | throw new \RuntimeException(); |
||
139 | } |
||
140 | |||
141 | 1 | $da = new Collection($config); |
|
142 | |||
143 | 1 | if ($da->has('local')) { |
|
144 | $builder = \Deployer\localServer($name); |
||
145 | } else { |
||
146 | 1 | $builder = $da->has('port') ? |
|
147 | 1 | $this->serverBuilders[] = \Deployer\server($name, $da['host'], $da['port']) : |
|
148 | 1 | $this->serverBuilders[] = \Deployer\server($name, $da['host']); |
|
149 | } |
||
150 | |||
151 | 1 | unset($da['local']); |
|
152 | 1 | unset($da['host']); |
|
153 | 1 | unset($da['port']); |
|
154 | |||
155 | 1 | $this->executeBuilderMethods($da, $builder); |
|
156 | 1 | } catch (\RuntimeException $e) { |
|
157 | throw new \RuntimeException("Error processing servers: " . $name); |
||
158 | } |
||
159 | 2 | } |
|
160 | 2 | return $this; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @throws \RuntimeException |
||
165 | * @return \Deployer\Bootstrap\BootstrapByConfigFile |
||
166 | */ |
||
167 | 2 | public function initClusters() |
|
188 | |||
189 | /** |
||
190 | * @param string $file |
||
191 | * @return \Deployer\Bootstrap\BootstrapByConfigFile |
||
192 | */ |
||
193 | 7 | public function setConfig($file) |
|
198 | } |
||
199 |