Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
    /**
3
     *	base include file for SimpleTest
4
     *	@package	SimpleTest
5
     *	@version	$Id: compatibility.php 1532 2006-12-01 12:28:55Z xue $
6
     */
7
 
8
    /**
9
     *  Static methods for compatibility between different
10
     *  PHP versions.
11
     *  @package	SimpleTest
12
     */
13
    class SimpleTestCompatibility {
14
 
15
    	/**
16
    	 *	  Creates a copy whether in PHP5 or PHP4.
17
    	 *	  @param object $object		Thing to copy.
18
    	 *	  @return object			A copy.
19
    	 *	  @access public
20
    	 *	  @static
21
    	 */
22
    	static function copy($object) {
23
            if (version_compare(phpversion(), '5') >= 0) {
24
            	eval('$copy = clone $object;');
25
            	return $copy;
26
            }
27
            return $object;
28
    	}
29
 
30
        /**
31
         *    Identity test. Drops back to equality + types for PHP5
32
         *    objects as the === operator counts as the
33
         *    stronger reference constraint.
34
         *    @param mixed $first    Test subject.
35
         *    @param mixed $second   Comparison object.
36
         *	  @return boolean		 True if identical.
37
         *    @access public
38
         *    @static
39
         */
40
        static function isIdentical($first, $second) {
41
            if ($first != $second) {
42
                return false;
43
            }
44
            if (version_compare(phpversion(), '5') >= 0) {
45
                return SimpleTestCompatibility::_isIdenticalType($first, $second);
46
            }
47
            return ($first === $second);
48
        }
49
 
50
        /**
51
         *    Recursive type test.
52
         *    @param mixed $first    Test subject.
53
         *    @param mixed $second   Comparison object.
54
         *	  @return boolean		 True if same type.
55
         *    @access private
56
         *    @static
57
         */
58
        static function _isIdenticalType($first, $second) {
59
            if (gettype($first) != gettype($second)) {
60
                return false;
61
            }
62
            if (is_object($first) && is_object($second)) {
63
                if (get_class($first) != get_class($second)) {
64
                    return false;
65
                }
66
                return SimpleTestCompatibility::_isArrayOfIdenticalTypes(
67
                        get_object_vars($first),
68
                        get_object_vars($second));
69
            }
70
            if (is_array($first) && is_array($second)) {
71
                return SimpleTestCompatibility::_isArrayOfIdenticalTypes($first, $second);
72
            }
73
            return true;
74
        }
75
 
76
        /**
77
         *    Recursive type test for each element of an array.
78
         *    @param mixed $first    Test subject.
79
         *    @param mixed $second   Comparison object.
80
         *	  @return boolean		 True if identical.
81
         *    @access private
82
         *    @static
83
         */
84
        static function _isArrayOfIdenticalTypes($first, $second) {
85
            if (array_keys($first) != array_keys($second)) {
86
                return false;
87
            }
88
            foreach (array_keys($first) as $key) {
89
                $is_identical = SimpleTestCompatibility::_isIdenticalType(
90
                        $first[$key],
91
                        $second[$key]);
92
                if (! $is_identical) {
93
                    return false;
94
                }
95
            }
96
            return true;
97
        }
98
 
99
        /**
100
         *    Test for two variables being aliases.
101
         *    @param mixed $first    Test subject.
102
         *    @param mixed $second   Comparison object.
103
         *	  @return boolean		 True if same.
104
         *    @access public
105
         *    @static
106
         */
107
        static function isReference($first, $second) {
108
            if (version_compare(phpversion(), '5', '>=')
109
	    	    && is_object($first)) {
110
	    	    return ($first === $second);
111
	        }
112
	        if (is_object($first) && is_object($second)) {
113
                $id = uniqid("test");
114
                $first->$id = true;
115
                $is_ref = isset($second->$id);
116
                unset($first->$id);
117
                return $is_ref;
118
	        }
119
	        $temp = $first;
120
            $first = uniqid("test");
121
            $is_ref = ($first === $second);
122
            $first = $temp;
123
            return $is_ref;
124
        }
125
 
126
        /**
127
         *    Test to see if an object is a member of a
128
         *    class hiearchy.
129
         *    @param object $object    Object to test.
130
         *    @param string $class     Root name of hiearchy.
131
         *    @return boolean		  True if class in hiearchy.
132
         *    @access public
133
         *    @static
134
         */
135
        static function isA($object, $class) {
136
            if (version_compare(phpversion(), '5') >= 0) {
137
                if (! class_exists($class, false)) {
138
                    if (function_exists('interface_exists')) {
139
                        if (! interface_exists($class, false))  {
140
                            return false;
141
                        }
142
                    }
143
                }
144
                eval("\$is_a = \$object instanceof $class;");
145
                return $is_a;
146
            }
147
            if (function_exists('is_a')) {
148
                return is_a($object, $class);
149
            }
150
            return ((strtolower($class) == get_class($object))
151
                    or (is_subclass_of($object, $class)));
152
        }
153
 
154
        /**
155
         *    Sets a socket timeout for each chunk.
156
         *    @param resource $handle    Socket handle.
157
         *    @param integer $timeout    Limit in seconds.
158
         *    @access public
159
         *    @static
160
         */
161
        static function setTimeout($handle, $timeout) {
162
            if (function_exists('stream_set_timeout')) {
163
                stream_set_timeout($handle, $timeout, 0);
164
            } elseif (function_exists('socket_set_timeout')) {
165
                socket_set_timeout($handle, $timeout, 0);
166
            } elseif (function_exists('set_socket_timeout')) {
167
                set_socket_timeout($handle, $timeout, 0);
168
            }
169
        }
170
 
171
        /**
172
         *    Gets the current stack trace topmost first.
173
         *    @return array        List of stack frames.
174
         *    @access public
175
         *    @static
176
         */
177
        static function getStackTrace() {
178
            if (function_exists('debug_backtrace')) {
179
                return array_reverse(debug_backtrace());
180
            }
181
            return array();
182
        }
183
    }
184
?>