-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstalledPackagesTrait.php
102 lines (91 loc) · 2.84 KB
/
InstalledPackagesTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace CPSIT\Auditor;
use CPSIT\Auditor\Dto\Package;
/***************************************************************
* Copyright notice
*
* (c) 2019 Dirk Wenzel
* All rights reserved
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the text file GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Trait InstalledPackagesTrait
*/
trait InstalledPackagesTrait
{
/**
* @var array|null
*/
protected static $resolvedPackages;
/**
* @return array
*/
static public function getInstalledPackages(): array
{
if (self::propertyExists(DescriberInterface::INSTALLED_PACKAGES)) {
return self::$installedPackages;
}
return [];
}
/**
* @param string $propertyName
* @return bool
*/
static public function propertyExists(string $propertyName): bool
{
if (!property_exists(self::class, DescriberInterface::INSTALLED_PACKAGES)) {
throw new \OutOfBoundsException(
sprintf(DescriberInterface::ERROR_MISSING_PROPERTY, $propertyName, self::class),
1557047757
);
}
if (!static::arePackagesResolved()) {
static::resolvePackages();
}
return true;
}
/**
* Get a representation of an installed package
*
* @param string $name
* @return Package|null
*/
static public function getInstalledPackage(string $name): ?Package
{
if (!self::isPackageInstalled($name)) {
return null;
}
return new Package(self::$installedPackages[$name]);
}
/**
* Tells whether a package is installed
*
* @param string $name Package Name
* @return bool
*/
static public function isPackageInstalled(string $name): bool
{
self::propertyExists(DescriberInterface::INSTALLED_PACKAGES);
return (array_key_exists($name, self::$installedPackages));
}
protected static function arePackagesResolved(): bool
{
return is_array(static::$resolvedPackages);
}
protected static function resolvePackages(): void
{
if (!property_exists(static::class, DescriberInterface::INSTALLED_PACKAGES)) {
return;
}
static::$resolvedPackages = unserialize(static::${DescriberInterface::INSTALLED_PACKAGES}) ?: [];
}
}