|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the koriym/printo package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Koriym\Printo; |
|
8
|
|
|
|
|
9
|
|
|
class Printo |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Extract object properties |
|
13
|
|
|
*/ |
|
14
|
|
|
const RANGE_PROPERTY = 1; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Extract array |
|
18
|
|
|
*/ |
|
19
|
|
|
const RANGE_ARRAY = 2; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Extract object in array |
|
23
|
|
|
*/ |
|
24
|
|
|
const RANGE_OBJECT_IN_ARRAY = 4; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Only object graph |
|
28
|
|
|
*/ |
|
29
|
|
|
const RANGE_OBJECT_ONLY = 0; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Extract all |
|
33
|
|
|
*/ |
|
34
|
|
|
const RANGE_ALL = 7; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var \SplObjectStorage |
|
37
|
|
|
*/ |
|
38
|
|
|
private $objectIdStorage; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \SplObjectStorage |
|
42
|
|
|
*/ |
|
43
|
|
|
private $sourceObjectStorage; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
private $graph = [ |
|
49
|
|
|
'nodes' => [], |
|
50
|
|
|
'links' => [] |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var int |
|
55
|
|
|
*/ |
|
56
|
|
|
private $nodeIndex = -1; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var NodeFactoryInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
private $nodeFactory; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var int |
|
65
|
|
|
*/ |
|
66
|
|
|
private $linkDistance = 100; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var int |
|
70
|
|
|
*/ |
|
71
|
|
|
private $charge = -300; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Default range is RANGE_PROPERTY | RANGE_ARRAY |
|
75
|
|
|
* |
|
76
|
|
|
* @var int |
|
77
|
|
|
*/ |
|
78
|
|
|
private $range = 3; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var object |
|
82
|
|
|
*/ |
|
83
|
|
|
private $object; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param object $object |
|
87
|
|
|
* @param NodeFactoryInterface $nodeFactory |
|
88
|
|
|
*/ |
|
89
|
9 |
|
public function __construct($object, ?NodeFactoryInterface $nodeFactory = null) |
|
90
|
|
|
{ |
|
91
|
9 |
|
$this->object = $object; |
|
92
|
9 |
|
$this->objectIdStorage = new \SplObjectStorage; |
|
93
|
9 |
|
$this->sourceObjectStorage = new \SplObjectStorage; |
|
94
|
9 |
|
$this->nodeFactory = $nodeFactory ?: new NodeFactory; |
|
95
|
9 |
|
if (is_array($this->object)) { |
|
|
|
|
|
|
96
|
2 |
|
$this->linkDistance = 200; |
|
97
|
|
|
} |
|
98
|
9 |
|
} |
|
99
|
|
|
|
|
100
|
8 |
|
public function __toString() |
|
101
|
|
|
{ |
|
102
|
8 |
|
if (is_object($this->object)) { |
|
103
|
6 |
|
$this->addObject($this->object); |
|
104
|
|
|
} |
|
105
|
8 |
|
if (is_array($this->object)) { |
|
106
|
2 |
|
$this->addArray(0, $this->object); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
8 |
|
$list = json_encode($this->graph); |
|
110
|
8 |
|
$linkDistance = $this->linkDistance; |
|
111
|
8 |
|
$charge = $this->charge; |
|
112
|
8 |
|
$html = require __DIR__ . '/html/default.php'; |
|
113
|
|
|
|
|
114
|
8 |
|
return $html; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $range |
|
119
|
|
|
* |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function setRange($range) |
|
123
|
|
|
{ |
|
124
|
3 |
|
$this->range = $range; |
|
125
|
|
|
|
|
126
|
3 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $linkDistance |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function setLinkDistance($linkDistance) |
|
135
|
|
|
{ |
|
136
|
1 |
|
$this->linkDistance = $linkDistance; |
|
137
|
|
|
|
|
138
|
1 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param $charge |
|
143
|
|
|
* |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function setCharge($charge) |
|
147
|
|
|
{ |
|
148
|
1 |
|
$this->charge = $charge; |
|
149
|
|
|
|
|
150
|
1 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param $object |
|
155
|
|
|
*/ |
|
156
|
6 |
|
private function addObject($object) |
|
157
|
|
|
{ |
|
158
|
6 |
|
$this->sourceObjectStorage->attach($object); |
|
159
|
6 |
|
$ref = new \ReflectionObject($object); |
|
160
|
6 |
|
$meta = ['file' => $ref->getFileName()]; |
|
161
|
6 |
|
$sourceIndex = $this->getObjectId($object, $meta); |
|
162
|
6 |
|
$props = $ref->getProperties(); |
|
163
|
6 |
|
foreach ($props as $prop) { |
|
164
|
6 |
|
$this->prop($prop, $object, $sourceIndex); |
|
165
|
|
|
} |
|
166
|
6 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param \ReflectionProperty $prop |
|
170
|
|
|
* @param object $object |
|
171
|
|
|
* @param int $sourceIndex |
|
172
|
|
|
*/ |
|
173
|
6 |
|
private function prop(\ReflectionProperty $prop, $object, $sourceIndex) |
|
174
|
|
|
{ |
|
175
|
6 |
|
$prop->setAccessible(true); |
|
176
|
6 |
|
$value = $prop->getValue($object); |
|
177
|
6 |
|
$nonObjectProperty = (! is_object($value) && (! ($this->range & self::RANGE_PROPERTY))); |
|
178
|
6 |
|
if ($nonObjectProperty) { |
|
179
|
1 |
|
return; |
|
180
|
|
|
} |
|
181
|
|
|
/* @var $prop \ReflectionProperty */ |
|
182
|
6 |
|
$meta = ['prop' => $prop->getName(), 'modifier' => $prop->getModifiers()]; |
|
183
|
6 |
|
$targetIndex = $this->addGraphLink($sourceIndex, $value, $meta); |
|
184
|
6 |
|
if (is_object($value) && ! $this->sourceObjectStorage->contains($value)) { |
|
185
|
6 |
|
$this->addObject($value); |
|
186
|
|
|
} |
|
187
|
6 |
|
if (is_array($value)) { |
|
188
|
6 |
|
$this->addArray($targetIndex, $value); |
|
189
|
|
|
} |
|
190
|
6 |
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param int $sourceIndex |
|
194
|
|
|
* @param array $array |
|
195
|
|
|
*/ |
|
196
|
8 |
|
private function addArray($sourceIndex, array $array) |
|
197
|
|
|
{ |
|
198
|
8 |
|
if (! ($this->range & self::RANGE_ARRAY)) { |
|
199
|
1 |
|
return; |
|
200
|
|
|
} |
|
201
|
8 |
|
foreach ($array as $key => $value) { |
|
202
|
8 |
|
if (is_object($value) && ($this->range & self::RANGE_OBJECT_IN_ARRAY)) { |
|
203
|
1 |
|
$this->addObject($value); |
|
204
|
1 |
|
continue; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
8 |
|
$targetIndex = $this->addGraphLink($sourceIndex, $value, ['key' => $key]); |
|
208
|
8 |
|
if (is_array($value) && $value !== $GLOBALS) { |
|
209
|
8 |
|
$this->addArray($targetIndex, $value); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
8 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param int $sourceIndex |
|
216
|
|
|
* @param mixed $value |
|
217
|
|
|
* @param array $meta |
|
218
|
|
|
* |
|
219
|
|
|
* @return int |
|
220
|
|
|
*/ |
|
221
|
8 |
|
private function addGraphLink($sourceIndex, $value, array $meta) |
|
222
|
|
|
{ |
|
223
|
8 |
|
$targetIndex = $this->getTargetIndex($value, $meta); |
|
224
|
8 |
|
$type = $this->getType($value); |
|
225
|
8 |
|
$this->graph['links'][] = ['source' => $sourceIndex, 'target' => $targetIndex, 'type' => $type]; |
|
226
|
|
|
|
|
227
|
8 |
|
return $targetIndex; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param $value |
|
232
|
|
|
* |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
8 |
|
private function getType($value) |
|
236
|
|
|
{ |
|
237
|
8 |
|
if (is_object($value)) { |
|
238
|
6 |
|
return 'object'; |
|
239
|
|
|
} |
|
240
|
8 |
|
if (is_array($value)) { |
|
241
|
8 |
|
return 'array'; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
8 |
|
return 'scalar'; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param mixed $value |
|
249
|
|
|
* @param array $meta |
|
250
|
|
|
* |
|
251
|
|
|
* @return int |
|
252
|
|
|
*/ |
|
253
|
8 |
|
private function getTargetIndex($value, array $meta) |
|
254
|
|
|
{ |
|
255
|
8 |
|
if (is_object($value)) { |
|
256
|
6 |
|
return $this->getObjectId($value, $meta); |
|
257
|
|
|
} |
|
258
|
8 |
|
$node = $this->nodeFactory->newInstance($value, $meta); |
|
259
|
8 |
|
$this->addNode($node); |
|
260
|
|
|
|
|
261
|
8 |
|
return $this->nodeIndex; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param $object |
|
266
|
|
|
* |
|
267
|
|
|
* @return int |
|
268
|
|
|
*/ |
|
269
|
6 |
|
private function getObjectId($object, array $meta) |
|
270
|
|
|
{ |
|
271
|
6 |
|
if ($this->objectIdStorage->contains($object)) { |
|
272
|
6 |
|
return (int) $this->objectIdStorage[$object]; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
6 |
|
$node = $this->nodeFactory->newInstance($object, $meta); |
|
276
|
6 |
|
$index = $this->addNode($node); |
|
277
|
6 |
|
$this->objectIdStorage->attach($object, (string) $index); |
|
278
|
|
|
|
|
279
|
6 |
|
return $index; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @param NodeInterface $node |
|
284
|
|
|
* |
|
285
|
|
|
* @return int |
|
286
|
|
|
*/ |
|
287
|
8 |
|
private function addNode(NodeInterface $node) |
|
288
|
|
|
{ |
|
289
|
8 |
|
$this->nodeIndex++; |
|
290
|
8 |
|
$this->graph['nodes'][] = $node->toArray(); |
|
291
|
|
|
|
|
292
|
8 |
|
return $this->nodeIndex; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|