|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Pickle |
|
5
|
|
|
* |
|
6
|
|
|
* |
|
7
|
|
|
* @license |
|
8
|
|
|
* |
|
9
|
|
|
* New BSD License |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright © 2015-2015, Pickle community. All rights reserved. |
|
12
|
|
|
* |
|
13
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
14
|
|
|
* modification, are permitted provided that the following conditions are met: |
|
15
|
|
|
* * Redistributions of source code must retain the above copyright |
|
16
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
17
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
18
|
|
|
* notice, this list of conditions and the following disclaimer in the |
|
19
|
|
|
* documentation and/or other materials provided with the distribution. |
|
20
|
|
|
* * Neither the name of the Hoa nor the names of its contributors may be |
|
21
|
|
|
* used to endorse or promote products derived from this software without |
|
22
|
|
|
* specific prior written permission. |
|
23
|
|
|
* |
|
24
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
25
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
26
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
27
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
|
28
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
29
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
30
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
31
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
32
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
33
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Pickle\Package\Util; |
|
38
|
|
|
|
|
39
|
|
|
use Composer\Package\Loader\LoaderInterface; |
|
40
|
|
|
use Composer\Package\Version\VersionParser; |
|
41
|
|
|
use DateTime; |
|
42
|
|
|
use DateTimeZone; |
|
43
|
|
|
use Exception; |
|
44
|
|
|
use Pickle\Base\Interfaces; |
|
45
|
|
|
use Pickle\Package; |
|
46
|
|
|
use UnexpectedValueException; |
|
47
|
|
|
|
|
48
|
|
|
class Loader implements LoaderInterface |
|
49
|
|
|
{ |
|
50
|
|
|
protected $versionParser; |
|
51
|
|
|
|
|
52
|
|
|
protected $loadOptions; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct(?VersionParser $parser = null, $loadOptions = false) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->versionParser = $parser ?: new VersionParser(); |
|
57
|
|
|
$this->loadOptions = $loadOptions; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $package |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Pickle\Base\Interfaces\Package $package |
|
64
|
|
|
*/ |
|
65
|
|
|
public function load(array $config, $package = 'Pickle\Base\Interfaces\Package') |
|
66
|
|
|
{ |
|
67
|
|
|
if (isset($config['version'])) { |
|
68
|
|
|
$version = $this->versionParser->normalize($config['version']); |
|
69
|
|
|
$package = Package::factory($config['name'], $version, $config['version'], true); |
|
70
|
|
|
} else { |
|
71
|
|
|
$package = Package::factory($config['name'], '', '', true); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (isset($config['type']) && $config['type'] != 'extension') { |
|
75
|
|
|
throw new UnexpectedValueException($package->getName() . ' is not a extension(s) package'); |
|
76
|
|
|
} |
|
77
|
|
|
$package->setType('extension'); |
|
78
|
|
|
|
|
79
|
|
|
$this->setPackageSource($package, $config); |
|
80
|
|
|
$this->setPackageDist($package, $config); |
|
81
|
|
|
$this->setPackageReleaseDate($package, $config); |
|
82
|
|
|
$this->setPackageStability($package, $config); |
|
83
|
|
|
$this->setPackageExtra($package, $config); |
|
84
|
|
|
$this->setPackageDescription($package, $config); |
|
85
|
|
|
$this->setPackageHomepage($package, $config); |
|
86
|
|
|
$this->setPackageKeywords($package, $config); |
|
87
|
|
|
$this->setPackageLicense($package, $config); |
|
88
|
|
|
$this->setPackageAuthors($package, $config); |
|
89
|
|
|
$this->setPackageSupport($package, $config); |
|
90
|
|
|
|
|
91
|
|
|
return $package; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function setPackageStability(Interfaces\Package $package, array $config) |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->isValid($config, 'stability', 'string')) { |
|
97
|
|
|
$package->setStability($config['stability']); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function setPackageExtra(Interfaces\Package $package, array $config) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->isValid($config, 'extra', 'array')) { |
|
104
|
|
|
$package->setExtra($config['extra']); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function setPackageDescription(Interfaces\Package $package, array $config) |
|
109
|
|
|
{ |
|
110
|
|
|
if ($this->isValid($config, 'description', 'string')) { |
|
111
|
|
|
$package->setDescription($config['description']); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function setPackageHomepage(Interfaces\Package $package, array $config) |
|
116
|
|
|
{ |
|
117
|
|
|
if ($this->isValid($config, 'homepage', 'string')) { |
|
118
|
|
|
$package->setHomepage($config['homepage']); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function setPackageKeywords(Interfaces\Package $package, array $config) |
|
123
|
|
|
{ |
|
124
|
|
|
if ($this->isValid($config, 'keywords', 'array')) { |
|
125
|
|
|
$package->setKeywords($config['keywords']); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
protected function setPackageLicense(Interfaces\Package $package, array $config) |
|
130
|
|
|
{ |
|
131
|
|
|
if (!empty($config['license'])) { |
|
132
|
|
|
$package->setLicense(is_array($config['license']) ? $config['license'] : [$config['license']]); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
protected function setPackageAuthors(Interfaces\Package $package, array $config) |
|
137
|
|
|
{ |
|
138
|
|
|
if ($this->isValid($config, 'authors', 'array')) { |
|
139
|
|
|
$package->setAuthors($config['authors']); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected function setPackageSupport(Interfaces\Package $package, array $config) |
|
144
|
|
|
{ |
|
145
|
|
|
if (isset($config['support'])) { |
|
146
|
|
|
$package->setSupport($config['support']); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
protected function isValid($config, $key, $type = 'any') |
|
151
|
|
|
{ |
|
152
|
|
|
switch ($type) { |
|
153
|
|
|
case 'string': |
|
154
|
|
|
return isset($config[$key]) && !empty($config[$key]) && is_string($config[$key]); |
|
155
|
|
|
|
|
156
|
|
|
case 'array': |
|
157
|
|
|
return isset($config[$key]) && !empty($config[$key]) && is_array($config[$key]); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
protected function setPackageSource(Interfaces\Package $package, array $config) |
|
164
|
|
|
{ |
|
165
|
|
|
if (!isset($config['source'])) { |
|
166
|
|
|
return; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if (!isset($config['source']['type']) || !isset($config['source']['url']) || !isset($config['source']['reference'])) { |
|
170
|
|
|
throw new UnexpectedValueException(sprintf( |
|
171
|
|
|
"Package %s's source key should be specified as {\"type\": ..., \"url\": ..., \"reference\": ...},\n%s given.", |
|
172
|
|
|
$config['name'], |
|
173
|
|
|
json_encode($config['source']) |
|
174
|
|
|
)); |
|
175
|
|
|
} |
|
176
|
|
|
$package->setSourceType($config['source']['type']); |
|
|
|
|
|
|
177
|
|
|
$package->setSourceUrl($config['source']['url']); |
|
|
|
|
|
|
178
|
|
|
$package->setSourceReference($config['source']['reference']); |
|
179
|
|
|
if (isset($config['source']['mirrors'])) { |
|
180
|
|
|
$package->setSourceMirrors($config['source']['mirrors']); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
protected function setPackageDist(Interfaces\Package $package, array $config) |
|
185
|
|
|
{ |
|
186
|
|
|
if (!isset($config['dist'])) { |
|
187
|
|
|
return; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if (!isset($config['dist']['type']) |
|
191
|
|
|
|| !isset($config['dist']['url'])) { |
|
192
|
|
|
throw new UnexpectedValueException(sprintf( |
|
193
|
|
|
"Package %s's dist key should be specified as " |
|
194
|
|
|
. "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given.", |
|
195
|
|
|
$config['name'], |
|
196
|
|
|
json_encode($config['dist']) |
|
197
|
|
|
)); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$package->setDistType($config['dist']['type']); |
|
201
|
|
|
$package->setDistUrl($config['dist']['url']); |
|
202
|
|
|
$package->setDistReference($config['dist']['reference'] ?? null); |
|
203
|
|
|
$package->setDistSha1Checksum($config['dist']['shasum'] ?? null); |
|
|
|
|
|
|
204
|
|
|
if (isset($config['dist']['mirrors'])) { |
|
205
|
|
|
$package->setDistMirrors($config['dist']['mirrors']); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
protected function setPackageReleaseDate(Interfaces\Package $package, array $config) |
|
210
|
|
|
{ |
|
211
|
|
|
if (empty($config['time'])) { |
|
212
|
|
|
return; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$time = ctype_digit($config['time']) ? '@' . $config['time'] : $config['time']; |
|
216
|
|
|
|
|
217
|
|
|
try { |
|
218
|
|
|
$date = new DateTime($time, new DateTimeZone('UTC')); |
|
219
|
|
|
$package->setReleaseDate($date); |
|
|
|
|
|
|
220
|
|
|
} catch (Exception $e) { |
|
221
|
|
|
// don't crash if time is incorrect |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
|
227
|
|
|
|