|
1
|
|
|
<?php |
|
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Deployer\Host; |
|
9
|
|
|
|
|
10
|
|
|
use Deployer\Exception\Exception; |
|
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
12
|
|
|
|
|
13
|
|
|
class FileLoader |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Host[] |
|
17
|
|
|
*/ |
|
18
|
|
|
private $hosts = []; |
|
19
|
|
|
/** |
|
20
|
|
|
* @param array $datain |
|
21
|
|
|
* @return array $dataexp |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function expandOnLoad($datain) |
|
24
|
|
|
{ |
|
25
|
1 |
|
$dataout = []; |
|
26
|
1 |
|
foreach ($datain as $hostname => $config) { |
|
27
|
1 |
|
if (preg_match('/\[(.+?)\]/', $hostname)) { |
|
28
|
1 |
|
foreach (Range::expand([$hostname]) as $splithost) { |
|
29
|
1 |
|
$dataout["$splithost"] = $config; |
|
30
|
|
|
} |
|
31
|
|
|
} else { |
|
32
|
1 |
|
$dataout["$hostname"] = $config; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
return $dataout; |
|
37
|
|
|
} |
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $file |
|
40
|
|
|
* @return $this |
|
41
|
|
|
* @throws Exception |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function load($file) |
|
44
|
|
|
{ |
|
45
|
1 |
|
if (!file_exists($file) || !is_readable($file)) { |
|
46
|
|
|
throw new Exception("File `$file` doesn't exists or isn't readable."); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
$data = Yaml::parse(file_get_contents($file)); |
|
50
|
1 |
|
$data = $this->expandOnLoad($data); |
|
51
|
|
|
|
|
52
|
1 |
|
if (!is_array($data)) { |
|
53
|
|
|
throw new Exception("Hosts file `$file` should contains array of hosts."); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
foreach ($data as $hostname => $config) { |
|
57
|
1 |
|
if (preg_match('/^\./', $hostname)) { |
|
58
|
1 |
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
if (isset($config['local'])) { |
|
62
|
1 |
|
$host = new Localhost($hostname); |
|
63
|
|
|
} else { |
|
64
|
1 |
|
$host = new Host($hostname); |
|
65
|
|
|
$methods = [ |
|
66
|
1 |
|
'sshOptions', |
|
67
|
|
|
'sshFlags', |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
1 |
|
foreach ($methods as $method) { |
|
71
|
1 |
|
if (isset($config[$method])) { |
|
72
|
1 |
|
$host->$method($config[$method]); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
if (is_array($config)) { |
|
78
|
1 |
|
foreach ($config as $name => $value) { |
|
79
|
1 |
|
$host->set($name, $value); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
$this->hosts[$hostname] = $host; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return Host[] |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getHosts() |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->hosts; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|