-
Notifications
You must be signed in to change notification settings - Fork 363
/
ExtensionFactory.php
337 lines (284 loc) · 11.6 KB
/
ExtensionFactory.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace PhpBrew\Extension;
use PhpBrew\Config;
use PhpBrew\Extension\PeclExtension;
use PhpBrew\Extension\M4Extension;
use PhpBrew\Extension\ConfigureOption;
use PEARX\PackageXml\Parser as PackageXmlParser;
use Exception;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
/**
* This factory class handles the extension information
*/
class ExtensionFactory
{
/**
* One extension directory might contains multiple config*.m4 file, like
* memcache extension.
*/
static public function configM4Exists($extensionDir)
{
$files = array();
$configM4Path = $extensionDir . DIRECTORY_SEPARATOR . 'config.m4';
if (file_exists($configM4Path)) {
$files[] = $configM4Path;
}
for ($i = 0 ; $i < 10 ; $i++ ) {
$configM4Path = $extensionDir . DIRECTORY_SEPARATOR . "config{$i}.m4";
if (file_exists($configM4Path)) {
$files[] = $configM4Path;
}
}
return $files;
}
static public function createFromDirectory($packageName, $extensionDir) {
$packageXmlPath = $extensionDir . DIRECTORY_SEPARATOR . 'package.xml';
// If the package.xml exists, we may get the configureoptions for configuring the Makefile
// and use the provided extension name to enable the extension.
//
// Currently only PECL extensions have package.xml, however It's the
// best strategy to install the extension.
if (file_exists($packageXmlPath)) {
// $this->logger->warning("===> Using xml extension meta");
if ($ext = self::createPeclExtension($packageName, $packageXmlPath)) {
return $ext;
}
}
// If the config.m4 or config0.m4 exists, it requires us to run `phpize` to
// initialize the `configure` script.
//
// It's basically a fallback for extensions that don't have package.xml.
// Generlly, The possible extensions using this strategy are usually
// PHP's core extensions, which are shipped in the distribution file.
// quote:
// the 0 there makes sure it gets into another stage of the buildprocess, the
// top IIRC, it was added @ 12th May 2001, 12:09am (10 months ago).
//
// http://grokbase.com/t/php/php-dev/023cpdc9k6/quick-summary-of-changes
//
// When config[0-9].m4 found, it might be an extension that can't be
// installed as a shared extension. We will need to raise a warning
// message for users.
$configM4Paths = self::configM4Exists($extensionDir);
foreach($configM4Paths as $m4path) {
if (file_exists($m4path)) {
try {
$ext = self::createM4Extension($packageName, $m4path);
if ($ext) {
return $ext;
}
} catch(Exception $e) {
// Can't parse the content, ignore the error and continue the parsing...
}
}
}
}
static public function lookupRecursive($packageName, array $lookupDirs = array(), $fallback = true) {
if ($fallback) {
// Always push the PHP source directory to the end of the list for the fallback.
$lookupDirs[] = Config::getBuildDir() . DIRECTORY_SEPARATOR . Config::getCurrentPhpName() . DIRECTORY_SEPARATOR . 'ext' . DIRECTORY_SEPARATOR . $packageName;
}
foreach($lookupDirs as $lookupDir) {
if (!file_exists($lookupDir)) {
continue;
}
if ($ext = self::createFromDirectory($packageName, $lookupDir)) {
return $ext;
}
/**
* FOLLOW_SYMLINKS is available from 5.2.11, 5.3.1
*/
$di = new RecursiveDirectoryIterator($lookupDir, RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
/**
* Search for config.m4 or config0.m4 and use them to determine
* the directory of the extension's source, because it's not always
* the root directory in the ext archive (example xhprof)
*/
foreach ($it as $fileinfo) {
if (!$fileinfo->isDir()) {
continue;
}
if ($ext = self::createFromDirectory($packageName, $fileinfo->getPathName())) {
return $ext;
}
}
}
}
static public function lookup($packageName, array $lookupDirectories = array(), $fallback = true)
{
if ($fallback) {
// Always push the PHP source directory to the end of the list for the fallback.
$lookupDirectories[] = Config::getBuildDir() . '/' . Config::getCurrentPhpName() . '/ext';
}
foreach($lookupDirectories as $lookupDir) {
if (!file_exists($lookupDir)) {
continue;
}
$extensionDir = $lookupDir . DIRECTORY_SEPARATOR . $packageName;
if ($ext = self::createFromDirectory($packageName, $extensionDir)) {
return $ext;
}
}
}
static public function createM4Extension($packageName, $m4Path) {
if (!file_exists($m4Path)) {
return NULL;
}
$m4 = file_get_contents($m4Path);
// PHP_NEW_EXTENSION(extname, sources [, shared [, sapi_class [, extra-cflags [, cxx [, zend_ext]]]]])
if (preg_match('/PHP_NEW_EXTENSION \( \s*
\[?
(\w+) # The extension name
\]?
\s*,\s*
\[?
([^,]*) # Source files
\]?
(?:
\s*,\s* ([^,\)]*) # Ext Shared
(?:
\s*,\s* ([^,\)]*) # SAPI class
(?:
\s*,\s* ([^,\)]*) # Extra cflags
(?:
\s*,\s* ([^,\)]*) # CXX
\s*,\s* ([^,\)]*) # zend extension
)?
)?
)?
)?
/x', $m4, $matches)) {
$fullmatched = array_shift($matches);
$ext = new M4Extension($packageName);
$ext->setExtensionName($matches[0]);
$ext->setSharedLibraryName($matches[0] . '.so');
if (isset($matches[6]) && strpos($matches[6], 'yes') !== false) {
$ext->setZend(true);
}
$ext->setSourceDirectory(dirname($m4Path));
/*
PHP_ARG_ENABLE(calendar,whether to enable calendar conversion support,
[ --enable-calendar Enable support for calendar conversion])
*/
if (preg_match_all('/
PHP_ARG_ENABLE\(
\s*([^,]*)
(?:
\s*,\s*
(
[^,\)]*
)
(?:
\s*,\s*
\[
\s*
([^\s]+)
\s+
([^,\)]*)
\s*
\]
)?
)?/x', $m4, $allMatches))
{
for( $i = 0; $i < count($allMatches[0]) ; $i++ ) {
$name = $allMatches[1][$i];
$desc = $allMatches[2][$i];
$option = $allMatches[3][$i];
$optionDesc = $allMatches[4][$i];
$ext->addConfigureOption(new ConfigureOption($option ?: '--enable-' . $name, $desc ?: $optionDesc));
}
}
/*
PHP_ARG_WITH(gd, for GD support,
[ --with-gd[=DIR] Include GD support. DIR is the GD library base
install directory [BUNDLED]])
Possible option formats:
--with-libxml-dir=DIR
--with-recode[=DIR]
--with-yaml[[=DIR]]
--with-mysql-sock[=SOCKPATH]
*/
if (preg_match_all('/
PHP_ARG_WITH\(
\s*
([^,]*)
(?:
\s*,\s*
\[?
([^,\)]*)
\]?
(?:
\s*,\s*
\[
\s*
# simple match (\S+)
([a-zA-Z0-9-]+) # option
(?:
=?
\[?
=?([^\s\]]*?)
\]?
)? # option value hint
\s+
([^,\)]*) # option description
\s*
\]
(?:
\s*,\s*
([^,\)]*)
(?:
\s*,\s*
([^,\)]*)
)?
)?
)?
)?/x', $m4, $allMatches))
{
// Parsing the M4 statement:
//
// dnl PHP_ARG_WITH(arg-name, check message, help text[, default-val[, extension-or-not]])
//
for( $i = 0; $i < count($allMatches[0]) ; $i++ ) {
$name = $allMatches[1][$i];
$desc = $allMatches[2][$i];
$option = $allMatches[3][$i];
$optionValueHint = $allMatches[4][$i];
$optionDesc = $allMatches[5][$i];
$defaultValue = $allMatches[6][$i];
$opt = new ConfigureOption(( $option ?: '--with-' . $name), ($desc ?: $optionDesc), $optionValueHint);
if ($defaultValue) {
$opt->setDefaultValue($opt);
}
$ext->addConfigureOption($opt);
}
}
return $ext;
} else {
throw new Exception("Can not parse config.m4: $m4Path");
}
}
static public function createPeclExtension($packageName, $packageXmlPath) {
$parser = new PackageXmlParser;
$package = $parser->parse($packageXmlPath);
$ext = new PeclExtension($packageName);
$ext->setPackage($package);
/**
* xhprof stores package.xml in the root directory, but putting the
* config.m4 in the extension directory.
* the path can be retrieve from the contents part from the package.xml
*/
if ($m4path = $ext->findConfigM4FileFromPackageXml()) {
$sourceDirectory = dirname($packageXmlPath);
$m4dir = dirname($m4path);
if ($m4dir != '.') {
$sourceDirectory .= DIRECTORY_SEPARATOR . $m4dir;
}
$ext->setSourceDirectory($sourceDirectory);
} else {
$ext->setSourceDirectory(dirname($packageXmlPath));
}
return $ext;
}
}