|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ExportTrait |
|
4
|
|
|
* |
|
5
|
|
|
* @package php-deferred-callchain |
|
6
|
|
|
* @author Jean Claveau |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace JClaveau\Async; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait gathering support of export functions like toString() or |
|
12
|
|
|
* jsonSerialize() |
|
13
|
|
|
*/ |
|
14
|
|
|
trait ExportTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* For implementing JsonSerializable interface. |
|
18
|
|
|
* |
|
19
|
|
|
* @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
|
20
|
|
|
*/ |
|
21
|
|
|
public function jsonSerialize() |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->stack; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Outputs the PHP code producing the current call chain while it's casted |
|
28
|
|
|
* as a string. |
|
29
|
|
|
* |
|
30
|
|
|
* @return string The PHP code corresponding to this call chain |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __toString() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->toString(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Outputs the PHP code producing the current call chain while it's casted |
|
39
|
|
|
* as a string. |
|
40
|
|
|
* |
|
41
|
|
|
* @return string The PHP code corresponding to this call chain |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function toString(array $options=[]) |
|
44
|
|
|
{ |
|
45
|
|
|
$target = isset($options['target']) ? $options['target'] : $this->expectedTarget; |
|
46
|
|
|
|
|
47
|
|
|
$string = '(new ' . get_called_class(); |
|
48
|
|
|
$target && $string .= '(' . static::varExport($target, ['short_objects']) . ')'; |
|
49
|
|
|
$string .= ')'; |
|
50
|
|
|
|
|
51
|
|
|
foreach ($this->stack as $i => $call) { |
|
52
|
|
|
if (isset($call['method'])) { |
|
53
|
|
|
$string .= '->'; |
|
54
|
|
|
$string .= $call['method'].'('; |
|
55
|
|
|
$string .= implode(', ', array_map(function($argument) { |
|
56
|
|
|
return static::varExport($argument, ['short_objects']); |
|
57
|
|
|
}, $call['arguments'])); |
|
58
|
|
|
$string .= ')'; |
|
59
|
|
|
} |
|
60
|
|
|
else { |
|
61
|
|
|
$string .= '[' . static::varExport($call['entry'], ['short_objects']) . ']'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (! empty($options['limit']) && $options['limit'] == $i) { |
|
65
|
|
|
break; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $string; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Enhanced var_export() required for dumps. |
|
74
|
|
|
* |
|
75
|
|
|
* @param mixed $variable |
|
76
|
|
|
* @param array $options max_length | alias_instances |
|
77
|
|
|
* @return string The PHP code of the variable |
|
78
|
|
|
*/ |
|
79
|
|
|
protected static function varExport($variable, array $options=[]) |
|
80
|
|
|
{ |
|
81
|
|
|
$options['max_length'] = isset($options['max_length']) ? $options['max_length'] : 512; |
|
82
|
|
|
$options['short_objects'] = ! empty($options['short_objects']) || in_array('short_objects', $options); |
|
83
|
|
|
|
|
84
|
|
|
$export = var_export($variable, true); |
|
85
|
|
|
|
|
86
|
|
|
if ($options['short_objects']) { |
|
87
|
|
|
if (is_object($variable)) { |
|
88
|
|
|
$export = ' ' . get_class($variable) . ' #' . spl_object_id($variable) . ' '; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (strlen($export) > $options['max_length']) { |
|
93
|
|
|
|
|
94
|
|
|
if (is_object($variable)) { |
|
95
|
|
|
$export = get_class($variable) . ' #' . spl_object_id($variable); |
|
96
|
|
|
} |
|
97
|
|
|
elseif (is_string($variable)) { |
|
98
|
|
|
$keep_length = floor(($options['max_length'] - 5) / 2); |
|
99
|
|
|
|
|
100
|
|
|
$export = substr($variable, 0, (int) $keep_length) |
|
101
|
|
|
. ' ... ' |
|
102
|
|
|
. substr($variable, -$keep_length) |
|
|
|
|
|
|
103
|
|
|
; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $export; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/**/ |
|
111
|
|
|
} |
|
112
|
|
|
|