Skip to content

Commit

Permalink
Fix #390: Use opis/closure to serialize data
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarke authored and samdark committed Jul 23, 2019
1 parent 30c01eb commit 89c03f7
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 debug extension Change Log
2.1.6 under development
-----------------------

- Enh #390: Use opis/closure to serialize data (Sarke)
- Enh #379: Better error message when no debug data is found (Sarke)


Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"require": {
"php": ">=5.4",
"ext-mbstring": "*",
"yiisoft/yii2": "~2.0.13"
"yiisoft/yii2": "~2.0.13",
"opis/closure": "^3.3"
},
"require-dev": {
"yiisoft/yii2-swiftmailer": "*",
Expand Down
9 changes: 5 additions & 4 deletions src/LogTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;
use yii\log\Target;
use Opis\Closure;

/**
* The debug LogTarget is used to store logs for later use in the debugger tool
Expand Down Expand Up @@ -57,15 +58,15 @@ public function export()
$exceptions = [];
foreach ($this->module->panels as $id => $panel) {
try {
$data[$id] = serialize($panel->save());
$data[$id] = Closure\serialize($panel->save());
} catch (\Exception $exception) {
$exceptions[$id] = new FlattenException($exception);
}
}
$data['summary'] = $summary;
$data['exceptions'] = $exceptions;

file_put_contents($dataFile, serialize($data));
file_put_contents($dataFile, Closure\serialize($data));
if ($this->module->fileMode !== null) {
@chmod($dataFile, $this->module->fileMode);
}
Expand Down Expand Up @@ -96,15 +97,15 @@ private function updateIndexFile($indexFile, $summary)
// error while reading index data, ignore and create new
$manifest = [];
} else {
$manifest = unserialize($manifest);
$manifest = Closure\unserialize($manifest);
}

$manifest[$this->tag] = $summary;
$this->gc($manifest);

ftruncate($fp, 0);
rewind($fp);
fwrite($fp, serialize($manifest));
fwrite($fp, Closure\serialize($manifest));

@flock($fp, LOCK_UN);
@fclose($fp);
Expand Down
24 changes: 2 additions & 22 deletions src/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,33 +186,13 @@ public function isEnabled()
* level values. Value 0 means allowing all levels.
* @param array $categories the message categories to filter by. If empty, it means all categories are allowed.
* @param array $except the message categories to exclude. If empty, it means all categories are allowed.
* @param bool $stringify Convert non-string (such as closures) to strings
* @return array the filtered messages.
* @since 2.1.4
* @see \yii\log\Target::filterMessages()
*/
protected function getLogMessages($levels = 0, $categories = [], $except = [], $stringify = false)
protected function getLogMessages($levels = 0, $categories = [], $except = [])
{
$target = $this->module->logTarget;
$messages = $target->filterMessages($target->messages, $levels, $categories, $except);

if (!$stringify) {
return $messages;
}

foreach ($messages as &$message) {
if (!isset($message[0]) || is_string($message[0])) {
continue;
}

// exceptions may not be serializable if in the call stack somewhere is a Closure
if ($message[0] instanceof \Throwable || $message[0] instanceof \Exception) {
$message[0] = (string) $message[0];
} else {
$message[0] = VarDumper::export($message[0]);
}
}

return $messages;
return $target->filterMessages($target->messages, $levels, $categories, $except);
}
}
7 changes: 4 additions & 3 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use Opis\Closure;

/**
* Debugger controller provides browsing over available debug logs.
Expand Down Expand Up @@ -186,7 +187,7 @@ protected function getManifest($forceReload = false)
}

if ($content !== '') {
$this->_manifest = array_reverse(unserialize($content), true);
$this->_manifest = array_reverse(Closure\unserialize($content), true);
} else {
$this->_manifest = [];
}
Expand All @@ -209,12 +210,12 @@ public function loadData($tag, $maxRetry = 0)
$manifest = $this->getManifest($retry > 0);
if (isset($manifest[$tag])) {
$dataFile = $this->module->dataPath . "/$tag.data";
$data = unserialize(file_get_contents($dataFile));
$data = Closure\unserialize(file_get_contents($dataFile));
$exceptions = $data['exceptions'];
foreach ($this->module->panels as $id => $panel) {
if (isset($data[$id])) {
$panel->tag = $tag;
$panel->load(unserialize($data[$id]));
$panel->load(Closure\unserialize($data[$id]));
}
if (isset($exceptions[$id])) {
$panel->setError($exceptions[$id]);
Expand Down
2 changes: 1 addition & 1 deletion src/panels/DumpPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function save()
$except = $this->module->panels['router']->getCategories();
}

$messages = $this->getLogMessages(Logger::LEVEL_TRACE, $this->categories, $except, true);
$messages = $this->getLogMessages(Logger::LEVEL_TRACE, $this->categories, $except);

return $messages;
}
Expand Down
2 changes: 1 addition & 1 deletion src/panels/LogPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function save()
$except = $this->module->panels['router']->getCategories();
}

$messages = $this->getLogMessages(Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE, [], $except, true);
$messages = $this->getLogMessages(Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE, [], $except);

return ['messages' => $messages];
}
Expand Down
9 changes: 5 additions & 4 deletions tests/FlattenExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use yii\debug\FlattenException;
use yii\web\NotFoundHttpException;
use Opis\Closure;

/**
* @author Dmitry Bashkarev <[email protected]>
Expand Down Expand Up @@ -85,7 +86,7 @@ public function testArguments()
$dh = opendir(__DIR__);
$fh = tmpfile();

$incomplete = unserialize('O:14:"BogusTestClass":0:{}');
$incomplete = Closure\unserialize('O:14:"BogusTestClass":0:{}');

$exception = $this->createException([
(object)['foo' => 1],
Expand Down Expand Up @@ -151,7 +152,7 @@ public function testClosureSerialize()
});

$flattened = new FlattenException($exception);
$this->assertContains('Closure', serialize($flattened));
$this->assertContains('Closure', Closure\serialize($flattened));
}

public function testRecursionInArguments()
Expand All @@ -162,7 +163,7 @@ public function testRecursionInArguments()

$flattened = new FlattenException($exception);
$trace = $flattened->getTrace();
$this->assertContains('*DEEP NESTED ARRAY*', serialize($trace));
$this->assertContains('*DEEP NESTED ARRAY*', Closure\serialize($trace));
}

public function testTooBigArray()
Expand All @@ -184,7 +185,7 @@ public function testTooBigArray()

$this->assertSame($trace[0]['args'][0], ['array', ['array', '*SKIPPED over 10000 entries*']]);

$serializeTrace = serialize($trace);
$serializeTrace = Closure\serialize($trace);

$this->assertContains('*SKIPPED over 10000 entries*', $serializeTrace);
$this->assertNotContains('*value1*', $serializeTrace);
Expand Down

0 comments on commit 89c03f7

Please sign in to comment.