-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompat.php
148 lines (146 loc) · 4.65 KB
/
Compat.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
<?php
/**
* PHP Compat
*
* Provides missing functionality in the form of constants and functions
* for older versions of PHP
*
* Optionally, you may simply include the file.
* e.g. require_once 'PHP/Compat/Function/scandir.php';
*
* PHP version 4
*
* @category PHP
* @package PHP_Compat
* @author Aidan Lister <[email protected]>
* @copyright 2004-2007 Aidan Lister <[email protected]>, Arpad Ray <[email protected]>
* @license LGPL - http://www.gnu.org/licenses/lgpl.html
* @version SVN: $Id$
* @link http://pear.php.net/package/PHP_Compat
*/
/**
* Provides components to achieve PHP version independence
*
* @category PHP
* @package PHP_Compat
* @author Aidan Lister <[email protected]>
* @copyright 2004-2007 Aidan Lister <[email protected]>, Arpad Ray <[email protected]>
* @license LGPL - http://www.gnu.org/licenses/lgpl.html
* @version Release: 1.7.2
* @link http://pear.php.net/package/PHP_Compat
*/
class PHP_Compat
{
/**
* Load a function, or array of functions
*
* @param string|array $function The function or functions to load
*
* @return bool|array TRUE if loaded, FALSE if not
*/
function loadFunction($function)
{
// Multiple
if (is_array($function)) {
$res = array();
foreach ($function as $singlefunc) {
$res[$singlefunc] = PHP_Compat::loadFunction($singlefunc);
}
return $res;
}
// Check for packages which can modify the function table at runtime
$symbolfuncs = array('rename_function', 'runkit_rename_function');
foreach ($symbolfuncs as $symbolfunc) {
$renamedfunction = 'php_compat_renamed' . $function;
if (function_exists($symbolfunc) && function_exists($function)
&& !function_exists($renamedfunction)
) {
// Rename the core function
rename_function($function, $renamedfunction);
break;
}
}
// Single
if (!function_exists($function)) {
$file = sprintf('PHP/Compat/Function/%s.php', $function);
if ((@include_once $file) !== false) {
return true;
}
}
return false;
}
/**
* Load a constant, or array of constants
*
* @param string|array $constant The constant or constants to load
*
* @return bool|array TRUE if loaded, FALSE if not
*/
function loadConstant($constant)
{
// Multiple
if (is_array($constant)) {
$res = array();
foreach ($constant as $singleconst) {
$res[$singleconst] = PHP_Compat::loadConstant($singleconst);
}
return $res;
}
// Single
$file = sprintf('PHP/Compat/Constant/%s.php', $constant);
if ((@include_once $file) !== false) {
return true;
}
return false;
}
/**
* Load an environment
*
* @param string $environment The environment to load
* @param string $setting Turn the environment on or off
*
* @return bool TRUE if loaded, FALSE if not
*/
function loadEnvironment($environment, $setting)
{
// Load environment
$file = sprintf('PHP/Compat/Environment/%s_%s.php', $environment, $setting);
if ((@include_once $file) !== false) {
return true;
}
return false;
}
/**
* Load components for a PHP version
*
* @param string $version PHP Version to load
*
* @return array An associative array of component names loaded
*/
function loadVersion($version = null)
{
// Include list of components
include 'PHP/Compat/Components.php';
// Include version_compare to work with older versions
PHP_Compat::loadFunction('version_compare');
// Init
$phpver = phpversion();
$methods = array('function' => 'loadFunction', 'constant' => 'loadConstant');
$res = array();
// Iterate each component
foreach ($components as $type => $slice) {
foreach ($slice as $component => $cver) {
$vercom = version_compare($cver, $phpver);
if (($version === null && $vercom === 1) // C > PHP
|| (version_compare($cver, $version) === 0 // C = S
|| $vercom === 1)
) { // C > PHP
$res[$type][$component] = call_user_func(
array('PHP_Compat', $methods[$type]), $component
);
}
}
}
return $res;
}
}