forked from netcommons/NetCommons2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDumpHelper.class.php
More file actions
217 lines (198 loc) · 5.72 KB
/
DumpHelper.class.php
File metadata and controls
217 lines (198 loc) · 5.72 KB
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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Maple - PHP Web Application Framework
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @package Maple
* @author Hawk <scholar@hawklab.jp>
* @copyright 2004-2006 The Maple Project
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: DumpHelper.class.php,v 1.2 2006/05/23 05:17:32 Ryuji.M Exp $
*/
/**
* 配列・オブジェクトから循環参照を取り除くためのクラス
*
* @package Maple
* @author Hawk <scholar@hawklab.jp>
* @copyright 2004-2006 The Maple Project
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @access public
* @since 3.1.1
*/
class DumpHelper
{
/**
* @var array $_refStack 参照を格納するスタック
*/
var $_refStack;
/**
* @var boolean $_isPHP5 PHP5か否か
*/
var $_isPHP5;
/**
* コンストラクタ
*
* @access public
*/
function DumpHelper()
{
$this->_isPHP5 = version_compare(phpversion(), '5', '>=');
$this->reset();
}
/**
* SimpleTest
* http://www.lastcraft.com/unit_test_documentation.php
*
* assertReference が内部で用いている
* SimpleTestCompatibility::isReference より
*
* @access public
* @param mixed
* @param mixed
* @return boolean
*/
function isReference(&$first, &$second)
{
if ($this->_isPHP5 && is_object($first)) {
return ($first === $second);
}
$temp = $first;
$first = uniqid("dumphelper");
$is_ref = ($first === $second);
$first = $temp;
return $is_ref;
}
/**
* $var から循環参照を取り除いたものを返す
*
* @access public
* @param mixed
* @return mixed
*/
function removeCircularReference(&$var,$key=null)
{
$result = null;
if(is_object($var)) {
//省略配列
$skip_array = array("_db","db","_container","container");
if($key!=null && array_search($key,$skip_array) !== false) {
//省略
$result = $this->_getSubstituteFor($var);
}
elseif($this->_push($var)) {
//空の場合、エラーとなるので修正
if($this->_isPHP5) {
$result = $this->_cloneCreateObject($var);
} else
$result = $var;
//$result = $this->_isPHP5 ? clone($var) : $var;
//$skip_array = array("_db","db","_container","container");
foreach(array_keys(get_object_vars($var)) as $k) {
$tmp = $this->removeCircularReference($var->$k,$k);
$result->$k =& $tmp;
unset($tmp);
}
$this->_pop();
} else {
$result = $this->_getSubstituteFor($var);
}
} elseif(is_array($var)) {
if($this->_push($var)) {
$result = array();
foreach(array_keys($var) as $k) {
$result[$k] = $this->removeCircularReference($var[$k]);
}
$this->_pop();
} else {
$result = $this->_getSubstituteFor($var);
}
} else {
$result = $var;
}
return $result;
}
/**
* cloneラッパー関数
*
* @access protected
* @param mixed
* @return mixed
*/
function _cloneCreateObject(&$var)
{
$result = null;
$count_flag = 0;
foreach($var as $sub_value) {
$count_flag = 1;
break;
}
if($count_flag)
$result = clone($var);
return $result;
}
/**
* 循環しているobject or arrayの代替表現を返す
*
* @access protected
* @param mixed
* @return mixed
*/
function _getSubstituteFor($var)
{
if(is_object($var)) {
return '&object('. get_class($var) .')';
} elseif(is_array($var)) {
return '&array';
}
return "";
}
/**
* スタックをリセットする
* 厳密には処理が終わった時点でスタックは空になっているはずだが
* 念のため
*
* @access public
*/
function reset()
{
$this->_refStack = array();
}
/**
* 参照$varをスタックに積む
* 既にスタック内に同一の参照が存在する場合、
* スタックには積まずfalseを返す
*
* @param mixed $var
* @return boolean
*/
function _push(&$var)
{
$vartype = gettype($var);
foreach($this->_refStack as $i => $v) {
if($vartype == gettype($this->_refStack[$i]) &&
$this->isReference($var, $this->_refStack[$i])) {
return false;
}
}
$this->_refStack[] =& $var;
return true;
}
/**
* スタックから参照を取り除く
*
* access private
*/
function _pop()
{
array_pop($this->_refStack);
}
}
?>