Rev 171 Rev 172
1 <?php 1 <?php
2 // +----------------------------------------------------------------------+ 2 // +----------------------------------------------------------------------+
3 // | PHP Version 4 | 3 // | PHP Version 4 |
4 // +----------------------------------------------------------------------+ 4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997-2004 The PHP Group | 5 // | Copyright (c) 1997-2004 The PHP Group |
6 // +----------------------------------------------------------------------+ 6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 3.0 of the PHP license, | 7 // | This source file is subject to version 3.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is | 8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at | 9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/3_0.txt. | 10 // | http://www.php.net/license/3_0.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to | 11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to | 12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. | 13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+ 14 // +----------------------------------------------------------------------+
15 // | Authors: Aidan Lister <aidan@php.net> | 15 // | Authors: Aidan Lister <aidan@php.net> |
16 // +----------------------------------------------------------------------+ 16 // +----------------------------------------------------------------------+
17 // 17 //
18 // $Id: Compat.php,v 1.19 2005/05/10 12:05:36 aidan Exp $ 18 // $Id: Compat.php,v 1.19 2005/05/10 12:05:36 aidan Exp $
19   19  
20   20  
21 /** 21 /**
22 * Provides missing functionality in the form of constants and functions 22 * Provides missing functionality in the form of constants and functions
23 * for older versions of PHP 23 * for older versions of PHP
24 * 24 *
25 * Optionally, you may simply include the file. 25 * Optionally, you may simply include the file.
26 * e.g. require_once 'PHP/Compat/Function/scandir.php'; 26 * e.g. require_once 'PHP/Compat/Function/scandir.php';
27 * 27 *
28 * @category PHP 28 * @category PHP
29 * @package PHP_Compat 29 * @package PHP_Compat
30 * @version $Revision: 1.19 $ 30 * @version $Revision: 1.19 $
31 * @author Aidan Lister <aidan@php.net> 31 * @author Aidan Lister <aidan@php.net>
32 * @static 32 * @static
33 */ 33 */
34 class PHP_Compat 34 class PHP_Compat
35 { 35 {
36 /** 36 /**
37 * Load a function, or array of functions 37 * Load a function, or array of functions
38 * 38 *
39 * @param string|array $function The function or functions to load 39 * @param string|array $function The function or functions to load
40 * @return bool|array TRUE if loaded, FALSE if not 40 * @return bool|array TRUE if loaded, FALSE if not
41 */ 41 */
42 function loadFunction($function) 42 function loadFunction($function)
43 { 43 {
44 // Recursiveness 44 // Recursiveness
45 if (is_array($function)) { 45 if (is_array($function)) {
46 $res = array(); 46 $res = array();
47 foreach ($function as $singlefunc) { 47 foreach ($function as $singlefunc) {
48 $res[$singlefunc] = PHP_Compat::loadFunction($singlefunc); 48 $res[$singlefunc] = PHP_Compat::loadFunction($singlefunc);
49 } 49 }
50   50  
51 return $res; 51 return $res;
52 } 52 }
53   53  
54 // Load function 54 // Load function
55 if (!function_exists($function)) { 55 if (!function_exists($function)) {
56 $file = sprintf('PHP/Compat/Function/%s.php', $function); 56 $file = sprintf('PHP/Compat/Function/%s.php', $function);
57 if ((@include_once $file) !== false) { 57 if ((@include_once $file) !== false) {
58 return true; 58 return true;
59 } 59 }
60 } 60 }
61   61  
62 return false; 62 return false;
63 } 63 }
64   64  
65   65  
66 /** 66 /**
67 * Load a constant, or array of constants 67 * Load a constant, or array of constants
68 * 68 *
69 * @param string|array $constant The constant or constants to load 69 * @param string|array $constant The constant or constants to load
70 * @return bool|array TRUE if loaded, FALSE if not 70 * @return bool|array TRUE if loaded, FALSE if not
71 */ 71 */
72 function loadConstant($constant) 72 function loadConstant($constant)
73 { 73 {
74 // Recursiveness 74 // Recursiveness
75 if (is_array($constant)) { 75 if (is_array($constant)) {
76 $res = array(); 76 $res = array();
77 foreach ($constant as $singleconst) { 77 foreach ($constant as $singleconst) {
78 $res[$singleconst] = PHP_Compat::loadConstant($singleconst); 78 $res[$singleconst] = PHP_Compat::loadConstant($singleconst);
79 } 79 }
80   80  
81 return $res; 81 return $res;
82 } 82 }
83   83  
84 // Load constant 84 // Load constant
85 $file = sprintf('PHP/Compat/Constant/%s.php', $constant); 85 $file = sprintf('PHP/Compat/Constant/%s.php', $constant);
86 if ((@include_once $file) !== false) { 86 if ((@include_once $file) !== false) {
87 return true; 87 return true;
88 } 88 }
89   89  
90 return false; 90 return false;
91 } 91 }
92   92  
93   93  
94 /** 94 /**
95 * Load components for a PHP version 95 * Load components for a PHP version
96 * 96 *
97 * @param string $version PHP Version to load 97 * @param string $version PHP Version to load
98 * @return array An associative array of component names loaded 98 * @return array An associative array of component names loaded
99 */ 99 */
100 function loadVersion($version = null) 100 function loadVersion($version = null)
101 { 101 {
102 // Include list of components 102 // Include list of components
103 require 'PHP/Compat/Components.php'; 103 require 'PHP/Compat/Components.php';
104   104  
105 // Include version_compare to work with older versions 105 // Include version_compare to work with older versions
106 PHP_Compat::loadFunction('version_compare'); 106 PHP_Compat::loadFunction('version_compare');
107   107  
108 // Init 108 // Init
109 $phpversion = phpversion(); 109 $phpversion = phpversion();
110 $methods = array( 110 $methods = array(
111 'function' => 'loadFunction', 111 'function' => 'loadFunction',
112 'constant' => 'loadConstant'); 112 'constant' => 'loadConstant');
113 $res = array(); 113 $res = array();
114   114  
115 // Iterate each component 115 // Iterate each component
116 foreach ($components as $type => $slice) { 116 foreach ($components as $type => $slice) {
117 foreach ($slice as $component => $compversion) { 117 foreach ($slice as $component => $compversion) {
118 if (($version === null && 118 if (($version === null &&
119 1 === version_compare($compversion, $phpversion)) || // C > PHP 119 1 === version_compare($compversion, $phpversion)) || // C > PHP
120 (0 === version_compare($compversion, $version) || // C = S 120 (0 === version_compare($compversion, $version) || // C = S
121 1 === version_compare($compversion, $phpversion))) { // C > PHP 121 1 === version_compare($compversion, $phpversion))) { // C > PHP
122 122
123 $res[$type][$component] = 123 $res[$type][$component] =
124 call_user_func(array('PHP_Compat', $methods[$type]), $component); 124 call_user_func(array('PHP_Compat', $methods[$type]), $component);
125 } 125 }
126 } 126 }
127 } 127 }
128   128  
129 return $res; 129 return $res;
130 } 130 }
131 } 131 }
132   132  
133 ?> 133 ?>