/var/www/hkosl.com/b2b2c/webadmin/libraies/php-console/php-console/src/PhpConsole/Dispatcher.php


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
<?php

namespace PhpConsole;

/**
 * Abstract class of dispatchers that sends different kind data to connector as client expected messages
 *
 * @package PhpConsole
 * @version 3.1
 * @link http://php-console.com
 * @author Sergey Barbushin http://linkedin.com/in/barbushin
 * @copyright © Sergey Barbushin, 2011-2013. All rights reserved.
 * @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License"
 */
abstract class Dispatcher {

    
/** @var  Connector */
    
protected $connector;
    
/** @var Dumper */
    
protected $dumper;

    
/**
     * @param Connector $connector
     * @param Dumper $dumper
     */
    
public function __construct(Connector $connectorDumper $dumper) {
        
$this->connector $connector;
        
$this->setDumper($dumper);
    }

    
/**
     * Override default dumper
     * @param Dumper $dumper
     */
    
public function setDumper(Dumper $dumper) {
        
$this->dumper $dumper;
    }

    
/**
     * Check if dispatcher is active to send messages
     * @return bool
     */
    
public function isActive() {
        return 
$this->connector->isActiveClient();
    }

    
/**
     * Send message to PHP Console connector
     * @param Message $message
     */
    
protected function sendMessage(Message $message) {
        
$this->connector->sendMessage($message);
    }

    
/**
     * Convert backtrace to array of TraceCall with source file & line detection
     * @param array $trace Standard PHP backtrace array
     * @param null|string $file Reference to var that will contain source file path
     * @param null|string $line Reference to var that will contain source line number
     * @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
     * @return TraceCall[]
     */
    
protected function fetchTrace(array $trace, &$file null, &$line null$ignoreTraceCalls 0) {
        
$ignoreByNumber is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls 0;
        
$ignoreByClassPrefixes is_array($ignoreTraceCalls) ? array_merge($ignoreTraceCalls, array(__NAMESPACE__)) : null;

        foreach(
$trace as $i => $call) {
            if(!
$file && $i == $ignoreTraceCalls && isset($call['file'])) {
                
$file $call['file'];
                
$line $call['line'];
            }
            if(
$ignoreByClassPrefixes && isset($call['class'])) {
                foreach(
$ignoreByClassPrefixes as $classPrefix) {
                    if(
strpos($call['class'], $classPrefix) !== false) {
                        unset(
$trace[$i]);
                        continue;
                    }
                }
            }
            if(
$i $ignoreByNumber || (isset($call['file']) && $call['file'] == $file && $call['line'] == $line)) {
                unset(
$trace[$i]);
            }
        }

        
$traceCalls = array();
        foreach(
array_reverse($trace) as $call) {
            
$args = array();
            if(isset(
$call['args'])) {
                foreach(
$call['args'] as $arg) {
                    if(
is_object($arg)) {
                        
$args[] = get_class($arg);
                    }
                    elseif(
is_array($arg)) {
                        
$args[] = 'Array[' count($arg) . ']';
                    }
                    else {
                        
$arg var_export($arg1);
                        
$args[] = strlen($arg) > 15 substr($arg015) . '...\'' $arg;
                    }
                }
            }

            
$traceCall = new TraceCall();
            
$traceCall->call = (isset($call['class']) ? $call['class'] . $call['type'] : '') . $call['function'] . '(' implode(', '$args) . ')';
            if(isset(
$call['file'])) {
                
$traceCall->file $call['file'];
            }
            if(isset(
$call['line'])) {
                
$traceCall->line $call['line'];
            }
            
$traceCalls[] = $traceCall;
        }
        return 
$traceCalls;
    }
}