-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…es-to-the-debug-toolbar Dump debug panel
- Loading branch information
Showing
8 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\debug; | ||
|
||
use Craft; | ||
use yii\debug\Module as DebugModule; | ||
use yii\debug\Panel; | ||
use yii\web\NotFoundHttpException; | ||
|
||
/** | ||
* Debugger panel that collects and displays dumped variables. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.4.0 | ||
*/ | ||
class DumpPanel extends Panel | ||
{ | ||
/** | ||
* Displays a variable, if the Dump panel is active | ||
* | ||
* @param mixed $var The variable to be dumped. | ||
* @param string $file The source file or template name | ||
* @param int $line The line number | ||
*/ | ||
public static function dump(mixed $var, string $file, int $line): void | ||
{ | ||
$debugModule = Craft::$app->getModule('debug'); | ||
if ( | ||
$debugModule instanceof DebugModule && | ||
isset($debugModule->panels['dump']) && | ||
$debugModule->panels['dump'] instanceof DumpPanel | ||
) { | ||
ob_start(); | ||
Craft::dump($var); | ||
$debugModule->panels['dump']->data[] = [$file, $line, ob_get_clean()]; | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getName(): string | ||
{ | ||
return 'Dumps'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getSummary(): string | ||
{ | ||
return Craft::$app->getView()->render('@app/views/debug/dump/summary', [ | ||
'panel' => $this, | ||
]); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @throws NotFoundHttpException if a `trace` parameter is in the query string, but its value isn’t a valid deprecation warning’s ID | ||
*/ | ||
public function getDetail(): string | ||
{ | ||
return Craft::$app->getView()->render('@app/views/debug/dump/detail', [ | ||
'panel' => $this, | ||
]); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function save() | ||
{ | ||
return $this->data ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
|
||
/** @var craft\debug\DumpPanel $panel */ | ||
?> | ||
<h1>Variable Dumps</h1> | ||
<?php | ||
|
||
?> | ||
|
||
<?php if (empty($panel->data)): ?> | ||
<p>No variables were dumped on this request.</p> | ||
<?php else: ?> | ||
<?php foreach ($panel->data as [$file, $line, $dump]): ?> | ||
<h3><code><?= $file ?>:<?= $line ?></code></h3> | ||
<?= $dump ?> | ||
<?php endforeach; ?> | ||
<?php endif; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
/** @var craft\debug\DumpPanel $panel */ | ||
$count = count($panel->data); | ||
?> | ||
<div class="yii-debug-toolbar__block"> | ||
<a href="<?= $panel->getUrl() ?>"> | ||
Dumps <span | ||
class="yii-debug-toolbar__label"><?= $count ?></span> | ||
</a> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\web\twig\nodes; | ||
|
||
use craft\debug\DumpPanel; | ||
use craft\helpers\Template; | ||
use Twig\Compiler; | ||
use Twig\Node\Node; | ||
|
||
/** | ||
* Class DumpNode | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.4.0 | ||
*/ | ||
class DumpNode extends Node | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function compile(Compiler $compiler): void | ||
{ | ||
$compiler | ||
->addDebugInfo($this) | ||
->write(sprintf('%s::dump(', DumpPanel::class)); | ||
|
||
if ($this->hasNode('var')) { | ||
$compiler->subcompile($this->getNode('var')); | ||
} else { | ||
$compiler->raw(sprintf('%s::contextWithoutTemplate($context)', Template::class)); | ||
} | ||
|
||
$compiler | ||
->raw(sprintf(", '%s', %s);\n", $this->getTemplateName(), $this->getTemplateLine())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\web\twig\tokenparsers; | ||
|
||
use craft\web\twig\nodes\DumpNode; | ||
use Twig\Token; | ||
use Twig\TokenParser\AbstractTokenParser; | ||
|
||
/** | ||
* Class DumpTokenParser | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.4.0 | ||
*/ | ||
class DumpTokenParser extends AbstractTokenParser | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function parse(Token $token): DumpNode | ||
{ | ||
$lineno = $token->getLine(); | ||
$parser = $this->parser; | ||
$stream = $parser->getStream(); | ||
|
||
$nodes = []; | ||
|
||
if (!$stream->test(Token::BLOCK_END_TYPE)) { | ||
$nodes['var'] = $parser->getExpressionParser()->parseExpression(); | ||
} | ||
|
||
$stream->expect(Token::BLOCK_END_TYPE); | ||
|
||
return new DumpNode($nodes, [], $lineno, $this->getTag()); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getTag(): string | ||
{ | ||
return 'dump'; | ||
} | ||
} |