1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Geekish\Crap; |
4
|
|
|
|
5
|
|
|
use Composer\Package\Version\VersionParser; |
6
|
|
|
use Exception; |
7
|
|
|
use Webmozart\KeyValueStore\JsonFileStore; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CrapHelper |
11
|
|
|
* @package Geekish\Crap |
12
|
|
|
*/ |
13
|
|
|
class CrapHelper |
14
|
|
|
{ |
15
|
|
|
/** @var JsonFileStore */ |
16
|
|
|
private $file; |
17
|
|
|
|
18
|
|
|
/** @var VersionParser */ |
19
|
|
|
private $parser; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Construct CrapHelper |
23
|
|
|
* |
24
|
|
|
* @param JsonFileStore $file |
25
|
|
|
* @param VersionParser $parser |
26
|
|
|
*/ |
27
|
|
|
public function __construct(JsonFileStore $file, VersionParser $parser) |
28
|
|
|
{ |
29
|
|
|
$this->file = $file; |
30
|
|
|
$this->parser = $parser; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get JsonFileStore |
35
|
|
|
* |
36
|
|
|
* @return JsonFileStore |
37
|
|
|
*/ |
38
|
|
|
public function getFile() |
39
|
|
|
{ |
40
|
|
|
return $this->file; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set JsonFileStore |
45
|
|
|
* |
46
|
|
|
* @param JsonFileStore $file |
47
|
|
|
*/ |
48
|
|
|
public function setFile(JsonFileStore $file) |
49
|
|
|
{ |
50
|
|
|
$this->file = $file; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get VersionParser |
55
|
|
|
* |
56
|
|
|
* @return VersionParser |
57
|
|
|
*/ |
58
|
|
|
public function getVersionParser() |
59
|
|
|
{ |
60
|
|
|
return $this->parser; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get aliases from FileStore |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getAliases() |
69
|
|
|
{ |
70
|
|
|
return $this->file->keys(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get alias from FileStore |
75
|
|
|
* |
76
|
|
|
* @param string $alias |
77
|
|
|
* @throws Exception If alias is not found in JSON file. |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getAlias($alias) |
81
|
|
|
{ |
82
|
|
|
return $this->file->get($alias); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set alias in FileStore |
87
|
|
|
* |
88
|
|
|
* @param string $alias |
89
|
|
|
* @param $package |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function setAlias($alias, $package) |
93
|
|
|
{ |
94
|
|
|
$this->file->set($alias, $package); |
95
|
|
|
$this->file->sort(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Remove alias from FileStore |
100
|
|
|
* |
101
|
|
|
* @param string $alias |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function unsetAlias($alias) |
105
|
|
|
{ |
106
|
|
|
return $this->file->remove($alias); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check alias exists in FileStore |
111
|
|
|
* |
112
|
|
|
* @param string $alias |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function hasAlias($alias) |
116
|
|
|
{ |
117
|
|
|
return $this->file->exists($alias); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check that alias is valid |
122
|
|
|
* |
123
|
|
|
* @param string $alias |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function validateAlias($alias) |
127
|
|
|
{ |
128
|
|
|
return (bool) preg_match("{^[a-z0-9_.-]+$}", $alias); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Make sure provided package string is valid |
133
|
|
|
* |
134
|
|
|
* @param string $input |
135
|
|
|
* @return boolean |
136
|
|
|
*/ |
137
|
|
|
public function validatePackage($input) |
138
|
|
|
{ |
139
|
|
|
if (empty($input)) { |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
list($package, $version) = $this->parsePackageToArray($input); |
144
|
|
|
|
145
|
|
|
if (!preg_match("{^[a-z0-9_.-]+/[a-z0-9_.-]+$}", $package)) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($version !== null) { |
150
|
|
|
try { |
151
|
|
|
$this->parser->parseConstraints($version); |
152
|
|
|
} catch (Exception $e) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Parse package argument array to string |
162
|
|
|
* Version is null if no constraint is provided |
163
|
|
|
* |
164
|
|
|
* @param array $arguments |
165
|
|
|
* @param bool $excludeVersions |
166
|
|
|
* @return array |
167
|
|
|
* |
168
|
|
|
* @throws CrapException If the alias is not set with crap |
169
|
|
|
*/ |
170
|
|
|
public function parseArguments(array $arguments, $excludeVersions = false) |
171
|
|
|
{ |
172
|
|
|
return array_map(function ($arg) use ($excludeVersions) { |
173
|
|
|
if ($this->validatePackage($arg)) { |
174
|
|
|
return $arg; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
list($alias, $argVersion) = $this->parsePackageToArray($arg); |
178
|
|
|
|
179
|
|
|
if (!$this->hasAlias($alias)) { |
180
|
|
|
throw CrapException::create("No record found for alias `%s`.", $alias); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
list($package, $packageVersion) = $this->parsePackageToArray($this->getAlias($alias)); |
184
|
|
|
|
185
|
|
|
$version = null; |
186
|
|
|
|
187
|
|
|
if (!$excludeVersions) { |
188
|
|
|
$version = $argVersion ?: $packageVersion; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return (is_null($version)) ? $package : sprintf('%s:%s', $package, $version); |
192
|
|
|
}, $arguments); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Parse package argument string to array[package, version] |
197
|
|
|
* Version is null if no constraint is provided |
198
|
|
|
* |
199
|
|
|
* @param string $input |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
protected function parsePackageToArray($input) |
203
|
|
|
{ |
204
|
|
|
$result = $this->parser->parseNameVersionPairs([$input])[0]; |
205
|
|
|
|
206
|
|
|
if (!isset($result['version'])) { |
207
|
|
|
return [$result['name'], null]; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return [$result['name'], $result['version']]; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|