|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpConsole; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Abstract class of dispatchers that sends different kind data to connector as client expected messages |
|
7
|
|
|
* |
|
8
|
|
|
* @package PhpConsole |
|
9
|
|
|
* @version 3.1 |
|
10
|
|
|
* @link http://consle.com |
|
11
|
|
|
* @author Sergey Barbushin http://linkedin.com/in/barbushin |
|
12
|
|
|
* @copyright © Sergey Barbushin, 2011-2013. All rights reserved. |
|
13
|
|
|
* @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License" |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class Dispatcher { |
|
16
|
|
|
|
|
17
|
|
|
/** @var Connector */ |
|
18
|
|
|
protected $connector; |
|
19
|
|
|
/** @var Dumper */ |
|
20
|
|
|
protected $dumper; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Connector $connector |
|
24
|
|
|
* @param Dumper $dumper |
|
25
|
|
|
*/ |
|
26
|
71 |
|
public function __construct(Connector $connector, Dumper $dumper) { |
|
27
|
71 |
|
$this->connector = $connector; |
|
28
|
71 |
|
$this->setDumper($dumper); |
|
29
|
71 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Override default dumper |
|
33
|
|
|
* @param Dumper $dumper |
|
34
|
|
|
*/ |
|
35
|
71 |
|
public function setDumper(Dumper $dumper) { |
|
36
|
71 |
|
$this->dumper = $dumper; |
|
37
|
71 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check if dispatcher is active to send messages |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
49 |
|
public function isActive() { |
|
44
|
49 |
|
return $this->connector->isActiveClient(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Send message to PHP Console connector |
|
49
|
|
|
* @param Message $message |
|
50
|
|
|
*/ |
|
51
|
47 |
|
protected function sendMessage(Message $message) { |
|
52
|
47 |
|
$this->connector->sendMessage($message); |
|
53
|
47 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Convert backtrace to array of TraceCall with source file & line detection |
|
57
|
|
|
* @param array $trace Standard PHP backtrace array |
|
58
|
|
|
* @param null|string $file Reference to var that will contain source file path |
|
59
|
|
|
* @param null|string $line Reference to var that will contain source line number |
|
60
|
|
|
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore |
|
61
|
|
|
* @return TraceCall[] |
|
62
|
|
|
*/ |
|
63
|
35 |
|
protected function fetchTrace(array $trace, &$file = null, &$line = null, $ignoreTraceCalls = 0) { |
|
64
|
35 |
|
$ignoreByNumber = is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls : 0; |
|
65
|
35 |
|
$ignoreByClassPrefixes = is_array($ignoreTraceCalls) ? array_merge($ignoreTraceCalls, array(__NAMESPACE__)) : null; |
|
66
|
|
|
|
|
67
|
35 |
|
foreach($trace as $i => $call) { |
|
68
|
35 |
|
if(!$file && $i == $ignoreTraceCalls && isset($call['file'])) { |
|
69
|
10 |
|
$file = $call['file']; |
|
70
|
10 |
|
$line = $call['line']; |
|
71
|
|
|
} |
|
72
|
35 |
|
if($ignoreByClassPrefixes && isset($call['class'])) { |
|
73
|
1 |
|
foreach($ignoreByClassPrefixes as $classPrefix) { |
|
74
|
1 |
|
if(strpos($call['class'], $classPrefix) !== false) { |
|
75
|
1 |
|
unset($trace[$i]); |
|
76
|
1 |
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
35 |
|
if($i < $ignoreByNumber || (isset($call['file']) && $call['file'] == $file && $call['line'] == $line)) { |
|
81
|
35 |
|
unset($trace[$i]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
35 |
|
$traceCalls = array(); |
|
86
|
35 |
|
foreach(array_reverse($trace) as $call) { |
|
87
|
35 |
|
$args = array(); |
|
88
|
35 |
|
if(isset($call['args'])) { |
|
89
|
35 |
|
foreach($call['args'] as $arg) { |
|
90
|
35 |
|
if(is_object($arg)) { |
|
91
|
35 |
|
$args[] = get_class($arg); |
|
92
|
|
|
} |
|
93
|
35 |
|
elseif(is_array($arg)) { |
|
94
|
35 |
|
$args[] = 'Array[' . count($arg) . ']'; |
|
95
|
|
|
} |
|
96
|
|
|
else { |
|
97
|
35 |
|
$arg = var_export($arg, 1); |
|
98
|
35 |
|
$args[] = strlen($arg) > 15 ? substr($arg, 0, 15) . '...\'' : $arg; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
35 |
|
if(strpos($call['function'], '{closure}')) { |
|
104
|
3 |
|
$call['function'] = '{closure}'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
35 |
|
$traceCall = new TraceCall(); |
|
108
|
35 |
|
$traceCall->call = (isset($call['class']) ? $call['class'] . $call['type'] : '') . $call['function'] . '(' . implode(', ', $args) . ')'; |
|
109
|
35 |
|
if(isset($call['file'])) { |
|
110
|
35 |
|
$traceCall->file = $call['file']; |
|
111
|
|
|
} |
|
112
|
35 |
|
if(isset($call['line'])) { |
|
113
|
35 |
|
$traceCall->line = $call['line']; |
|
114
|
|
|
} |
|
115
|
35 |
|
$traceCalls[] = $traceCall; |
|
116
|
|
|
} |
|
117
|
35 |
|
return $traceCalls; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|