1
|
|
|
<?php namespace nyx\utils\platform; |
2
|
|
|
|
3
|
|
|
// Internal dependencies |
4
|
|
|
use nyx\utils; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* PHP |
8
|
|
|
* |
9
|
|
|
* Utils for introspecting information about PHP itself. |
10
|
|
|
* |
11
|
|
|
* @package Nyx\Utils\Platform |
12
|
|
|
* @version 0.0.1 |
13
|
|
|
* @author Michal Chojnacki <[email protected]> |
14
|
|
|
* @copyright 2012-2016 Nyx Dev Team |
15
|
|
|
* @link http://docs.muyo.io/nyx/utils/platform.html |
16
|
|
|
* @todo Bitwise checks for combined phpinfo() sections when parsing information. |
17
|
|
|
*/ |
18
|
|
|
class Php |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The traits of the Php class. |
22
|
|
|
*/ |
23
|
|
|
use utils\traits\StaticallyExtendable; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool Whether PHP has been compiled with the "--enable-sigchild" flag. |
27
|
|
|
*/ |
28
|
|
|
private static $flags = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array An array containing the output of phpinfo() calls, grouped together by the INFO_ constants |
32
|
|
|
* used to retrieve them. |
33
|
|
|
*/ |
34
|
|
|
private static $info = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Checks whether PHP has been compiled with the given flag. |
38
|
|
|
* |
39
|
|
|
* @param string $flag The name of the flag to check. The two initial hyphens can be omitted. |
40
|
|
|
* @return bool True when PHP has been compiled with the given flag, false otherwise. |
41
|
|
|
*/ |
42
|
|
|
public static function hasFlag(string $flag) : bool |
43
|
|
|
{ |
44
|
|
|
// Standardize the flag name - remove starting hyphens. |
45
|
|
|
$flag = ltrim($flag, '-'); |
46
|
|
|
|
47
|
|
|
// Return the check right away if it's already cached. |
48
|
|
|
if (isset(static::$flags[$flag])) { |
|
|
|
|
49
|
|
|
return static::$flags[$flag]; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Grab the output of phpinfo(). If INFO_ALL is already available, we will just parse it instead of |
53
|
|
|
// fetching INFO_GENERAL specifically for our case. Then we are simply going to check if the --string |
54
|
|
|
// appears in the info. |
55
|
|
|
$result = strpos(static::$info[INFO_ALL] ?? static::getInfo(INFO_GENERAL), '--'.$flag); |
|
|
|
|
56
|
|
|
|
57
|
|
|
// Cache the result and return it. |
58
|
|
|
return static::$flags[$flag] = false !== $result; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Fetches, caches and returns the output of phpinfo(). |
63
|
|
|
* |
64
|
|
|
* @param int $for One of the INFO_* constants {@see http://php.net/manual/en/function.phpinfo.php}. |
65
|
|
|
* @return string The raw output of phpinfo. |
66
|
|
|
*/ |
67
|
|
|
public static function getInfo(int $for = INFO_ALL) : string |
68
|
|
|
{ |
69
|
|
|
// Return the data right away if we've already cached the given section. |
70
|
|
|
if (isset(static::$info[$for])) { |
|
|
|
|
71
|
|
|
return static::$info[$for]; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
ob_start(); |
75
|
|
|
phpinfo($for); |
76
|
|
|
|
77
|
|
|
return static::$info[$for] = ob_get_clean(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: