This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Package.php
150 lines (134 loc) · 3.26 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
<?php
/**
* Copyright (C) php-fn. See LICENSE file for license details.
*/
namespace php;
/**
* @property-read string $name
* @property-read string $version
* @property-read string $dir
* @property-read string $homepage
* @property-read string $description
* @property-read bool $root
* @property-read array $authors
* @property-read array $extra
*/
class Package
{
use PropertiesTrait\ReadOnly;
use PropertiesTrait\Init;
private static $null;
private static $data;
private const CONSTANT = 'php\\PACKAGES';
/**
* @param string $name
* @param bool $assert
*
* @return Package|null
*/
public static function get(string $name, bool $assert = false): ?self
{
self::$null ?: self::$null = new static([]);
self::$data === null && self::$data = defined(self::CONSTANT) ? constant(self::CONSTANT) : [];
if ($package = self::$data[$name] ?? null) {
return new static($package);
}
return $assert ? null : self::$null;
}
/**
* @see $name
* @return string
*/
protected function resolveName(): ?string
{
return $this->properties['name'] ?? null;
}
/**
* @see $version
* @return string
*/
protected function resolveVersion(): ?string
{
return $this->properties['version'] ?? null;
}
/**
* @see $homepage
* @return string
*/
protected function resolveHomepage(): ?string
{
return $this->properties['homepage'] ?? null;
}
/**
* @see $description
* @return string
*/
protected function resolveDescription(): ?string
{
return $this->properties['description'] ?? null;
}
/**
* @see $dir
* @return string
*/
protected function resolveDir(): ?string
{
return $this->properties['dir'] ?? null;
}
/**
* @see $authors
* @return array
*/
protected function resolveAuthors(): array
{
return (array)($this->properties['authors'] ?? []);
}
/**
* @see $extra
* @return array
*/
protected function resolveExtra(): array
{
return (array)($this->properties['extra'] ?? []);
}
/**
* @see $root
* @return bool
*/
protected function resolveRoot(): bool
{
return (bool)($this->properties['root'] ?? false);
}
/**
* @param string ...$files
* @return string[]
*/
public function files(string ...$files): array
{
return traverse($files, [$this, 'file']);
}
/**
* @param string|null $file
* @return string
*/
public function file(string $file = null): ?string
{
return $file[0] === DIRECTORY_SEPARATOR ? $file : $this->dir . $file;
}
/**
* @param bool|int $format null => remove patch level if empty, int => number of levels
* @return string
*/
public function version($format = null): ?string
{
$version = $this->version;
if ($format === true) {
return $version;
}
$version = explode('.', $version);
if (!is_int($format) && ($version[3] ?? null) === '0') {
$format = 3;
}
return implode('.', array_slice($version, 0, $format));
}
}