Skip to content

Commit

Permalink
Added trace calls ignore by array of class name prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
barbushin committed Mar 20, 2015
1 parent 4f064f6 commit 223a739
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 39 deletions.
19 changes: 15 additions & 4 deletions src/PhpConsole/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,27 @@ protected function sendMessage(Message $message) {
* @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 $skipTraceCalls Last trace calls that will be stripped in result
* @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, $skipTraceCalls = 0) {
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 == $skipTraceCalls && isset($call['file'])) {
if(!$file && $i == $ignoreTraceCalls && isset($call['file'])) {
$file = $call['file'];
$line = $call['line'];
}
if($i < $skipTraceCalls || (isset($call['file']) && $call['file'] == $file && $call['line'] == $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]);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/PhpConsole/Dispatcher/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ class Debug extends \PhpConsole\Dispatcher {
* Send debug data message to client
* @param mixed $data
* @param null|string $tags Tags separated by dot, e.g. "low.db.billing"
* @param null|int $callLevel Number of proxy methods between original "debug call" and this method call
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
*/
public function dispatchDebug($data, $tags = null, $callLevel = 0) {
public function dispatchDebug($data, $tags = null, $ignoreTraceCalls = 0) {
if($this->isActive()) {
$message = new \PhpConsole\DebugMessage();
$message->data = $this->dumper->dump($data);
if($tags) {
$message->tags = explode('.', $tags);
}
if($this->detectTraceAndSource && $callLevel !== null) {
$message->trace = $this->fetchTrace(debug_backtrace(), $message->file, $message->line, $callLevel);
if($this->detectTraceAndSource && $ignoreTraceCalls !== null) {
$message->trace = $this->fetchTrace(debug_backtrace(), $message->file, $message->line, $ignoreTraceCalls);
}
$this->sendMessage($message);
}
Expand Down
8 changes: 4 additions & 4 deletions src/PhpConsole/Dispatcher/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ class Errors extends \PhpConsole\Dispatcher {
* @param null|string $text
* @param null|string $file
* @param null|integer $line
* @param null|int $callLevel Number of proxy methods between original "error handler method" and this method call
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
*/
public function dispatchError($code = null, $text = null, $file = null, $line = null, $callLevel = 0) {
public function dispatchError($code = null, $text = null, $file = null, $line = null, $ignoreTraceCalls = 0) {
if($this->isActive()) {
$message = new \PhpConsole\ErrorMessage();
$message->code = $code;
$message->class = $this->getErrorTypeByCode($code);
$message->data = $this->dumper->dump($text);
$message->file = $file;
$message->line = $line;
if($callLevel !== null) {
$message->trace = $this->fetchTrace(debug_backtrace(), $file, $line, $callLevel + 1);
if($ignoreTraceCalls !== null) {
$message->trace = $this->fetchTrace(debug_backtrace(), $file, $line, is_array($ignoreTraceCalls) ? $ignoreTraceCalls : $ignoreTraceCalls + 1);
}
$this->sendMessage($message);
}
Expand Down
12 changes: 6 additions & 6 deletions src/PhpConsole/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ public function checkFatalErrorOnShutDown() {
* @param string|null $file
* @param int|null $line
* @param null $context
* @param int $skipCallsLevel Number of proxy methods between original "error handler method" and this method call
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
*/
public function handleError($code = null, $text = null, $file = null, $line = null, $context = null, $skipCallsLevel = 0) {
public function handleError($code = null, $text = null, $file = null, $line = null, $context = null, $ignoreTraceCalls = 0) {
if(!$this->isStarted || error_reporting() === 0 || $this->isHandlingDisabled()) {
return;
}
$this->onHandlingStart();
$this->connector->getErrorsDispatcher()->dispatchError($code, $text, $file, $line, $skipCallsLevel + 1);
$this->connector->getErrorsDispatcher()->dispatchError($code, $text, $file, $line, is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls + 1 : $ignoreTraceCalls);
if($this->oldErrorsHandler && $this->callOldHandlers) {
call_user_func_array($this->oldErrorsHandler, array($code, $text, $file, $line, $context));
}
Expand Down Expand Up @@ -231,11 +231,11 @@ public function handleException(\Exception $exception) {
* Handle debug data
* @param mixed $data
* @param string|null $tags Tags separated by dot, e.g. "low.db.billing"
* @param int $skipTraceCalls Number of proxy methods between original "debug method call" and this method call
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
*/
public function debug($data, $tags = null, $skipTraceCalls = 0) {
public function debug($data, $tags = null, $ignoreTraceCalls = 0) {
if($this->connector->isActiveClient()) {
$this->connector->getDebugDispatcher()->dispatchDebug($data, $tags, $skipTraceCalls + 1);
$this->connector->getDebugDispatcher()->dispatchDebug($data, $tags, is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls + 1 : $ignoreTraceCalls);
}
}
}
6 changes: 3 additions & 3 deletions src/PhpConsole/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public static function getHandler() {
* Analog of Handler::getInstance()->debug(...) method
* @param mixed $data
* @param string|null $tags Tags separated by dot, e.g. "low.db.billing"
* @param int $skipTraceCalls Number of proxy methods between original "debug method call" and this method call
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
*/
public static function debug($data, $tags = null, $skipTraceCalls = 0) {
public static function debug($data, $tags = null, $ignoreTraceCalls = 0) {
if(self::$isActive) {
self::$connector->getDebugDispatcher()->dispatchDebug($data, $tags, $skipTraceCalls + 1);
self::$connector->getDebugDispatcher()->dispatchDebug($data, $tags, is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls + 1 : $ignoreTraceCalls);
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/PhpConsole/PsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ class PsrLogger extends \Psr\Log\AbstractLogger {
protected $connector;
/** @var Dumper */
protected $contextDumper;
protected $ignoreTraceCalls;

public function __construct(Connector $connector = null, Dumper $contextDumper = null) {
$this->connector = $connector ? : Connector::getInstance();
$this->contextDumper = $contextDumper ? : $this->connector->getDumper();
/**
* @param Connector|null $connector
* @param Dumper|null $contextDumper
* @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
*/
public function __construct(Connector $connector = null, Dumper $contextDumper = null, $ignoreTraceCalls = 1) {
$this->connector = $connector ?: Connector::getInstance();
$this->contextDumper = $contextDumper ?: $this->connector->getDumper();
$this->ignoreTraceCalls = $ignoreTraceCalls;
}

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

if(isset(static::$debugLevels[$level])) {
$this->connector->getDebugDispatcher()->dispatchDebug($message, static::$debugLevels[$level], null);
$this->connector->getDebugDispatcher()->dispatchDebug($message, static::$debugLevels[$level], $this->ignoreTraceCalls);
}
elseif(isset(static::$errorsLevels[$level])) {
if(isset($context['exception']) && $context['exception'] instanceof \Exception) {
$this->connector->getErrorsDispatcher()->dispatchException($context['exception']);
}
else {
$this->connector->getErrorsDispatcher()->dispatchError(static::$errorsLevels[$level], $message, null, null, null);
$this->connector->getErrorsDispatcher()->dispatchError(static::$errorsLevels[$level], $message, null, null, $this->ignoreTraceCalls);
}
}
else {
Expand Down
4 changes: 2 additions & 2 deletions tests/ClientEmulator/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function sendRequest(Request $request, $postponedResponseId = null, $post

$curlOptions = array(
CURLOPT_URL => $url . '?signature=' . $this->getPostDataSignature($rawPostData),
CURLOPT_CONNECTTIMEOUT => 100,
CURLOPT_TIMEOUT => 100,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 5,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $rawPostData,
Expand Down
37 changes: 33 additions & 4 deletions tests/Test/Dispatcher/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,43 @@ public function testSourceAndTraceDetection() {
$test->assertEquals(new \PhpConsole\TraceCall(array(
'file' => __FILE__,
'line' => $lastCallLine,
'call' => $message->data['class'] . '->' . $message->data['method'] . '(' . $lastCallLine . ', NULL, true, \'01234567890123...\', Array[2], stdClass)'
'call' => $message->data['method'] . '(' . $lastCallLine . ', NULL, true, \'01234567890123...\', Array[2], stdClass)'
)), end($message->trace));
return true;
}));
$this->callDispatchDebug($lastCallLine = __LINE__, null, true, '0123456789012345', array(1, 2), new \stdClass());

$dispatcher = $this->dispatcher;
$func = function () use ($dispatcher) {
$dispatcher->dispatchDebug(array('method' => __FUNCTION__, 'line' => __LINE__));
};
$func($lastCallLine = __LINE__, null, true, '0123456789012345', array(1, 2), new \stdClass());
}

protected function callDispatchDebug() {
$this->dispatcher->dispatchDebug(array('class' => get_class($this), 'method' => __FUNCTION__, 'line' => __LINE__));
public function testIgnoreCallsByNumber() {
$test = $this;
$actualTraceCalls = count(debug_backtrace());
$ignoreTraceCalls = 3;
$this->dispatcher->detectTraceAndSource = true;
$this->connector->expects($this->once())
->method('sendMessage')
->with($this->callback(function (\PhpConsole\DebugMessage $message) use ($test, $ignoreTraceCalls, $actualTraceCalls) {
$test->assertEquals($actualTraceCalls - $ignoreTraceCalls, count($message->trace));
return true;
}));
$this->dispatcher->dispatchDebug(null, null, $ignoreTraceCalls);
}

public function testIgnoreCallsByClassNames() {
$test = $this;
$actualTraceCalls = count(debug_backtrace());
$ignoreTraceClasses = array('PhpConsole\Test', 'ReflectionMethod');
$this->dispatcher->detectTraceAndSource = true;
$this->connector->expects($this->once())
->method('sendMessage')
->with($this->callback(function (\PhpConsole\DebugMessage $message) use ($test, $ignoreTraceClasses, $actualTraceCalls) {
$test->assertEquals($actualTraceCalls - count($ignoreTraceClasses), count($message->trace));
return true;
}));
$this->dispatcher->dispatchDebug(null, null, $ignoreTraceClasses);
}
}
4 changes: 4 additions & 0 deletions tests/Test/PsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ public function testExceptionContextVarIsDispatched() {
$this->assertInstanceOf('PhpConsole\ErrorMessage', $message);
$message->data = 'exception message';
}

public function testContextExceptionKeyCanBeExceptionOrOtherValues() {
$this->markTestSkipped('Stupid PSR issue that will not be implemented in PHP Console');
}
}
1 change: 0 additions & 1 deletion tests/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"require": {
"phpunit/phpunit": "3.7.*",
"psr/log": "dev-master"
}
}
12 changes: 7 additions & 5 deletions tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace PhpConsole\Test;

// Configure it up to your server
const SERVER_URL = 'http://localhost/php-console/tests/server.php'; // URL path to __DIR__ . '/server.php'
const SERVER_KEY = null; // some very very unique password :)
// Set actual to your server values
const SERVER_URL = ''; // URL path to __DIR__ . '/server.php'
const SERVER_KEY = ''; // some random string like kudhu1h3918da

// leave it as is
const LOCAL_IP = '127.0.0.1'; // local client IP
Expand Down Expand Up @@ -33,8 +33,10 @@ function getClientEmulator() {
}
});

if(!class_exists('PHPUnit_Framework_TestCase')) {
require_once(__DIR__ . '/vendor/autoload.php');
$composerAutoloadPath = __DIR__ . '/vendor/autoload.php';
if(!file_exists($composerAutoloadPath)) {
throw new \Exception('Test vendors not found. Run `composer install` in /tests directory');
}

require_once($composerAutoloadPath);
require_once(__DIR__ . '/../src/PhpConsole/__autoload.php');
1 change: 0 additions & 1 deletion tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
stopOnIncomplete="true"
stopOnSkipped="false"
bootstrap="./bootstrap.php"
strict="false"
verbose="true"
>

Expand Down

0 comments on commit 223a739

Please sign in to comment.