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\PHP\Util\XML; |
38
|
|
|
|
39
|
|
|
use Composer\Package\Loader\LoaderInterface; |
40
|
|
|
use Exception; |
41
|
|
|
use InvalidArgumentException; |
42
|
|
|
use Pickle\Package\Util\Header; |
43
|
|
|
use RuntimeException; |
44
|
|
|
use SimpleXMLElement; |
45
|
|
|
|
46
|
|
|
class Loader |
47
|
|
|
{ |
48
|
|
|
protected $loader; |
49
|
|
|
|
50
|
|
|
public function __construct(LoaderInterface $loader) |
51
|
|
|
{ |
52
|
1 |
|
$this->loader = $loader; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $path |
57
|
|
|
* |
58
|
|
|
* @return \Pickle\Base\Interfaces\Package |
59
|
|
|
*/ |
60
|
|
|
public function load($path, $versionOverride = null) |
61
|
|
|
{ |
62
|
1 |
|
if (is_file($path) === false) { |
63
|
1 |
|
throw new InvalidArgumentException('File not found: ' . $path); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$xml = @simplexml_load_file($path); |
67
|
|
|
|
68
|
1 |
|
if ($xml === false) { |
69
|
1 |
|
$error = error_get_last(); |
70
|
1 |
|
$exception = null; |
71
|
|
|
|
72
|
1 |
|
if ($error !== null) { |
73
|
|
|
$exception = new Exception($error['message'], $error['type']); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
throw new RuntimeException('Failed to read ' . $path, 0, $exception); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$this->validate($xml); |
80
|
|
|
|
81
|
|
|
$package = [ |
82
|
1 |
|
'name' => (string) $xml->name, |
83
|
1 |
|
'version' => (string) $versionOverride === '' ? (string) $xml->version->release : $versionOverride, |
84
|
1 |
|
'stability' => (string) $xml->stability->release, |
85
|
1 |
|
'description' => (string) $xml->summary, |
86
|
|
|
]; |
87
|
|
|
|
88
|
1 |
|
if (!isset($xml->providesextension)) { |
89
|
|
|
throw new Exception('not a PHP extension package.xml, providesextension tag missing'); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$authors = []; |
93
|
1 |
|
foreach ([$xml->lead, $xml->developer, $xml->contributor, $xml->helper] as $devs) { |
94
|
1 |
|
foreach ($devs as $dev) { |
95
|
1 |
|
$authors[] = $dev; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
if (empty($authors) === false) { |
100
|
1 |
|
$package['authors'] = []; |
101
|
|
|
|
102
|
1 |
|
foreach ($authors as $author) { |
103
|
|
|
$tmp = [ |
104
|
1 |
|
'name' => trim((string) $author->name), |
105
|
1 |
|
'email' => trim((string) $author->email), |
106
|
|
|
]; |
107
|
1 |
|
if ($tmp['name'] === '') { |
108
|
|
|
unset($tmp['name']); |
109
|
|
|
} |
110
|
1 |
|
if ($tmp['email'] === '') { |
111
|
|
|
unset($tmp['email']); |
112
|
|
|
} |
113
|
1 |
|
if ($tmp !== []) { |
114
|
1 |
|
$package['authors'][] = $tmp; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$opts = $configureOptions = []; |
120
|
|
|
|
121
|
1 |
|
if (isset($xml->extsrcrelease->configureoption)) { |
122
|
1 |
|
$opts = $xml->extsrcrelease->configureoption; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
foreach ($opts as $opt) { |
126
|
1 |
|
$name = trim($opt['name']); |
127
|
1 |
|
$default = trim($opt['default']); |
128
|
1 |
|
$prompt = trim($opt['prompt']); |
129
|
|
|
|
130
|
1 |
|
$configureOptions[$name] = [ |
131
|
1 |
|
'default' => $default, |
132
|
1 |
|
'prompt' => $prompt, |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
if (empty($configureOptions) === false) { |
137
|
1 |
|
$package['extra'] = ['configure-options' => $configureOptions]; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
if (isset($xml->license)) { |
141
|
1 |
|
$package['license'] = (string) $xml->license; |
142
|
|
|
} |
143
|
1 |
|
$package['type'] = 'extension'; |
144
|
|
|
|
145
|
1 |
|
$ret_pkg = $this->loader->load($package); |
146
|
1 |
|
if (!$ret_pkg) { |
|
|
|
|
147
|
1 |
|
throw new Exception("Package from '{$path}' failed to load."); |
148
|
|
|
} |
149
|
|
|
$ret_pkg->setRootDir(dirname($path)); |
|
|
|
|
150
|
|
|
|
151
|
|
|
if ($versionOverride !== '') { |
152
|
|
|
$src_ver = $versionOverride ?? (string) new Header\Version($ret_pkg); |
153
|
|
|
if ($src_ver !== $ret_pkg->getPrettyVersion()) { |
154
|
|
|
throw new Exception("Version mismatch - '" . $src_ver . "' != '" . $ret_pkg->getPrettyVersion() . "' in source vs. XML"); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
$ret_pkg->setType('extension'); |
|
|
|
|
158
|
|
|
|
159
|
|
|
return $ret_pkg; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function validate(SimpleXMLElement $xml) |
163
|
|
|
{ |
164
|
1 |
|
if (version_compare($xml['version'], '2.0') === -1) { |
165
|
1 |
|
throw new RuntimeException('Unsupported package.xml version, 2.0 or later only is supported'); |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
if (!isset($xml->providesextension)) { |
169
|
1 |
|
throw new RuntimeException('Only extension packages are supported'); |
170
|
|
|
} |
171
|
1 |
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
175
|
|
|
|