-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPackage.php
195 lines (153 loc) · 4.6 KB
/
Package.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* JBZoo Toolbox - Composer-Graph.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Composer-Graph
*/
declare(strict_types=1);
namespace JBZoo\ComposerGraph;
class Package
{
public const MAIN = 'main';
public const DIRECT = 'direct';
public const PHP = 'php';
public const EXT = 'ext';
public const REQUIRED = 'require';
public const REQUIRED_DEV = 'require-dev';
public const SUGGEST = 'suggest';
public const HAS_META = 'has-meta';
public const INSTALLED = 'installed';
private string $name;
private string $version = '*';
private array $required = [];
private array $requiredDev = [];
private array $suggests = [];
private array $tags = [];
public function __construct(string $name, ?string $vendorDir = null)
{
$this->name = \strtolower($name);
if (
!\str_contains($this->name, '/')
&& (
\preg_match('#^ext-[a-z\d]*#', $this->name) > 0
|| \preg_match('#^lib-[a-z\d]*#', $this->name) > 0
)
) {
$this->addTags([self::EXT, self::HAS_META]);
if (
\extension_loaded($this->name)
|| \extension_loaded(\str_replace(['ext-', 'lib-'], '', $this->name))
) {
$this->addTags([self::INSTALLED]);
}
}
if (
$vendorDir !== null
&& $vendorDir !== ''
&& (\is_dir("{$vendorDir}/{$this->name}") || \is_dir("{$vendorDir}/{$name}"))
) {
$this->addTags([self::INSTALLED]);
}
}
public function setVersion(string $version): self
{
if ($version !== '') {
$this->version = \strtolower($version);
}
return $this;
}
public function addRequire(array $required): self
{
$this->required = \array_merge($this->required, $required);
return $this;
}
public function addRequireDev(array $requiredDev): self
{
$this->requiredDev = \array_merge($this->requiredDev, $requiredDev);
return $this;
}
public function addSuggest(array $suggest): self
{
$this->suggests = \array_merge($this->suggests, $suggest);
return $this;
}
public function addTags(array $tags): self
{
$this->tags = \array_unique(\array_merge($this->tags, $tags));
return $this;
}
public function isTag(string $tag): bool
{
return \in_array($tag, $this->tags, true);
}
public function isDirectPackage(): bool
{
return $this->isTag(self::DIRECT) && $this->isTag(self::REQUIRED);
}
public function isDirectPackageDev(): bool
{
return $this->isTag(self::DIRECT) && $this->isTag(self::REQUIRED_DEV);
}
public function isPlatform(): bool
{
return !$this->isMain() && ($this->isTag(self::PHP) || $this->isTag(self::EXT));
}
public function isPhp(): bool
{
return $this->isTag(self::PHP);
}
public function isPhpExt(): bool
{
return $this->isTag(self::EXT);
}
public function isMain(): bool
{
return $this->isTag(self::MAIN);
}
public function getName(bool $addVersion = true): string
{
$name = \strtolower(\trim($this->name));
if ($name === 'php') {
$name = 'PHP';
}
$prefixNoMeta = '';
if (!$this->isTag(self::HAS_META)) {
$prefixNoMeta = '* ';
}
if (!$addVersion) {
$result = $name;
} else {
$result = $this->version !== '' && $this->version !== '*'
? "{$name}@{$this->version}"
: $name;
}
return $prefixNoMeta . $result;
}
public function getRequired(): array
{
return $this->required;
}
public function getRequiredDev(): array
{
return $this->requiredDev;
}
public function getSuggested(): array
{
return $this->suggests;
}
public function getId(): string
{
return self::alias($this->getName(false));
}
public static function alias(string $string): string
{
$string = \strip_tags($string);
return \str_replace(['/', '-', 'graph', '(', ')', ' ', '*'], ['__', '_', 'g_raph', '', '', '', ''], $string);
}
}