Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge request & session and environment & include panels #1006

Merged
merged 6 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
<code>$vendorName</code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Panel/EnvironmentPanel.php">
<RiskyTruthyFalsyComparison>
<code>$pluginName</code>
<code>$vendorName</code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Panel/IncludePanel.php">
<RiskyTruthyFalsyComparison>
<code>$pluginName</code>
Expand Down
74 changes: 74 additions & 0 deletions src/Panel/EnvironmentPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,31 @@
namespace DebugKit\Panel;

use Cake\Core\Configure;
use Cake\Error\Debugger;
use Cake\Event\EventInterface;
use DebugKit\DebugInclude;
use DebugKit\DebugPanel;

/**
* Provides information about your PHP and CakePHP environment to assist with debugging.
*/
class EnvironmentPanel extends DebugPanel
{
/**
* instance of DebugInclude
*
* @var \DebugKit\DebugInclude
*/
protected DebugInclude $_debug;

/**
* construct
*/
public function __construct()
{
$this->_debug = new DebugInclude();
}

/**
* Get necessary data about environment to pass back to controller
*
Expand Down Expand Up @@ -75,6 +92,10 @@ protected function _prepare(): array
$var = get_defined_constants(true);
$return['app'] = array_diff_key($var['user'], $return['cake'], $hiddenCakeConstants);

// Included files data
$return['includePaths'] = $this->_debug->includePaths();
$return['includedFiles'] = $this->prepareIncludedFiles();

return $return;
}

Expand All @@ -88,4 +109,57 @@ public function shutdown(EventInterface $event): void
{
$this->_data = $this->_prepare();
}

/**
* Build the list of files segmented by app, cake, plugins, vendor and other
*
* @return array
*/
protected function prepareIncludedFiles(): array
{
$return = ['cake' => [], 'app' => [], 'plugins' => [], 'vendor' => [], 'other' => []];

foreach (get_included_files() as $file) {
/** @var string|false $pluginName */
$pluginName = $this->_debug->getPluginName($file);

if ($pluginName) {
$return['plugins'][$pluginName][$this->_debug->getFileType($file)][] = $this->_debug->niceFileName(
$file,
'plugin',
$pluginName
);
} elseif ($this->_debug->isAppFile($file)) {
$return['app'][$this->_debug->getFileType($file)][] = $this->_debug->niceFileName($file, 'app');
} elseif ($this->_debug->isCakeFile($file)) {
$return['cake'][$this->_debug->getFileType($file)][] = $this->_debug->niceFileName($file, 'cake');
} else {
/** @var string|false $vendorName */
$vendorName = $this->_debug->getComposerPackageName($file);

if ($vendorName) {
$return['vendor'][$vendorName][] = $this->_debug->niceFileName($file, 'vendor', $vendorName);
} else {
$return['other'][] = $this->_debug->niceFileName($file, 'root');
}
}
}

$return['paths'] = $this->_debug->includePaths();

ksort($return['app']);
ksort($return['cake']);
ksort($return['plugins']);
ksort($return['vendor']);

foreach ($return['plugins'] as &$plugin) {
ksort($plugin);
}

foreach ($return as $k => $v) {
$return[$k] = Debugger::exportVarAsNodes($v);
}

return $return;
}
}
5 changes: 5 additions & 0 deletions src/Panel/IncludePanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\Utility\Hash;
use DebugKit\DebugInclude;
use DebugKit\DebugPanel;
use function Cake\Core\deprecationWarning;

/**
* Provides a list of included files for the current request
Expand All @@ -38,6 +39,10 @@ class IncludePanel extends DebugPanel
public function __construct()
{
$this->_debug = new DebugInclude();
deprecationWarning(
'5.1.0',
'Include panel is deprecated. Remove it from your panel configuration, and use Environment Panel instead.'
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Panel/RequestPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function shutdown(EventInterface $event): void
'data' => Debugger::exportVarAsNodes($request->getData(), $maxDepth),
'cookie' => Debugger::exportVarAsNodes($request->getCookieParams(), $maxDepth),
'get' => Debugger::exportVarAsNodes($_GET, $maxDepth),
'session' => Debugger::exportVarAsNodes($request->getSession()->read(), $maxDepth),
'matchedRoute' => $request->getParam('_matchedRoute'),
'headers' => [
'response' => headers_sent($file, $line),
Expand Down
5 changes: 5 additions & 0 deletions src/Panel/SessionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cake\Error\Debugger;
use Cake\Event\EventInterface;
use DebugKit\DebugPanel;
use function Cake\Core\deprecationWarning;

/**
* Provides debug information on the Session contents.
Expand All @@ -32,6 +33,10 @@ class SessionPanel extends DebugPanel
*/
public function shutdown(EventInterface $event): void
{
deprecationWarning(
'5.1.0',
'SessionPanel is deprecated. Remove it from your panel list, and use Request panel instead.'
);
/** @var \Cake\Controller\Controller $controller */
$controller = $event->getSubject();
$request = $controller->getRequest();
Expand Down
2 changes: 0 additions & 2 deletions src/ToolbarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ class ToolbarService
protected array $_defaultConfig = [
'panels' => [
'DebugKit.Cache' => true,
'DebugKit.Session' => true,
'DebugKit.Request' => true,
'DebugKit.SqlLog' => true,
'DebugKit.Timer' => true,
'DebugKit.Log' => true,
'DebugKit.Variables' => true,
'DebugKit.Environment' => true,
'DebugKit.Include' => true,
'DebugKit.History' => true,
'DebugKit.Routes' => true,
'DebugKit.Packages' => true,
Expand Down
18 changes: 15 additions & 3 deletions templates/element/environment_panel.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* Environment Panel Element
*
Expand All @@ -15,17 +17,19 @@
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
use Cake\Error\Debugger;
use function Cake\Core\h;

/**
* @var \DebugKit\View\AjaxView $this
* @var array $app
* @var array $cake
* @var array $php
* @var array $includedFiles
* @var array $includePaths
* @var \DebugKit\View\Helper\ToolbarHelper $this->Toolbar
* @var \DebugKit\View\Helper\CredentialsHelper $this->Credentials
*/

use function Cake\Core\h;
?>

<div class="c-environment-panel">
<h2>Application Constants</h2>

Expand Down Expand Up @@ -126,4 +130,12 @@
PHP environment unavailable.
</div>
<?php endif; ?>

<h2>Included Files</h2>

<h4>Include Paths</h4>
<?= $this->Toolbar->dumpNodes($includePaths) ?>

<h4>Included Files</h4>
<?= $this->Toolbar->dumpNodes($includedFiles) ?>
</div>
9 changes: 8 additions & 1 deletion templates/element/request_panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<code><?php echo h($routePath); ?></code>
</div>
<p>
<i>[Plugin].[Prefix]/[Controller]::[action]</i>
<i class="o-help">Route path grammar: [Plugin].[Prefix]/[Controller]::[action]</i>
</p>

<h4>Attributes</h4>
Expand Down Expand Up @@ -93,6 +93,13 @@
<p class="c-flash c-flash--info">No Cookie data.</p>
<?php endif; ?>

<h4>Session</h4>
<?php if (isset($session)) : ?>
<?= $this->Toolbar->dumpNode($session) ?>
<?php else : ?>
<p class="c-flash c-flash--info">No Session data.</p>
<?php endif; ?>

<?php if (!empty($matchedRoute)) : ?>
<h4>Matched Route</h4>
<p><?= $this->Toolbar->dumpNode(Debugger::exportVarAsNodes(['template' => $matchedRoute])) ?></p>
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Middleware/DebugKitMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ public function testInvokeSaveData()
$this->assertSame(200, $result->status_code);
$this->assertGreaterThan(1, $result->panels);

$this->assertSame('SqlLog', $result->panels[11]->panel);
$this->assertSame('DebugKit.sql_log_panel', $result->panels[11]->element);
$this->assertNotNull($result->panels[11]->summary);
$this->assertSame('Sql Log', $result->panels[11]->title);
$this->assertSame('Timer', $result->panels[10]->panel);
$this->assertSame('DebugKit.timer_panel', $result->panels[10]->element);
$this->assertNotNull($result->panels[10]->summary);
$this->assertSame('Timer', $result->panels[10]->title);

$timeStamp = filectime(Plugin::path('DebugKit') . 'webroot' . DS . 'js' . DS . 'inject-iframe.js');

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Panel/EnvironmentPanelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testShutdown()
$this->panel->shutdown($event);
$output = $this->panel->data();
$this->assertIsArray($output);
$this->assertSame(['php', 'ini', 'cake', 'app'], array_keys($output));
$this->assertSame(['php', 'ini', 'cake', 'app', 'includePaths', 'includedFiles'], array_keys($output));
$this->assertSame('mysql://user:password@localhost/my_db', $output['php']['TEST_URL_1']);
}
}
4 changes: 4 additions & 0 deletions tests/TestCase/Panel/RequestPanelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function testShutdownSkipAttributes()

$data = $this->panel->data();
$this->assertArrayHasKey('attributes', $data);
$this->assertArrayHasKey('session', $data);
$this->assertArrayHasKey('params', $data);
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('cookie', $data);
$this->assertEquals('string', $data['attributes']['ok']->getType());
$this->assertStringContainsString('Could not serialize `closure`', $data['attributes']['closure']->getValue());
}
Expand Down
11 changes: 7 additions & 4 deletions tests/TestCase/ToolbarServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,13 @@ public function testSaveData()
$this->assertSame(200, $result->status_code);
$this->assertGreaterThan(1, $result->panels);

$this->assertSame('SqlLog', $result->panels[11]->panel);
$this->assertSame('DebugKit.sql_log_panel', $result->panels[11]->element);
$this->assertSame('0', $result->panels[11]->summary);
$this->assertSame('Sql Log', $result->panels[11]->title);
$this->assertSame('Timer', $result->panels[10]->panel);
$this->assertSame('DebugKit.timer_panel', $result->panels[10]->element);
$this->assertMatchesRegularExpression(
'/\d+\.\d+\s[ms]+\s+\/\s+\d+\.\d+\s+[mbMB]+/',
$result->panels[10]->summary
);
$this->assertSame('Timer', $result->panels[10]->title);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions webroot/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ strong {
-webkit-animation: spin 4s linear infinite;
animation: spin 4s linear infinite;
}
.o-help {
color: var(--checkbox-label);
font-size: 14px;
}

@-webkit-keyframes spin {
100% {
Expand Down
Loading