-
Notifications
You must be signed in to change notification settings - Fork 209
/
Pecl.php
618 lines (564 loc) · 21.5 KB
/
Pecl.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<?php
namespace Valet;
use Exception;
use DomainException;
/**
* @deprecated
*/
class Pecl extends AbstractPecl
{
// Extensions.
const XDEBUG_EXTENSION = 'xdebug';
const APCU_EXTENSION = 'apcu';
const APCU_BC_EXTENSION = 'apcu_bc';
const GEOIP_EXTENSION = 'geoip';
const MEMCACHE_EXTENSION = 'memcached';
const YAML_EXTENSION = 'yaml';
// Extension aliases.
const APCU_BC_ALIAS = 'apc';
/**
* Supported pecl extensions for the PECL manager. There are 2 types of extensions supported: zend_extensions and
* extensions. Not all extension are available through PECL. This class handles packages that are handled by PECL.
*
* PECL example:
*
* The PECL extensions sometimes differ in version per PHP version. For example Xdebug can only be installed on
* PHP 5.6 using the 2.2.7 version. In such a case one would define the version within the array as key. The value
* would be 2.2.7 which is the version to be installed. See example below.
*
* The 'default' key allows one to disable the module by default. This is useful for modules that enable through
* on/off commands, like Xdebug.
*
* @formatter:off
*
* 'extension_key_name' => [
* '5.6' => '2.2.7'
* 'default' => false
* ]
*
* @formatter:on
*/
const EXTENSIONS = [
self::XDEBUG_EXTENSION => [
'8.3' => '3.3.0',
'8.2' => '3.1.5',
'8.1' => '3.1.5',
'8.0' => '3.1.5',
'7.4' => '3.1.5',
'7.3' => '3.1.5',
'7.2' => '2.9.8',
'7.1' => '2.9.8',
'7.0' => '2.9.8',
'5.6' => '2.2.7',
'default' => false,
'extension_type' => self::ZEND_EXTENSION_TYPE
],
self::APCU_EXTENSION => [
'7.3' => '5.1.17',
'7.2' => '5.1.17',
'7.1' => '5.1.17',
'7.0' => '5.1.17',
'5.6' => '4.0.11',
'extension_type' => self::NORMAL_EXTENSION_TYPE
],
self::GEOIP_EXTENSION => [
'8.3' => false, //todo; will probably be 1.1.2
'8.2' => false, //todo; will probably be 1.1.2
'8.1' => false, //todo; will probably be 1.1.2
'8.0' => false, //todo; will probably be 1.1.2
'7.4' => '1.1.1',
'7.3' => '1.1.1',
'7.2' => '1.1.1',
'7.1' => '1.1.1',
'7.0' => '1.1.1',
'extension_type' => self::NORMAL_EXTENSION_TYPE,
'brew_dependency' => 'geoip'
],
self::MEMCACHE_EXTENSION => [
'7.3' => '3.1.3',
'7.2' => '3.1.3',
'7.1' => '3.1.3',
'7.0' => '3.1.3',
'default' => false,
'extension_type' => self::NORMAL_EXTENSION_TYPE
],
self::YAML_EXTENSION => [
'5.6' => '1.3.1',
'7.0' => '2.0.4',
'extension_type' => self::NORMAL_EXTENSION_TYPE,
'brew_dependency' => 'libyaml'
]
];
/**
* @var PeclCustom
*/
public $peclCustom;
/**
* @var Architecture
*/
private $architecture;
/**
* @inheritdoc
*/
public function __construct(Architecture $architecture, CommandLine $cli, Filesystem $files, PeclCustom $peclCustom)
{
parent::__construct($cli, $files);
$this->peclCustom = $peclCustom;
$this->architecture = $architecture;
}
/**
* @inheritdoc
*/
public function installExtensions($onlyDefaults = true)
{
info("[PECL] Installing extensions");
foreach (self::EXTENSIONS as $extension => $versions) {
if ($onlyDefaults && $this->isDefaultExtension($extension) === false) {
continue;
}
if ($this->getVersion($extension) !== false) {
$this->installExtension($extension);
$this->enableExtension($extension);
}
}
}
/**
* Install a single extension if not already installed, default is true and has a version for this PHP version.
* If version equals false the extension will not be installed for this PHP version.
*
* @param $extension
* The extension key name.
* @return bool
*/
public function installExtension($extension)
{
if ($this->isInstalled($extension)) {
output("\t$extension is already installed, skipping...");
return false;
}
$this->install($extension, $this->getVersion($extension));
return true;
}
/**
* Install a single extension.
*
* @param $extension
* The extension key name.
* @param null $version
*/
protected function install($extension, $version = null)
{
if ($version === null) {
if ($this->hasBrewDependency($extension)) {
$result = $this->cli->runAsUser("echo $(brew --prefix " . $this->getBrewDependency($extension) . ") | pecl install $extension");
} else {
$result = $this->cli->runAsUser("pecl install $extension");
}
} else {
$result = $this->cli->runAsUser("pecl install $extension-$version");
}
$alias = $this->getExtensionAlias($extension);
if (!preg_match("/Installing '(.*$alias.so)'/", $result)) {
throw new DomainException("Could not find installation path for: $extension\n\n$result");
}
if (strpos($result, "Error:")) {
throw new DomainException("Installation path found, but installation failed:\n\n$result");
}
$phpIniPath = $this->getPhpIniPath();
$phpIniFile = $this->files->get($phpIniPath);
if (!preg_match('/(zend_extension|extension)\="(.*' . $alias . '.so)"/', $phpIniFile, $iniMatches)) {
throw new DomainException("Could not find ini definition for: $extension in $phpIniPath");
}
output("\t$extension successfully installed");
}
/**
* Enable an single extension if not already enabled, default is true and has a version does not equal false.
*
* @param $extension
* The extension key name.
* @return bool
*/
public function enableExtension($extension)
{
if ($this->isEnabled($extension) && $this->isEnabledCorrectly($extension)) {
output("\t$extension is already enabled, skipping...");
return false;
}
$this->enable($extension);
return true;
}
/**
* Enable an single extension.
*
* @param $extension
* The extension key name.
*/
public function enable($extension)
{
$phpIniPath = $this->getPhpIniPath();
$phpIniFile = $this->files->get($phpIniPath);
$phpIniFile = $this->replaceIniDefinition($extension, $phpIniFile);
$phpIniFile = $this->alternativeInstall($extension, $phpIniFile);
$this->peclCustom->saveIniFile($phpIniPath, $phpIniFile);
output("\t$extension successfully enabled");
}
/**
* @inheritdoc
*/
public function uninstallExtensions()
{
info("[PECL] Removing extensions");
foreach (self::EXTENSIONS as $extension => $versions) {
if ($this->getVersion($extension) !== false) {
$this->disableExtension($extension);
}
}
}
/**
* Uninstall and disable an extension if installed.
*
* @param $extension
* The extension key name.
* @return bool
* Whether or not an uninstall happened.
*/
public function disableExtension($extension)
{
$version = $this->getVersion($extension);
if ($this->isEnabled($extension)) {
$this->disable($extension);
}
if ($this->isInstalled($extension)) {
$this->uninstall($extension, $version);
return true;
}
return false;
}
public function disable($extension)
{
$this->removeIniDefinition($extension);
$this->alternativeDisable($extension);
}
/**
* Replace and remove all directives of the .so file for the given extension within the php.ini file.
*
* @param $extension
* The extension key name.
*/
private function removeIniDefinition($extension)
{
$phpIniPath = $this->getPhpIniPath();
$alias = $this->getExtensionAlias($extension);
$phpIniFile = $this->files->get($phpIniPath);
$phpIniFile = preg_replace('/;?(zend_extension|extension)\=".*' . $alias . '.so"/', '', $phpIniFile);
$this->peclCustom->saveIniFile($phpIniPath, $phpIniFile);
output("\t$extension successfully disabled");
}
/**
* Because some extensions install others, like apcu_bc, the default disable method will not be sufficient.
* In that case use this method to for example add additional dependencies for the specific disable. For example
* apcu_bc installs apcu and apc both will need to be removed from the php.ini file. The default would only remove
* apcu.so so we define apc.so here as alternative.
*
* @param $extension
* @return string
*/
private function alternativeDisable($extension)
{
switch ($extension) {
default:
break;
}
}
/**
* Because some extensions install others, like apcu_bc, the default uninstall method will not be sufficient.
* In that case use this method to for example add additional dependencies for the specific uninstall. For example
* apcu_bc installs apcu and apc both will need to be uninstalled from pecl. The default would only uninstall
* apcu.so so we define apc.so here as alternative.
*
* @param $extension
* @return string
*/
private function alternativeUninstall($extension)
{
switch ($extension) {
default:
break;
}
}
/**
* Uninstall a single extension.
*
* @param $extension
* The extension key name.
* @param null $version
*/
private function uninstall($extension, $version = null)
{
if ($version === null || $version === false) {
$this->cli->passthru("pecl uninstall $extension");
} else {
$this->cli->passthru("pecl uninstall $extension-$version");
}
$this->alternativeUninstall($extension);
}
/**
* Update the default PECL channel.
*/
public function updatePeclChannel()
{
info('[PECL] Updating PECL channel: pecl.php.net');
$this->cli->runAsUser('pecl channel-update pecl.php.net');
}
/**
* Fix common problems related to the PECL/PEAR installation.
*/
public function fix()
{
info('[PECL] Checking pear config...');
// Check if pear config is set correctly as per:
// https://github.com/kabel/homebrew-core/blob/2564749d8f73e43cbb8cfc449bca4f564ac0e9e1/Formula/php%405.6.rb
// Brew installation standard.
foreach (PhpFpm::SUPPORTED_PHP_FORMULAE as $phpVersion => $brewname) {
output("Checking php $phpVersion...");
$pearConfigPath = $this->architecture->getBrewPath() . PhpFpm::LOCAL_PHP_FOLDER . "$phpVersion/pear.conf";
if (!$this->files->exists($pearConfigPath)) {
warning(" Skipping $phpVersion, Pear config path could not be found at: $pearConfigPath");
continue;
}
$pearConfig = $this->files->get($pearConfigPath);
$pearConfigSplit = explode("\n", $pearConfig);
$pearConfigVersion = '';
$pearConfig = false;
foreach ($pearConfigSplit as $splitValue) {
if (strpos($splitValue, 'php_ini')) {
$pearConfig = unserialize($splitValue);
} else if (strpos($splitValue, 'Config')) {
$pearConfigVersion = $splitValue;
} else {
continue;
}
}
if ($pearConfig === false) {
warning("Could not determine pear configuration for PhP version: $phpVersion, skipping...");
continue;
}
$pearName = $this->replacePhpWithPear($brewname);
$phpIniPath = str_replace('pear.conf', 'php.ini', $pearConfigPath);
$phpDirPath = $this->architecture->getBrewPath() . "/share/$pearName";
$pearDocDirPath = $this->architecture->getBrewPath() . "/share/$pearName/doc";
$phpExtensionDirPath = $this->architecture->getBrewPath() . '/lib/php/pecl/'.basename($pearConfig['ext_dir']);
$phpBinPath = $this->architecture->getBrewPath() . "/opt/$brewname/bin";
$pearDataDirPath = $this->architecture->getBrewPath() . "/share/$pearName/data";
$pearCfgDirPath = $this->architecture->getBrewPath() . "/share/$pearName/cfg";
$pearWwwDirPath = $this->architecture->getBrewPath() . "/share/$pearName/htdocs";
$pearManDirPath = $this->architecture->getBrewPath() . '/share/man';
$pearTestDirPath = $this->architecture->getBrewPath() . "/share/$pearName/test";
$phpBinDirPath = $this->architecture->getBrewPath() . "/opt/$brewname/bin/php";
// Check php_ini value of par config.
if (empty($pearConfig['php_ini']) || $pearConfig['php_ini'] !== $phpIniPath) {
output(" Setting pear config php_ini directive to: $phpIniPath");
$pearConfig['php_ini'] = $phpIniPath;
}
// Check php_dir value of par config.
if (empty($pearConfig['php_dir']) || $pearConfig['php_dir'] !== $phpDirPath) {
output(" Setting pear config php_dir directive to: $phpDirPath");
$pearConfig['php_dir'] = $phpDirPath;
}
// Check doc_dir value of par config.
if (empty($pearConfig['doc_dir']) || $pearConfig['doc_dir'] !== $pearDocDirPath) {
output(" Setting pear config doc_dir directive to: $pearDocDirPath");
$pearConfig['doc_dir'] = $pearDocDirPath;
}
// Check ext_dir value of par config.
if (empty($pearConfig['ext_dir']) || $pearConfig['ext_dir'] !== $phpExtensionDirPath) {
output(" Setting pear config ext_dir directive to: $phpExtensionDirPath");
$pearConfig['ext_dir'] = $phpExtensionDirPath;
}
// Check php_bin value of par config.
if (empty($pearConfig['bin_dir']) || $pearConfig['bin_dir'] !== $phpBinPath) {
output(" Setting pear config bin_dir directive to: $phpBinPath");
$pearConfig['bin_dir'] = $phpBinPath;
}
// Check data_dir value of par config.
if (empty($pearConfig['data_dir']) || $pearConfig['data_dir'] !== $pearDataDirPath) {
output(" Setting pear config data_dir directive to: $pearDataDirPath");
$pearConfig['data_dir'] = $pearDataDirPath;
}
// Check cfg_dir value of par config.
if (empty($pearConfig['cfg_dir']) || $pearConfig['cfg_dir'] !== $pearCfgDirPath) {
output(" Setting pear config cfg_dir directive to: $pearCfgDirPath");
$pearConfig['cfg_dir'] = $pearCfgDirPath;
}
// Check www_dir value of par config.
if (empty($pearConfig['www_dir']) || $pearConfig['www_dir'] !== $pearWwwDirPath) {
output(" Setting pear config www_dir directive to: $pearWwwDirPath");
$pearConfig['www_dir'] = $pearWwwDirPath;
}
// Check man_dir value of par config.
if (empty($pearConfig['man_dir']) || $pearConfig['man_dir'] !== $pearManDirPath) {
output(" Setting pear config man_dir directive to: $pearManDirPath");
$pearConfig['man_dir'] = $pearManDirPath;
}
// Check test_dir value of par config.
if (empty($pearConfig['test_dir']) || $pearConfig['test_dir'] !== $pearTestDirPath) {
output(" Setting pear config test_dir directive to: $pearTestDirPath");
$pearConfig['test_dir'] = $pearTestDirPath;
}
// Check php_bin value of par config.
if (empty($pearConfig['php_bin']) || $pearConfig['php_bin'] !== $phpBinDirPath) {
output(" Setting pear config php_bin directive to: $phpBinDirPath");
$pearConfig['php_bin'] = $phpBinDirPath;
}
// Rebuild the config.
$pearConfig = serialize($pearConfig);
if (!empty($pearConfigVersion)) {
$pearConfig = $pearConfigVersion."\n".$pearConfig;
}
// Put config back into the pear.conf file.
$this->files->put($pearConfigPath, $pearConfig);
}
}
/**
* @inheritdoc
*/
public function isInstalled($extension)
{
return strpos($this->cli->runAsUser('pecl list | grep ' . $extension), $extension) !== false;
}
/**
* Because some extensions install others, like apcu_bc, the default install method will not be sufficient.
* In that case use this method to for example add additional dependencies for the specific install. For example
* apcu_bc installs apcu and apc both will need to be linked within the php.ini file. The default would only link
* apcu.so so we define apc.so here as alternative.
*
* @param $extension
* The extension key name.
* @param $phpIniFile
* @return string
*/
private function alternativeInstall($extension, $phpIniFile)
{
switch ($extension) {
default:
return $phpIniFile;
}
}
/**
* Replace the .so directive within the php.ini file. Initial .so directive will be set by
* 'pecl install {extension}'. Replacing will prevent duplicate entries.
*
* @param $extension
* The extension key name.
* @param $phpIniFile
*
* @return string
*/
private function replaceIniDefinition($extension, $phpIniFile)
{
$alias = $this->getExtensionAlias($extension);
$type = $this->getExtensionType($extension);
$phpIniFile = preg_replace("/(zend_extension|extension)\=\"(.*$alias.so)\"/", '', $phpIniFile);
return "$type=\"$alias.so\"\n" . $phpIniFile;
}
/**
* Whether or not this extension should be enabled by default. Is set by setting the
* 'default' key to true within the extensions configuration.
*
* @param $extension
* The extension key name.
* @return bool
*/
private function isDefaultExtension($extension)
{
if (array_key_exists('default', self::EXTENSIONS[$extension])) {
return false;
} elseif (array_key_exists('default', self::EXTENSIONS[$extension]) === false) {
return true;
} else {
return false;
}
}
/**
* @inheritdoc
*/
protected function getExtensionAlias($extension)
{
switch ($extension) {
default:
return $extension;
}
}
/**
* Get the version of the extension supported by the current PHP version.
*
* @param $extension
* The extension key name.
* @return null
*/
private function getVersion($extension)
{
$phpVersion = $this->getPhpVersion();
if (array_key_exists($phpVersion, self::EXTENSIONS[$extension])) {
return self::EXTENSIONS[$extension][$phpVersion];
}
return null;
}
/**
* Check if the extension has any brew dependency
*
* @param mixed $extension
* @return bool
*/
private function hasBrewDependency($extension)
{
return array_key_exists("brew_dependency", self::EXTENSIONS[$extension]);
}
/**
* Get the brew dependency for the pecl command
*
* @param mixed $extension
* @return mixed
*/
private function getBrewDependency($extension)
{
return self::EXTENSIONS[$extension]["brew_dependency"];
}
/**
* Pecl sometimes adds a directive twice. This causes the PHP installation to see the extension as
* installed. However the seconds directive is seen as faulty which causes the PHP installation to
* fail. This van be solved by checking if the directive exists twice. If it does valet-plus will
* deem the module as "disabled" and replace the existing directives with a single one.
*
* @param $extension
* @return bool
*/
private function isEnabledCorrectly($extension)
{
$phpIniPath = $this->getPhpIniPath();
$phpIniFile = $this->files->get($phpIniPath);
$type = $this->getExtensionType($extension);
$alias = $this->getExtensionAlias($extension);
preg_match_all("/$type=\"$alias.so\"/m", $phpIniFile, $matches);
$alternativeEnabledCorrectly = $this->isAlternativeEnabledCorrectly($extension);
$isEnabledCorrectly = $alternativeEnabledCorrectly && (is_array($matches) && count($matches) === 1?count($matches[0]) === 1:false);
return $isEnabledCorrectly;
}
/**
* Alternative case for checking directives of modules that depend on other modules.
*
* @param $extension
* @return bool
*/
private function isAlternativeEnabledCorrectly($extension)
{
switch ($extension) {
default:
return true;
}
}
private function replacePhpWithPear($brewname)
{
return str_replace('php', 'pear', $brewname);
}
}