|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PackagesManager.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license https://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec <[email protected]> |
|
8
|
|
|
* @package iPublikuj:Packages! |
|
9
|
|
|
* @subpackage common |
|
10
|
|
|
* @since 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 30.05.15 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
|
16
|
|
|
|
|
17
|
|
|
namespace IPub\Packages; |
|
18
|
|
|
|
|
19
|
|
|
use Nette; |
|
20
|
|
|
use Nette\Utils; |
|
21
|
|
|
|
|
22
|
|
|
use IPub\Packages\DependencyResolver; |
|
23
|
|
|
use IPub\Packages\Entities; |
|
24
|
|
|
use IPub\Packages\Exceptions; |
|
25
|
|
|
use IPub\Packages\Installers; |
|
26
|
|
|
use IPub\Packages\Repository; |
|
27
|
|
|
use IPub\Packages\Scripts; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Packages manager |
|
31
|
|
|
* |
|
32
|
|
|
* @package iPublikuj:Packages! |
|
33
|
|
|
* @subpackage common |
|
34
|
|
|
* |
|
35
|
|
|
* @author Adam Kadlec <[email protected]> |
|
36
|
|
|
* |
|
37
|
|
|
* @method onEnable(PackagesManager $manager, Entities\IPackage $package) |
|
38
|
|
|
* @method onDisable(PackagesManager $manager, Entities\IPackage $package) |
|
39
|
|
|
* @method onUpgrade(PackagesManager $manager, Entities\IPackage $package) |
|
40
|
|
|
* @method onInstall(PackagesManager $manager, Entities\IPackage $package) |
|
41
|
|
|
* @method onUninstall(PackagesManager $manager, Entities\IPackage $package) |
|
42
|
|
|
* @method onRegister(PackagesManager $manager, Entities\IPackage $package) |
|
43
|
|
|
* @method onUnregister(PackagesManager $manager, Entities\IPackage $package) |
|
44
|
|
|
*/ |
|
45
|
1 |
|
final class PackagesManager implements IPackagesManager |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* Implement nette smart magic |
|
49
|
|
|
*/ |
|
50
|
1 |
|
use Nette\SmartObject; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Define package metadata keys |
|
54
|
|
|
*/ |
|
55
|
|
|
private const PACKAGE_STATUS = 'status'; |
|
56
|
|
|
private const PACKAGE_METADATA = 'metadata'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Define actions |
|
60
|
|
|
*/ |
|
61
|
|
|
private const ACTION_ENABLE = 'enable'; |
|
62
|
|
|
private const ACTION_DISABLE = 'disable'; |
|
63
|
|
|
private const ACTION_REGISTER = 'register'; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var callable[] |
|
67
|
|
|
*/ |
|
68
|
|
|
public $onEnable = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var callable[] |
|
72
|
|
|
*/ |
|
73
|
|
|
public $onDisable = []; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var callable[] |
|
77
|
|
|
*/ |
|
78
|
|
|
public $onUpgrade = []; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var callable[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public $onInstall = []; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var callable[] |
|
87
|
|
|
*/ |
|
88
|
|
|
public $onUninstall = []; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @var callable[] |
|
92
|
|
|
*/ |
|
93
|
|
|
public $onRegister = []; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @var callable[] |
|
97
|
|
|
*/ |
|
98
|
|
|
public $onUnregister = []; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @var string |
|
102
|
|
|
*/ |
|
103
|
|
|
private $vendorDir; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @var string |
|
107
|
|
|
*/ |
|
108
|
|
|
private $configDir; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @var Repository\IRepository |
|
112
|
|
|
*/ |
|
113
|
|
|
private $repository; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @var Installers\IInstaller|NULL |
|
117
|
|
|
*/ |
|
118
|
|
|
private $installer; |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @var DependencyResolver\Solver |
|
122
|
|
|
*/ |
|
123
|
|
|
private $dependencySolver; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @var Utils\ArrayHash |
|
127
|
|
|
*/ |
|
128
|
|
|
private $packagesConfig; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @var Scripts\IScript[] |
|
132
|
|
|
*/ |
|
133
|
|
|
private $scripts = []; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $vendorDir |
|
137
|
|
|
* @param string $configDir |
|
138
|
|
|
* @param Repository\IRepository $repository |
|
139
|
|
|
* @param Installers\IInstaller|NULL $installer |
|
140
|
|
|
*/ |
|
141
|
|
|
public function __construct( |
|
142
|
|
|
string $vendorDir, |
|
143
|
|
|
string $configDir, |
|
144
|
|
|
Repository\IRepository $repository, |
|
145
|
|
|
Installers\IInstaller $installer = NULL |
|
146
|
|
|
) { |
|
147
|
1 |
|
$this->vendorDir = $vendorDir; |
|
148
|
1 |
|
$this->configDir = $configDir; |
|
149
|
|
|
|
|
150
|
1 |
|
$this->repository = $repository; |
|
151
|
1 |
|
$this->installer = $installer; |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getStatus(Entities\IPackage $package) : string |
|
158
|
|
|
{ |
|
159
|
|
|
$packageConfig = $this->getPackagesConfig(); |
|
160
|
|
|
|
|
161
|
|
|
if (!$packageConfig->offsetExists($package->getName()) || !isset($packageConfig[$package->getName()][self::PACKAGE_STATUS])) { |
|
162
|
|
|
return Entities\IPackage::STATE_UNREGISTERED; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $packageConfig[$package->getName()][self::PACKAGE_STATUS]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getVersion(Entities\IPackage $package) : string |
|
172
|
|
|
{ |
|
173
|
|
|
if (!$path = $package->getPath()) { |
|
174
|
|
|
throw new \RuntimeException('Package path is missing.'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (!file_exists($file = $path . DIRECTORY_SEPARATOR . 'composer.json')) { |
|
178
|
|
|
throw new \RuntimeException('\'composer.json\' is missing.'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$packageData = Utils\Json::decode(file_get_contents($file), Utils\Json::FORCE_ARRAY); |
|
182
|
|
|
|
|
183
|
|
|
if (isset($packageData['version'])) { |
|
184
|
|
|
return $packageData['version']; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if (file_exists($file = $this->vendorDir . DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR . 'installed.json')) { |
|
188
|
|
|
$installed = Utils\Json::decode(file_get_contents($file), Utils\Json::FORCE_ARRAY); |
|
189
|
|
|
|
|
190
|
|
|
foreach ($installed as $packageData) { |
|
191
|
|
|
if ($packageData['name'] === $package->getName()) { |
|
192
|
|
|
return $packageData['version']; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return '0.0.0'; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
*/ |
|
203
|
|
|
public function comparePackages(Entities\IPackage $first, Entities\IPackage $second, string $operator = '==') : bool |
|
204
|
|
|
{ |
|
205
|
|
|
return strtolower($first->getName()) === strtolower($second->getName()) && |
|
206
|
|
|
version_compare(strtolower($this->getVersion($first)), strtolower($this->getVersion($second)), $operator); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* {@inheritdoc} |
|
211
|
|
|
*/ |
|
212
|
|
|
public function addScript(string $name, Scripts\IScript $service) |
|
213
|
|
|
{ |
|
214
|
1 |
|
$this->scripts[$name] = $service; |
|
215
|
1 |
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* {@inheritdoc} |
|
219
|
|
|
*/ |
|
220
|
|
|
public function registerAvailable() : array |
|
221
|
|
|
{ |
|
222
|
|
|
$actions = []; |
|
223
|
|
|
|
|
224
|
|
|
$installedPackages = array_keys($this->repository->getPackages()); |
|
225
|
|
|
$registeredPackages = array_keys((array) $this->getPackagesConfig()); |
|
226
|
|
|
|
|
227
|
|
|
foreach (array_diff($installedPackages, $registeredPackages) as $name) { |
|
228
|
|
|
/** @var Entities\IPackage $package */ |
|
229
|
|
|
if ($package = $this->repository->findPackage($name)) { |
|
230
|
|
|
$this->register($package); |
|
231
|
|
|
$actions[] = [$name => self::ACTION_REGISTER]; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $actions; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* {@inheritdoc} |
|
240
|
|
|
*/ |
|
241
|
|
|
public function enableAvailable() : array |
|
242
|
|
|
{ |
|
243
|
|
|
$actions = []; |
|
244
|
|
|
|
|
245
|
|
|
while (TRUE) { |
|
246
|
|
|
/** @var Entities\IPackage[] $packages */ |
|
247
|
|
|
$packages = $this->repository->filterPackages(function (Entities\IPackage $package) : bool { |
|
248
|
|
|
return $this->getStatus($package) === Entities\IPackage::STATE_DISABLED; |
|
249
|
|
|
}); |
|
250
|
|
|
|
|
251
|
|
|
if (!count($packages)) { |
|
252
|
|
|
break; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** @var Entities\IPackage $package */ |
|
256
|
|
|
$package = reset($packages); |
|
257
|
|
|
|
|
258
|
|
|
foreach ($this->testEnable($package)->getSolutions() as $job) { |
|
259
|
|
|
if ($job->getAction() === DependencyResolver\Job::ACTION_ENABLE) { |
|
260
|
|
|
$this->enable($job->getPackage()); |
|
261
|
|
|
$actions[] = [$job->getPackage()->getName() => self::ACTION_ENABLE]; |
|
262
|
|
|
|
|
263
|
|
|
} elseif ($job->getAction() === DependencyResolver\Job::ACTION_DISABLE) { |
|
264
|
|
|
$this->disable($job->getPackage()); |
|
265
|
|
|
$actions[] = [$job->getPackage()->getName() => self::ACTION_DISABLE]; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
$this->enable($package); |
|
270
|
|
|
|
|
271
|
|
|
$this->dependencySolver = NULL; |
|
272
|
|
|
|
|
273
|
|
|
$actions[] = [$package->getName() => self::ACTION_ENABLE]; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return $actions; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* {@inheritdoc} |
|
281
|
|
|
*/ |
|
282
|
|
|
public function disableAbsent() : array |
|
283
|
|
|
{ |
|
284
|
|
|
$actions = []; |
|
285
|
|
|
|
|
286
|
|
|
$installedPackages = array_keys($this->repository->getPackages()); |
|
287
|
|
|
$registeredPackages = array_keys((array) $this->getPackagesConfig()); |
|
288
|
|
|
|
|
289
|
|
|
foreach (array_diff($registeredPackages, $installedPackages) as $name) { |
|
290
|
|
|
/** @var Entities\IPackage $package */ |
|
291
|
|
|
if ($package = $this->repository->findPackage($name)) { |
|
292
|
|
|
if ($this->getStatus($package) === Entities\IPackage::STATE_ENABLED) { |
|
293
|
|
|
$this->disable($package); |
|
294
|
|
|
$actions[] = [$name => self::ACTION_DISABLE]; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
$this->disable($package); |
|
298
|
|
|
|
|
299
|
|
|
$actions[] = [$package->getName() => self::ACTION_DISABLE]; |
|
|
|
|
|
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
return $actions; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* {@inheritdoc} |
|
308
|
|
|
*/ |
|
309
|
|
|
public function install(string $name) : void |
|
310
|
|
|
{ |
|
311
|
|
|
// Check if installer service is created |
|
312
|
|
|
if ($this->installer === NULL) { |
|
313
|
|
|
new Exceptions\InvalidStateException('Packages installer service is not created.'); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
// Check if package is not already installed |
|
317
|
|
|
if ($this->repository->findPackage($name)) { |
|
318
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Package "%s" is already installed', $name)); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$this->installer->install($name); |
|
322
|
|
|
|
|
323
|
|
|
// Reload repository after installation |
|
324
|
|
|
$this->repository->reload(); |
|
325
|
|
|
|
|
326
|
|
|
// Get newly installed package |
|
327
|
|
|
if (!$package = $this->repository->findPackage($name)) { |
|
328
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Package "%s" could not be found.', $name)); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
// Process all package scripts |
|
332
|
|
|
foreach ($package->getScripts() as $class) { |
|
|
|
|
|
|
333
|
|
|
try { |
|
334
|
|
|
$script = $this->getScript($class); |
|
335
|
|
|
$script->install($package); |
|
|
|
|
|
|
336
|
|
|
|
|
337
|
|
|
} catch (\Exception $e) { |
|
338
|
|
|
foreach ($package->getScripts() as $class2) { |
|
|
|
|
|
|
339
|
|
|
if ($class === $class2) { |
|
340
|
|
|
break; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
$script = $this->getScript($class2); |
|
344
|
|
|
$script->uninstall($package); |
|
|
|
|
|
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
throw new Exceptions\InvalidStateException($e->getMessage()); |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
$this->register($package); |
|
352
|
|
|
|
|
353
|
|
|
$this->onInstall($this, $package); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* {@inheritdoc} |
|
358
|
|
|
*/ |
|
359
|
|
|
public function uninstall(string $name) : void |
|
360
|
|
|
{ |
|
361
|
|
|
// Check if installer service is created |
|
362
|
|
|
if ($this->installer === NULL) { |
|
363
|
|
|
new Exceptions\InvalidStateException('Packages installer service is not created.'); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
// Check if package is installed |
|
367
|
|
|
if (!$package = $this->repository->findPackage($name)) { |
|
368
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Package "%s" is already uninstalled', $name)); |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
// If package is still enabled, disable it first |
|
372
|
|
|
if ($this->getStatus($package) === Entities\IPackage::STATE_ENABLED) { |
|
373
|
|
|
$this->disable($package); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
// Process all package scripts |
|
377
|
|
|
foreach ($package->getScripts() as $class) { |
|
378
|
|
|
try { |
|
379
|
|
|
$script = $this->getScript($class); |
|
380
|
|
|
$script->uninstall($package); |
|
|
|
|
|
|
381
|
|
|
|
|
382
|
|
|
} catch (\Exception $e) { |
|
383
|
|
|
foreach ($package->getScripts() as $class2) { |
|
384
|
|
|
if ($class === $class2) { |
|
385
|
|
|
break; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
$script = $this->getScript($class2); |
|
389
|
|
|
$script->install($package); |
|
|
|
|
|
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
throw new Exceptions\InvalidStateException($e->getMessage()); |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
$this->unregister($package); |
|
397
|
|
|
|
|
398
|
|
|
if ($this->installer->isInstalled($package->getName())) { |
|
399
|
|
|
$this->installer->uninstall($package->getName()); |
|
400
|
|
|
|
|
401
|
|
|
} else { |
|
402
|
|
|
if (!$path = $package->getPath()) { |
|
403
|
|
|
throw new Exceptions\InvalidStateException('Package path is missing.'); |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
$this->output->writeln("Removing package folder."); |
|
407
|
|
|
|
|
408
|
|
|
Utils\FileSystem::delete($path); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
// Reload repository after uninstallation |
|
412
|
|
|
$this->repository->reload(); |
|
413
|
|
|
|
|
414
|
|
|
$this->onUninstall($this, $package); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* {@inheritdoc} |
|
419
|
|
|
*/ |
|
420
|
|
|
public function enable(Entities\IPackage $package) : void |
|
421
|
|
|
{ |
|
422
|
|
|
if ($this->getStatus($package) === Entities\IPackage::STATE_ENABLED) { |
|
423
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Package \'%s\' is already enabled', $package->getName())); |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
$dependencyResolver = $this->getDependencySolver(); |
|
427
|
|
|
$dependencyResolver->testEnable($package); |
|
428
|
|
|
|
|
429
|
|
|
foreach ($package->getScripts() as $class) { |
|
430
|
|
|
try { |
|
431
|
|
|
$installer = $this->getScript($class); |
|
432
|
|
|
$installer->enable($package); |
|
433
|
|
|
|
|
434
|
|
|
} catch (\Exception $ex) { |
|
435
|
|
|
foreach ($package->getScripts() as $class2) { |
|
436
|
|
|
if ($class === $class2) { |
|
437
|
|
|
break; |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
$installer = $this->getScript($class2); |
|
441
|
|
|
$installer->disable($package); |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
throw new Exceptions\InvalidStateException($ex->getMessage()); |
|
445
|
|
|
} |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
$this->setStatus($package, Entities\IPackage::STATE_ENABLED); |
|
449
|
|
|
|
|
450
|
|
|
$this->onEnable($this, $package); |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* {@inheritdoc} |
|
455
|
|
|
*/ |
|
456
|
|
|
public function disable(Entities\IPackage $package) : void |
|
457
|
|
|
{ |
|
458
|
|
|
if ($this->getStatus($package) === Entities\IPackage::STATE_DISABLED) { |
|
459
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Package \'%s\' is already disabled', $package->getName())); |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
$dependencyResolver = $this->getDependencySolver(); |
|
463
|
|
|
$dependencyResolver->testDisable($package); |
|
464
|
|
|
|
|
465
|
|
|
foreach ($package->getScripts() as $class) { |
|
466
|
|
|
try { |
|
467
|
|
|
$installer = $this->getScript($class); |
|
468
|
|
|
$installer->disable($package); |
|
469
|
|
|
|
|
470
|
|
|
} catch (\Exception $e) { |
|
471
|
|
|
foreach ($package->getScripts() as $class2) { |
|
472
|
|
|
if ($class === $class2) { |
|
473
|
|
|
break; |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
$installer = $this->getScript($class2); |
|
477
|
|
|
$installer->enable($package); |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
throw new Exceptions\InvalidStateException($e->getMessage()); |
|
481
|
|
|
} |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
$this->setStatus($package, Entities\IPackage::STATE_DISABLED); |
|
485
|
|
|
|
|
486
|
|
|
$this->onDisable($this, $package); |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* {@inheritdoc} |
|
491
|
|
|
*/ |
|
492
|
|
|
public function testEnable(Entities\IPackage $package) : DependencyResolver\Problem |
|
493
|
|
|
{ |
|
494
|
|
|
$problem = new DependencyResolver\Problem; |
|
495
|
|
|
|
|
496
|
|
|
$dependencyResolver = $this->getDependencySolver(); |
|
497
|
|
|
$dependencyResolver->testEnable($package, $problem); |
|
498
|
|
|
|
|
499
|
|
|
return $problem; |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* {@inheritdoc} |
|
504
|
|
|
*/ |
|
505
|
|
|
public function testDisable(Entities\IPackage $package) : DependencyResolver\Problem |
|
506
|
|
|
{ |
|
507
|
|
|
$problem = new DependencyResolver\Problem; |
|
508
|
|
|
|
|
509
|
|
|
$dependencyResolver = $this->getDependencySolver(); |
|
510
|
|
|
$dependencyResolver->testDisable($package, $problem); |
|
511
|
|
|
|
|
512
|
|
|
return $problem; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* @param Entities\IPackage $package |
|
517
|
|
|
* @param string $state |
|
518
|
|
|
* |
|
519
|
|
|
* @return void |
|
520
|
|
|
*/ |
|
521
|
|
|
private function register(Entities\IPackage $package, string $state = Entities\IPackage::STATE_DISABLED) : void |
|
522
|
|
|
{ |
|
523
|
|
|
$packagesConfig = $this->getPackagesConfig(); |
|
524
|
|
|
|
|
525
|
|
|
if (!$packagesConfig->offsetExists($package->getName())) { |
|
526
|
|
|
// Create package config metadata |
|
527
|
|
|
$packagesConfig[$package->getName()] = [ |
|
528
|
|
|
self::PACKAGE_STATUS => $state, |
|
529
|
|
|
self::PACKAGE_METADATA => [ |
|
530
|
|
|
'authors' => array_merge((array) $package->getAuthors()), |
|
531
|
|
|
'description' => $package->getDescription(), |
|
532
|
|
|
'keywords' => array_merge((array) $package->getKeywords()), |
|
533
|
|
|
'license' => array_merge((array) $package->getLicense()), |
|
534
|
|
|
'require' => $package->getRequire(), |
|
535
|
|
|
'extra' => [ |
|
536
|
|
|
'ipub' => [ |
|
537
|
|
|
'configuration' => array_merge((array) $package->getConfiguration()), |
|
538
|
|
|
'scripts' => $package->getScripts(), |
|
539
|
|
|
], |
|
540
|
|
|
], |
|
541
|
|
|
], |
|
542
|
|
|
]; |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
$this->savePackagesConfig($packagesConfig); |
|
546
|
|
|
|
|
547
|
|
|
$this->onRegister($this, $package); |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* @param Entities\IPackage $package |
|
552
|
|
|
* |
|
553
|
|
|
* @return void |
|
554
|
|
|
*/ |
|
555
|
|
|
private function unregister(Entities\IPackage $package) : void |
|
556
|
|
|
{ |
|
557
|
|
|
$packagesConfig = $this->getPackagesConfig(); |
|
558
|
|
|
|
|
559
|
|
|
// Remove package info from configuration file |
|
560
|
|
|
unset($packagesConfig[$package->getName()]); |
|
561
|
|
|
|
|
562
|
|
|
$this->savePackagesConfig($packagesConfig); |
|
563
|
|
|
|
|
564
|
|
|
$this->onUnregister($this, $package); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
/** |
|
568
|
|
|
* @param Entities\IPackage $package |
|
569
|
|
|
* @param string $status |
|
570
|
|
|
* |
|
571
|
|
|
* @return void |
|
572
|
|
|
*/ |
|
573
|
|
|
private function setStatus(Entities\IPackage $package, string $status) : void |
|
574
|
|
|
{ |
|
575
|
|
|
if (!in_array($status, Entities\IPackage::STATUSES, TRUE)) { |
|
576
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Status \'%s\' not exists.', $status)); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
$packagesConfig = $this->getPackagesConfig(); |
|
580
|
|
|
|
|
581
|
|
|
// Check if package is registered |
|
582
|
|
|
if (!$packagesConfig->offsetExists($package->getName())) { |
|
583
|
|
|
throw new Exceptions\InvalidStateException(sprintf('Package "%s" is not registered. Please call ' . get_called_class() . '::registerAvailable first.', $package->getName())); |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
$packagesConfig[$package->getName()][self::PACKAGE_STATUS] = $status; |
|
587
|
|
|
|
|
588
|
|
|
$this->savePackagesConfig($packagesConfig); |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* @param string $class |
|
593
|
|
|
* |
|
594
|
|
|
* @return Scripts\IScript |
|
595
|
|
|
* |
|
596
|
|
|
* @throws Exceptions\InvalidStateException |
|
597
|
|
|
*/ |
|
598
|
|
|
private function getScript(string $class) : Scripts\IScript |
|
599
|
|
|
{ |
|
600
|
|
|
if (isset($this->scripts[$class])) { |
|
601
|
|
|
return $this->scripts[$class]; |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
throw new Exceptions\InvalidStateException(sprintf('Package script "%s" was not found.', $class)); |
|
605
|
|
|
} |
|
606
|
|
|
|
|
607
|
|
|
/** |
|
608
|
|
|
* @return DependencyResolver\Solver |
|
609
|
|
|
*/ |
|
610
|
|
|
private function getDependencySolver() : DependencyResolver\Solver |
|
611
|
|
|
{ |
|
612
|
|
|
if ($this->dependencySolver === NULL) { |
|
613
|
|
|
$this->createSolver(); |
|
614
|
|
|
} |
|
615
|
|
|
|
|
616
|
|
|
return $this->dependencySolver; |
|
617
|
|
|
} |
|
618
|
|
|
|
|
619
|
|
|
/** |
|
620
|
|
|
* @return void |
|
621
|
|
|
*/ |
|
622
|
|
|
private function createSolver() : void |
|
623
|
|
|
{ |
|
624
|
|
|
$this->dependencySolver = new DependencyResolver\Solver($this->repository, $this); |
|
625
|
|
|
} |
|
626
|
|
|
|
|
627
|
|
|
/** |
|
628
|
|
|
* @return Utils\ArrayHash |
|
629
|
|
|
*/ |
|
630
|
|
|
private function getPackagesConfig() : Utils\ArrayHash |
|
631
|
|
|
{ |
|
632
|
|
|
if ($this->packagesConfig === NULL) { |
|
633
|
|
|
$config = new Nette\DI\Config\Adapters\PhpAdapter; |
|
634
|
|
|
|
|
635
|
|
|
if (!is_file($this->getPackageConfigPath())) { |
|
636
|
|
|
file_put_contents($this->getPackageConfigPath(), $config->dump([])); |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
$this->packagesConfig = Utils\ArrayHash::from($config->load($this->getPackageConfigPath())); |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
return $this->packagesConfig; |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
/** |
|
646
|
|
|
* @param Utils\ArrayHash $packagesConfig |
|
647
|
|
|
* |
|
648
|
|
|
* @return void |
|
649
|
|
|
* |
|
650
|
|
|
* @throws Exceptions\NotWritableException |
|
651
|
|
|
*/ |
|
652
|
|
|
private function savePackagesConfig(Utils\ArrayHash $packagesConfig) : void |
|
653
|
|
|
{ |
|
654
|
|
|
$config = new Nette\DI\Config\Adapters\PhpAdapter; |
|
655
|
|
|
|
|
656
|
|
|
if (file_put_contents($this->getPackageConfigPath(), $config->dump(array_merge((array) $packagesConfig))) === FALSE) { |
|
657
|
|
|
throw new Exceptions\NotWritableException(sprintf('Packages configuration file "%s" is not writable.', $this->getPackageConfigPath())); |
|
658
|
|
|
}; |
|
659
|
|
|
|
|
660
|
|
|
// Refresh packages config data |
|
661
|
|
|
$this->packagesConfig = $packagesConfig; |
|
662
|
|
|
} |
|
663
|
|
|
|
|
664
|
|
|
/** |
|
665
|
|
|
* @return string |
|
666
|
|
|
*/ |
|
667
|
|
|
private function getPackageConfigPath() : string |
|
668
|
|
|
{ |
|
669
|
|
|
return $this->configDir . DIRECTORY_SEPARATOR . 'packages.php'; |
|
670
|
|
|
} |
|
671
|
|
|
} |
|
672
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.