Skip to content

Commit 223a739

Browse files
committed
Added trace calls ignore by array of class name prefix
1 parent 4f064f6 commit 223a739

File tree

12 files changed

+90
-39
lines changed

12 files changed

+90
-39
lines changed

src/PhpConsole/Dispatcher.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,27 @@ protected function sendMessage(Message $message) {
5757
* @param array $trace Standard PHP backtrace array
5858
* @param null|string $file Reference to var that will contain source file path
5959
* @param null|string $line Reference to var that will contain source line number
60-
* @param int $skipTraceCalls Last trace calls that will be stripped in result
60+
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
6161
* @return TraceCall[]
6262
*/
63-
protected function fetchTrace(array $trace, &$file = null, &$line = null, $skipTraceCalls = 0) {
63+
protected function fetchTrace(array $trace, &$file = null, &$line = null, $ignoreTraceCalls = 0) {
64+
$ignoreByNumber = is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls : 0;
65+
$ignoreByClassPrefixes = is_array($ignoreTraceCalls) ? array_merge($ignoreTraceCalls, array(__NAMESPACE__)) : null;
66+
6467
foreach($trace as $i => $call) {
65-
if(!$file && $i == $skipTraceCalls && isset($call['file'])) {
68+
if(!$file && $i == $ignoreTraceCalls && isset($call['file'])) {
6669
$file = $call['file'];
6770
$line = $call['line'];
6871
}
69-
if($i < $skipTraceCalls || (isset($call['file']) && $call['file'] == $file && $call['line'] == $line)) {
72+
if($ignoreByClassPrefixes && isset($call['class'])) {
73+
foreach($ignoreByClassPrefixes as $classPrefix) {
74+
if(strpos($call['class'], $classPrefix) !== false) {
75+
unset($trace[$i]);
76+
continue;
77+
}
78+
}
79+
}
80+
if($i < $ignoreByNumber || (isset($call['file']) && $call['file'] == $file && $call['line'] == $line)) {
7081
unset($trace[$i]);
7182
}
7283
}

src/PhpConsole/Dispatcher/Debug.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ class Debug extends \PhpConsole\Dispatcher {
2121
* Send debug data message to client
2222
* @param mixed $data
2323
* @param null|string $tags Tags separated by dot, e.g. "low.db.billing"
24-
* @param null|int $callLevel Number of proxy methods between original "debug call" and this method call
24+
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
2525
*/
26-
public function dispatchDebug($data, $tags = null, $callLevel = 0) {
26+
public function dispatchDebug($data, $tags = null, $ignoreTraceCalls = 0) {
2727
if($this->isActive()) {
2828
$message = new \PhpConsole\DebugMessage();
2929
$message->data = $this->dumper->dump($data);
3030
if($tags) {
3131
$message->tags = explode('.', $tags);
3232
}
33-
if($this->detectTraceAndSource && $callLevel !== null) {
34-
$message->trace = $this->fetchTrace(debug_backtrace(), $message->file, $message->line, $callLevel);
33+
if($this->detectTraceAndSource && $ignoreTraceCalls !== null) {
34+
$message->trace = $this->fetchTrace(debug_backtrace(), $message->file, $message->line, $ignoreTraceCalls);
3535
}
3636
$this->sendMessage($message);
3737
}

src/PhpConsole/Dispatcher/Errors.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ class Errors extends \PhpConsole\Dispatcher {
4949
* @param null|string $text
5050
* @param null|string $file
5151
* @param null|integer $line
52-
* @param null|int $callLevel Number of proxy methods between original "error handler method" and this method call
52+
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
5353
*/
54-
public function dispatchError($code = null, $text = null, $file = null, $line = null, $callLevel = 0) {
54+
public function dispatchError($code = null, $text = null, $file = null, $line = null, $ignoreTraceCalls = 0) {
5555
if($this->isActive()) {
5656
$message = new \PhpConsole\ErrorMessage();
5757
$message->code = $code;
5858
$message->class = $this->getErrorTypeByCode($code);
5959
$message->data = $this->dumper->dump($text);
6060
$message->file = $file;
6161
$message->line = $line;
62-
if($callLevel !== null) {
63-
$message->trace = $this->fetchTrace(debug_backtrace(), $file, $line, $callLevel + 1);
62+
if($ignoreTraceCalls !== null) {
63+
$message->trace = $this->fetchTrace(debug_backtrace(), $file, $line, is_array($ignoreTraceCalls) ? $ignoreTraceCalls : $ignoreTraceCalls + 1);
6464
}
6565
$this->sendMessage($message);
6666
}

src/PhpConsole/Handler.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ public function checkFatalErrorOnShutDown() {
170170
* @param string|null $file
171171
* @param int|null $line
172172
* @param null $context
173-
* @param int $skipCallsLevel Number of proxy methods between original "error handler method" and this method call
173+
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
174174
*/
175-
public function handleError($code = null, $text = null, $file = null, $line = null, $context = null, $skipCallsLevel = 0) {
175+
public function handleError($code = null, $text = null, $file = null, $line = null, $context = null, $ignoreTraceCalls = 0) {
176176
if(!$this->isStarted || error_reporting() === 0 || $this->isHandlingDisabled()) {
177177
return;
178178
}
179179
$this->onHandlingStart();
180-
$this->connector->getErrorsDispatcher()->dispatchError($code, $text, $file, $line, $skipCallsLevel + 1);
180+
$this->connector->getErrorsDispatcher()->dispatchError($code, $text, $file, $line, is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls + 1 : $ignoreTraceCalls);
181181
if($this->oldErrorsHandler && $this->callOldHandlers) {
182182
call_user_func_array($this->oldErrorsHandler, array($code, $text, $file, $line, $context));
183183
}
@@ -231,11 +231,11 @@ public function handleException(\Exception $exception) {
231231
* Handle debug data
232232
* @param mixed $data
233233
* @param string|null $tags Tags separated by dot, e.g. "low.db.billing"
234-
* @param int $skipTraceCalls Number of proxy methods between original "debug method call" and this method call
234+
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
235235
*/
236-
public function debug($data, $tags = null, $skipTraceCalls = 0) {
236+
public function debug($data, $tags = null, $ignoreTraceCalls = 0) {
237237
if($this->connector->isActiveClient()) {
238-
$this->connector->getDebugDispatcher()->dispatchDebug($data, $tags, $skipTraceCalls + 1);
238+
$this->connector->getDebugDispatcher()->dispatchDebug($data, $tags, is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls + 1 : $ignoreTraceCalls);
239239
}
240240
}
241241
}

src/PhpConsole/Helper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public static function getHandler() {
8686
* Analog of Handler::getInstance()->debug(...) method
8787
* @param mixed $data
8888
* @param string|null $tags Tags separated by dot, e.g. "low.db.billing"
89-
* @param int $skipTraceCalls Number of proxy methods between original "debug method call" and this method call
89+
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
9090
*/
91-
public static function debug($data, $tags = null, $skipTraceCalls = 0) {
91+
public static function debug($data, $tags = null, $ignoreTraceCalls = 0) {
9292
if(self::$isActive) {
93-
self::$connector->getDebugDispatcher()->dispatchDebug($data, $tags, $skipTraceCalls + 1);
93+
self::$connector->getDebugDispatcher()->dispatchDebug($data, $tags, is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls + 1 : $ignoreTraceCalls);
9494
}
9595
}
9696

src/PhpConsole/PsrLogger.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ class PsrLogger extends \Psr\Log\AbstractLogger {
3535
protected $connector;
3636
/** @var Dumper */
3737
protected $contextDumper;
38+
protected $ignoreTraceCalls;
3839

39-
public function __construct(Connector $connector = null, Dumper $contextDumper = null) {
40-
$this->connector = $connector ? : Connector::getInstance();
41-
$this->contextDumper = $contextDumper ? : $this->connector->getDumper();
40+
/**
41+
* @param Connector|null $connector
42+
* @param Dumper|null $contextDumper
43+
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
44+
*/
45+
public function __construct(Connector $connector = null, Dumper $contextDumper = null, $ignoreTraceCalls = 1) {
46+
$this->connector = $connector ?: Connector::getInstance();
47+
$this->contextDumper = $contextDumper ?: $this->connector->getDumper();
48+
$this->ignoreTraceCalls = $ignoreTraceCalls;
4249
}
4350

4451
/**
@@ -52,14 +59,14 @@ public function log($level, $message, array $context = array()) {
5259
$message = $this->fetchMessageContext($message, $context);
5360

5461
if(isset(static::$debugLevels[$level])) {
55-
$this->connector->getDebugDispatcher()->dispatchDebug($message, static::$debugLevels[$level], null);
62+
$this->connector->getDebugDispatcher()->dispatchDebug($message, static::$debugLevels[$level], $this->ignoreTraceCalls);
5663
}
5764
elseif(isset(static::$errorsLevels[$level])) {
5865
if(isset($context['exception']) && $context['exception'] instanceof \Exception) {
5966
$this->connector->getErrorsDispatcher()->dispatchException($context['exception']);
6067
}
6168
else {
62-
$this->connector->getErrorsDispatcher()->dispatchError(static::$errorsLevels[$level], $message, null, null, null);
69+
$this->connector->getErrorsDispatcher()->dispatchError(static::$errorsLevels[$level], $message, null, null, $this->ignoreTraceCalls);
6370
}
6471
}
6572
else {

tests/ClientEmulator/Connector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public function sendRequest(Request $request, $postponedResponseId = null, $post
6060

6161
$curlOptions = array(
6262
CURLOPT_URL => $url . '?signature=' . $this->getPostDataSignature($rawPostData),
63-
CURLOPT_CONNECTTIMEOUT => 100,
64-
CURLOPT_TIMEOUT => 100,
63+
CURLOPT_CONNECTTIMEOUT => 2,
64+
CURLOPT_TIMEOUT => 5,
6565
CURLOPT_HEADER => true,
6666
CURLOPT_POST => true,
6767
CURLOPT_POSTFIELDS => $rawPostData,

tests/Test/Dispatcher/Debug.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,43 @@ public function testSourceAndTraceDetection() {
7070
$test->assertEquals(new \PhpConsole\TraceCall(array(
7171
'file' => __FILE__,
7272
'line' => $lastCallLine,
73-
'call' => $message->data['class'] . '->' . $message->data['method'] . '(' . $lastCallLine . ', NULL, true, \'01234567890123...\', Array[2], stdClass)'
73+
'call' => $message->data['method'] . '(' . $lastCallLine . ', NULL, true, \'01234567890123...\', Array[2], stdClass)'
7474
)), end($message->trace));
7575
return true;
7676
}));
77-
$this->callDispatchDebug($lastCallLine = __LINE__, null, true, '0123456789012345', array(1, 2), new \stdClass());
77+
78+
$dispatcher = $this->dispatcher;
79+
$func = function () use ($dispatcher) {
80+
$dispatcher->dispatchDebug(array('method' => __FUNCTION__, 'line' => __LINE__));
81+
};
82+
$func($lastCallLine = __LINE__, null, true, '0123456789012345', array(1, 2), new \stdClass());
7883
}
7984

80-
protected function callDispatchDebug() {
81-
$this->dispatcher->dispatchDebug(array('class' => get_class($this), 'method' => __FUNCTION__, 'line' => __LINE__));
85+
public function testIgnoreCallsByNumber() {
86+
$test = $this;
87+
$actualTraceCalls = count(debug_backtrace());
88+
$ignoreTraceCalls = 3;
89+
$this->dispatcher->detectTraceAndSource = true;
90+
$this->connector->expects($this->once())
91+
->method('sendMessage')
92+
->with($this->callback(function (\PhpConsole\DebugMessage $message) use ($test, $ignoreTraceCalls, $actualTraceCalls) {
93+
$test->assertEquals($actualTraceCalls - $ignoreTraceCalls, count($message->trace));
94+
return true;
95+
}));
96+
$this->dispatcher->dispatchDebug(null, null, $ignoreTraceCalls);
97+
}
98+
99+
public function testIgnoreCallsByClassNames() {
100+
$test = $this;
101+
$actualTraceCalls = count(debug_backtrace());
102+
$ignoreTraceClasses = array('PhpConsole\Test', 'ReflectionMethod');
103+
$this->dispatcher->detectTraceAndSource = true;
104+
$this->connector->expects($this->once())
105+
->method('sendMessage')
106+
->with($this->callback(function (\PhpConsole\DebugMessage $message) use ($test, $ignoreTraceClasses, $actualTraceCalls) {
107+
$test->assertEquals($actualTraceCalls - count($ignoreTraceClasses), count($message->trace));
108+
return true;
109+
}));
110+
$this->dispatcher->dispatchDebug(null, null, $ignoreTraceClasses);
82111
}
83112
}

tests/Test/PsrLogger.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ public function testExceptionContextVarIsDispatched() {
7979
$this->assertInstanceOf('PhpConsole\ErrorMessage', $message);
8080
$message->data = 'exception message';
8181
}
82+
83+
public function testContextExceptionKeyCanBeExceptionOrOtherValues() {
84+
$this->markTestSkipped('Stupid PSR issue that will not be implemented in PHP Console');
85+
}
8286
}

tests/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"require": {
3-
"phpunit/phpunit": "3.7.*",
43
"psr/log": "dev-master"
54
}
65
}

0 commit comments

Comments
 (0)