1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/manager package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
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 Puli\Manager\Api\Cache; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Puli\Manager\Api\Package\InstallInfo; |
16
|
|
|
use Puli\Manager\Api\Package\PackageFile; |
17
|
|
|
use Puli\Manager\Assert\Assert; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Stores combined packages configuration. |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* |
24
|
|
|
* @author Mateusz Sojda <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class CacheFile |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $path; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var PackageFile[] |
35
|
|
|
*/ |
36
|
|
|
private $packageFiles = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var InstallInfo[] |
40
|
|
|
*/ |
41
|
|
|
private $installInfos = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates new CacheFile. |
45
|
|
|
* |
46
|
|
|
* @param string|null $path The path where the cache file is stored. |
47
|
|
|
*/ |
48
|
|
|
public function __construct($path = null) |
49
|
|
|
{ |
50
|
|
|
Assert::nullOrAbsoluteSystemPath($path); |
51
|
|
|
|
52
|
|
|
$this->path = $path; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the path to the cache file. |
57
|
|
|
* |
58
|
|
|
* @return string|null The path or `null` if this file is not stored on the |
59
|
|
|
* file system. |
60
|
|
|
*/ |
61
|
|
|
public function getPath() |
62
|
|
|
{ |
63
|
|
|
return $this->path; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the package files. |
68
|
|
|
* |
69
|
|
|
* @param PackageFile[] $packageFiles The package files. |
70
|
|
|
*/ |
71
|
|
|
public function setPackageFiles(array $packageFiles) |
72
|
|
|
{ |
73
|
|
|
$this->packageFiles = $packageFiles; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Adds a package to the cache file. |
78
|
|
|
* |
79
|
|
|
* @param PackageFile $packageFile The added package file. |
80
|
|
|
*/ |
81
|
|
|
public function addPackageFile(PackageFile $packageFile) |
82
|
|
|
{ |
83
|
|
|
$this->packageFiles[$packageFile->getPackageName()] = $packageFile; |
84
|
|
|
|
85
|
|
|
ksort($this->packageFiles); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Removes a package file from the cache file. |
90
|
|
|
* |
91
|
|
|
* @param string $packageName The package name. |
92
|
|
|
*/ |
93
|
|
|
public function removePackageFile($packageName) |
94
|
|
|
{ |
95
|
|
|
unset($this->packageFiles[$packageName]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Removes all package files from the cache file. |
100
|
|
|
*/ |
101
|
|
|
public function clearPackageFiles() |
102
|
|
|
{ |
103
|
|
|
$this->packageFiles = array(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the package file with the given name. |
108
|
|
|
* |
109
|
|
|
* @param string $packageName The package name. |
110
|
|
|
* |
111
|
|
|
* @return PackageFile The package with the passed name. |
112
|
|
|
* |
113
|
|
|
* @throws InvalidArgumentException If the package was not found. |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
public function getPackageFile($packageName) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
Assert::packageName($packageName); |
118
|
|
|
|
119
|
|
|
if (!isset($this->packageFiles[$packageName])) { |
120
|
|
|
throw new InvalidArgumentException(sprintf("Could not find a package named %s.", $packageName)); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->packageFiles[$packageName]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the package files in the cache file. |
128
|
|
|
* |
129
|
|
|
* @return PackageFile[] The package files in the cache file. |
130
|
|
|
*/ |
131
|
|
|
public function getPackageFiles() |
132
|
|
|
{ |
133
|
|
|
return $this->packageFiles; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns whether a package with the given name exists. |
138
|
|
|
* |
139
|
|
|
* @param string $packageName The package name. |
140
|
|
|
* |
141
|
|
|
* @return bool Whether a package with this name exists. |
142
|
|
|
*/ |
143
|
|
|
public function hasPackageFile($packageName) |
144
|
|
|
{ |
145
|
|
|
return isset($this->packageFiles[$packageName]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns whether a cache file contains any package files. |
150
|
|
|
* |
151
|
|
|
* @return bool Whether a cache file contains any package files. |
152
|
|
|
*/ |
153
|
|
|
public function hasPackageFiles() |
154
|
|
|
{ |
155
|
|
|
return count($this->packageFiles) > 0; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Adds install info to the cache file. |
160
|
|
|
* |
161
|
|
|
* @param InstallInfo $installInfo The package install info. |
162
|
|
|
*/ |
163
|
|
|
public function addInstallInfo(InstallInfo $installInfo) |
164
|
|
|
{ |
165
|
|
|
$this->installInfos[$installInfo->getPackageName()] = $installInfo; |
166
|
|
|
|
167
|
|
|
ksort($this->installInfos); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Removes install info file from the cache file. |
172
|
|
|
* |
173
|
|
|
* @param string $packageName The package name. |
174
|
|
|
*/ |
175
|
|
|
public function removeInstallInfo($packageName) |
176
|
|
|
{ |
177
|
|
|
unset($this->installInfos[$packageName]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Removes all package install info from the cache file. |
182
|
|
|
*/ |
183
|
|
|
public function clearInstallInfo() |
184
|
|
|
{ |
185
|
|
|
$this->installInfos = array(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns the install info with the given package name. |
190
|
|
|
* |
191
|
|
|
* @param string $packageName The package name. |
192
|
|
|
* |
193
|
|
|
* @return InstallInfo The package install info with the passed name. |
194
|
|
|
* |
195
|
|
|
* @throws InvalidArgumentException If the install info was not found. |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function getInstallInfo($packageName) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
Assert::packageName($packageName); |
200
|
|
|
|
201
|
|
|
if (!isset($this->installInfos[$packageName])) { |
202
|
|
|
throw new InvalidArgumentException(sprintf("Could not find a package named %s.", $packageName)); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $this->installInfos[$packageName]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the install info for all packages in the cache file. |
210
|
|
|
* |
211
|
|
|
* @return InstallInfo[] The install info for all packages in the cache file. |
212
|
|
|
*/ |
213
|
|
|
public function getInstallInfos() |
214
|
|
|
{ |
215
|
|
|
return $this->installInfos; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Returns whether install info for a package with the given name exists. |
220
|
|
|
* |
221
|
|
|
* @param string $packageName The package name. |
222
|
|
|
* |
223
|
|
|
* @return bool Whether install info for a package with this name exists. |
224
|
|
|
*/ |
225
|
|
|
public function hasInstallInfo($packageName) |
226
|
|
|
{ |
227
|
|
|
return isset($this->installInfos[$packageName]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns whether a cache file contains any package install info. |
232
|
|
|
* |
233
|
|
|
* @return bool Whether a cache file contains any package install info. |
234
|
|
|
*/ |
235
|
|
|
public function hasInstallInfos() |
236
|
|
|
{ |
237
|
|
|
return count($this->installInfos) > 0; |
238
|
|
|
} |
239
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.