|
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\Collection\PersistentCollection; |
|
11
|
|
|
use Deployer\Exception\Exception; |
|
12
|
|
|
use function Deployer\on; |
|
13
|
|
|
use function Deployer\Support\array_flatten; |
|
14
|
|
|
|
|
15
|
|
|
class Storage |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param Host[] $hosts |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function persist(array $hosts) |
|
21
|
|
|
{ |
|
22
|
|
|
on($hosts, function (Host $host) { |
|
23
|
|
|
$values = []; |
|
24
|
|
|
|
|
25
|
|
|
// Materialize config values |
|
26
|
|
|
foreach ($host->getConfig()->getCollection() as $key => $value) { |
|
27
|
|
|
$values[$key] = $host->get($key); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$file = sys_get_temp_dir() . '/' . uniqid('deployer-') . '-' . $host->getHostname() . '.dep'; |
|
31
|
|
|
$values['host_config_storage'] = $file; |
|
32
|
|
|
|
|
33
|
|
|
$persistentCollection = new PersistentCollection($file, $values); |
|
34
|
|
|
$persistentCollection->flush(); |
|
35
|
|
|
|
|
36
|
|
|
$host->getConfig()->setCollection($persistentCollection); |
|
37
|
|
|
}); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Host[] $hosts |
|
42
|
|
|
* @throws Exception |
|
43
|
|
|
*/ |
|
44
|
|
View Code Duplication |
public static function load(...$hosts) |
|
45
|
|
|
{ |
|
46
|
|
|
$hosts = array_flatten($hosts); |
|
47
|
|
|
foreach ($hosts as $host) { |
|
48
|
|
|
$collection = $host->getConfig()->getCollection(); |
|
49
|
|
|
|
|
50
|
|
|
if ($collection instanceof PersistentCollection) { |
|
51
|
|
|
$collection->load(); |
|
52
|
|
|
} else { |
|
53
|
|
|
throw new Exception("Can't load data for `$host` host. Host doesn't persistent."); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Host[] $hosts |
|
60
|
|
|
* @throws Exception |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public static function flush(...$hosts) |
|
63
|
|
|
{ |
|
64
|
|
|
$hosts = array_flatten($hosts); |
|
65
|
|
|
foreach ($hosts as $host) { |
|
66
|
|
|
$collection = $host->getConfig()->getCollection(); |
|
67
|
|
|
|
|
68
|
|
|
if ($collection instanceof PersistentCollection) { |
|
69
|
|
|
$collection->flush(); |
|
70
|
|
|
} else { |
|
71
|
|
|
throw new Exception("Can't load data for `$host` host. Host doesn't persistent."); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Host $host |
|
78
|
|
|
* @param string $file |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function setup(Host $host, string $file) |
|
81
|
|
|
{ |
|
82
|
|
|
$persistentCollection = new PersistentCollection($file); |
|
83
|
|
|
$persistentCollection->load(); |
|
84
|
|
|
$host->getConfig()->setCollection($persistentCollection); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|