1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Satis; |
4
|
|
|
|
5
|
|
|
use App\Satis\Model\ConfigLock; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use JMS\Serializer\Serializer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Lukas Homza <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class ConfigPersister { |
13
|
|
|
/** @var \Illuminate\Filesystem\Filesystem $filesystem */ |
14
|
|
|
protected $filesystem; |
15
|
|
|
/** @var \App\Satis\Model\ConfigLock */ |
16
|
|
|
protected $configLock; |
17
|
|
|
/** @var \App\Satis\ConfigMirror */ |
18
|
|
|
protected $configMirror; |
19
|
|
|
/** @var \JMS\Serializer\Serializer $serializer */ |
20
|
|
|
protected $serializer; |
21
|
|
|
/** @var string $lockFilename */ |
22
|
|
|
protected $lockFilename; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return \App\Satis\Model\ConfigLock|array|\JMS\Serializer\scalar|mixed|object |
26
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
27
|
|
|
*/ |
28
|
|
|
protected function getLockFile() { |
29
|
|
|
if($this->filesystem->exists($this->lockFilename)) { |
30
|
|
|
$configLock = $this->serializer->deserialize( |
31
|
|
|
$this->filesystem->get($this->lockFilename), |
32
|
|
|
'App\Satis\Model\ConfigLock', |
33
|
|
|
'json' |
34
|
|
|
); |
35
|
|
|
} else { |
36
|
|
|
$configLock = new ConfigLock(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $configLock; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return \Illuminate\Support\Collection |
44
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
45
|
|
|
*/ |
46
|
|
|
protected function getLockedRepositories() { |
47
|
|
|
return $this->getLockFile()->getRepositories(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
52
|
|
|
* @param \App\Satis\Model\ConfigLock $configLock |
53
|
|
|
* @param \App\Satis\ConfigMirror $configMirror |
54
|
|
|
* @param \JMS\Serializer\Serializer $serializer |
55
|
|
|
*/ |
56
|
|
|
public function __construct(Filesystem $filesystem, ConfigLock $configLock, ConfigMirror $configMirror, |
57
|
|
|
Serializer $serializer |
58
|
|
|
) { |
59
|
|
|
$this->filesystem = $filesystem; |
60
|
|
|
$this->configLock = $configLock; |
61
|
|
|
$this->configMirror = $configMirror; |
62
|
|
|
$this->serializer = $serializer; |
63
|
|
|
|
64
|
|
|
$this->lockFilename = config('satis.lock'); |
65
|
|
|
$this->configFile = config('satis.config'); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isLocked() { |
72
|
|
|
return $this->getLockFile()->isLocked(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $repositoryId |
77
|
|
|
*/ |
78
|
|
|
public function lock($repositoryId) { |
79
|
|
|
$lockedRepositories = $this->getLockedRepositories(); |
80
|
|
|
|
81
|
|
|
if(!is_null($repositoryId) && !$lockedRepositories->contains($repositoryId)) { |
82
|
|
|
$lockedRepositories->push($repositoryId); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->configLock |
86
|
|
|
->isLocked(true) |
87
|
|
|
->since(date('d.m.Y H:i:s')) |
88
|
|
|
->by($lockedRepositories); |
89
|
|
|
|
90
|
|
|
$this->filesystem->put( |
91
|
|
|
$this->lockFilename, |
92
|
|
|
$this->serializer->serialize($this->configLock, 'json') |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string|null $repositoryId |
98
|
|
|
*/ |
99
|
|
|
public function unlock($repositoryId = null) { |
100
|
|
|
$lockedRepositories = $this->getLockedRepositories(); |
101
|
|
|
|
102
|
|
|
if($repositoryId !== null && $lockedRepositories->contains($repositoryId)) { |
103
|
|
|
$lockedRepositories = $lockedRepositories->filter(function($id) use($repositoryId) { |
104
|
|
|
return $id !== $repositoryId; |
105
|
|
|
}); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->configLock->isLocked(false)->by($lockedRepositories); |
109
|
|
|
|
110
|
|
|
$this->filesystem->put( |
111
|
|
|
$this->lockFilename, |
112
|
|
|
$this->serializer->serialize($this->configLock, 'json') |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
119
|
|
|
*/ |
120
|
|
|
public function load() { |
121
|
|
|
$config = $this->filesystem->get($this->configFile); |
122
|
|
|
|
123
|
|
|
return $config; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $config |
128
|
|
|
*/ |
129
|
|
|
public function updateWith($config) { |
130
|
|
|
$this->filesystem->put($this->configFile, $config); |
131
|
|
|
|
132
|
|
|
$this->filesystem->put(config('satis.public_mirror'), $this->configMirror->getPublicMirror($config)); |
133
|
|
|
$this->filesystem->put(config('satis.private_mirror'), $this->configMirror->getPrivateMirror($config)); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: