1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ExportTrait |
5
|
|
|
* |
6
|
|
|
* @package php-deferred-callchain |
7
|
|
|
* @author Jean Claveau |
8
|
|
|
*/ |
9
|
|
|
namespace JClaveau\Async; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait gathering support of export functions like toString() or |
13
|
|
|
* jsonSerialize() |
14
|
|
|
*/ |
15
|
|
|
trait ExportTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* For implementing JsonSerializable interface. |
19
|
|
|
* |
20
|
|
|
* @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
21
|
|
|
*/ |
22
|
|
|
public function jsonSerialize() |
23
|
|
|
{ |
24
|
|
|
return $this->stack; |
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
|
|
|
* Outputs the PHP code producing the current call chain while it's casted |
38
|
|
|
* as a string. |
39
|
|
|
* |
40
|
|
|
* @param array $options target: mixed | max_parameter_length: int | short_objects: bool |
41
|
|
|
* @return string The PHP code corresponding to this call chain |
42
|
|
|
*/ |
43
|
|
|
public function toString(array $options = []) |
44
|
|
|
{ |
45
|
|
|
$target = isset($options['target']) ? $options['target'] : $this->expectedTarget; |
46
|
|
|
$max_param_length = isset($options['max_parameter_length']) ? $options['max_parameter_length'] : 56; |
47
|
|
|
$short_objects = isset($options['short_objects']) ? $options['short_objects'] : true; |
48
|
|
|
$string = '(new ' . get_called_class(); |
49
|
|
|
$target && ($string .= '(' . static::varExport($target, ['short_objects' => $short_objects, 'max_length' => $max_param_length]) . ')'); |
50
|
|
|
$string .= ')'; |
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) use($max_param_length, $short_objects) { |
56
|
|
|
return static::varExport($argument, ['short_objects' => $short_objects, 'max_length' => $max_param_length]); |
57
|
|
|
}, $call['arguments'])); |
58
|
|
|
$string .= ')'; |
59
|
|
|
} else { |
60
|
|
|
$string .= '[' . static::varExport($call['entry'], ['short_objects' => $short_objects, 'max_length' => $max_param_length]) . ']'; |
61
|
|
|
} |
62
|
|
|
if (!empty($options['limit']) && $options['limit'] == $i) { |
63
|
|
|
break; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return $string; |
67
|
|
|
} |
68
|
|
|
/** |
69
|
|
|
* Enhanced var_export() required for dumps. |
70
|
|
|
* |
71
|
|
|
* @param mixed $variable |
72
|
|
|
* @param array $options max_length: int | short_objects: bool |
73
|
|
|
* @return string The PHP code of the variable |
74
|
|
|
*/ |
75
|
|
|
protected static function varExport($variable, array $options = []) |
76
|
|
|
{ |
77
|
|
|
$options['max_length'] = isset($options['max_length']) ? $options['max_length'] : 56; |
78
|
|
|
$options['short_objects'] = !empty($options['short_objects']) || in_array('short_objects', $options); |
79
|
|
|
$export = var_export($variable, true); |
80
|
|
|
if ($options['short_objects']) { |
81
|
|
|
if (is_object($variable)) { |
82
|
|
|
$export = ' ' . get_class($variable) . ' #' . spl_object_id($variable) . ' '; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
if (strlen($export) > $options['max_length']) { |
86
|
|
|
if (is_object($variable)) { |
87
|
|
|
// shortening short objects would only slow the workflow |
88
|
|
|
$export = get_class($variable) . ' #' . spl_object_id($variable); |
89
|
|
|
} elseif (is_string($variable)) { |
90
|
|
|
$keep_length = floor(($options['max_length'] - 5) / 2); |
91
|
|
|
$export = substr($variable, 0, (int) $keep_length) . ' ... ' . substr($variable, -$keep_length); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return $export; |
95
|
|
|
} |
96
|
|
|
} |