Skip to content

Commit

Permalink
Merge pull request #835 from raul338/dashboard_toolbar
Browse files Browse the repository at this point in the history
Show Toolbar on Dashboard - Reload Request History
  • Loading branch information
markstory authored Sep 12, 2021
2 parents 8fbe61b + 0d7a40c commit 10d7d9b
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 24 deletions.
4 changes: 4 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
'/toolbar/*',
['controller' => 'Requests', 'action' => 'view']
);
$routes->connect(
'/panels/view/latest-history',
['controller' => 'Panels', 'action' => 'latestHistory']
);
$routes->connect(
'/panels/view/*',
['controller' => 'Panels', 'action' => 'view']
Expand Down
31 changes: 30 additions & 1 deletion src/Controller/PanelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,40 @@ public function index($requestId = null)
public function view($id = null)
{
$this->set('sort', $this->request->getCookie('debugKit_sort'));
$panel = $this->Panels->get($id);
$panel = $this->Panels->get($id, ['contain' => ['Requests']]);

$this->set('panel', $panel);
// @codingStandardsIgnoreStart
$this->set(@unserialize($panel->content));
// @codingStandardsIgnoreEnd
}

/**
* Get Latest request history panel
*
* @return \Cake\Http\Response|null
*/
public function latestHistory()
{
/** @var array{id:string}|null $request */
$request = $this->Panels->Requests->find('recent')
->select(['id'])
->disableHydration()
->first();
if (!$request) {
throw new NotFoundException('No requests found');
}
/** @var array{id:string}|null $historyPanel */
$historyPanel = $this->Panels->find('byRequest', ['requestId' => $request['id']])
->where(['title' => 'History'])
->select(['id'])
->first();
if (!$historyPanel) {
throw new NotFoundException('History Panel from latest request not found');
}

return $this->redirect([
'action' => 'view', $historyPanel['id'],
]);
}
}
2 changes: 2 additions & 0 deletions src/Model/Entity/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* @property string $title
* @property string $element
* @property string $content
*
* @property \DebugKit\Model\Entity\Request $request
*/
class Panel extends Entity
{
Expand Down
1 change: 1 addition & 0 deletions src/Model/Table/PanelsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* The panels table collects the information for each panel on
* each request.
*
* @property \DebugKit\Model\Table\RequestsTable&\Cake\ORM\Association\BelongsTo $Requests
* @method \DebugKit\Model\Entity\Panel get($primaryKey, $options = [])
* @method \DebugKit\Model\Entity\Panel newEntity($data = null, array $options = [])
* @method \DebugKit\Model\Entity\Panel[] newEntities(array $data, array $options = [])
Expand Down
24 changes: 14 additions & 10 deletions src/ToolbarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,23 @@ public function initializePanels()
*/
public function saveData(ServerRequest $request, ResponseInterface $response)
{
$ignorePathsPattern = $this->getConfig('ignorePathsPattern');
$path = $request->getUri()->getPath();
$statusCode = $response->getStatusCode();
$dashboardUrl = '/debug-kit';
if (strpos($path, 'debug_kit') !== false || strpos($path, 'debug-kit') !== false) {
if (!($path === $dashboardUrl || $path === $dashboardUrl . '/')) {
// internal debug-kit request
return false;
}
// debug-kit dashboard, save request and show toolbar
}

$ignorePathsPattern = $this->getConfig('ignorePathsPattern');
$statusCode = $response->getStatusCode();
if (
strpos($path, 'debug_kit') !== false ||
strpos($path, 'debug-kit') !== false ||
(
$ignorePathsPattern &&
$statusCode >= 200 &&
$statusCode <= 299 &&
preg_match($ignorePathsPattern, $path)
)
$ignorePathsPattern &&
$statusCode >= 200 &&
$statusCode <= 299 &&
preg_match($ignorePathsPattern, $path)
) {
return false;
}
Expand Down
40 changes: 27 additions & 13 deletions templates/element/history_panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* @since DebugKit 1.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

/**
* @var \DebugKit\View\AjaxView $this
* @var \DebugKit\Model\Entity\Panel $panel
Expand All @@ -20,16 +19,25 @@
?>
<div id="request-history">
<?php if (empty($requests)): ?>
<p class="warning"><?= __d('debug_kit', 'No previous requests logged.') ?></p>
<p class="warning">
<?= __d('debug_kit', 'No requests logged.') ?>
<button type="button" onclick="toolbar.loadPanel('latest-history')"><?= __d('debug_kit', 'Reload') ?></button>
</p>
<?php else: ?>
<p><?= count($requests) ?> <?= __d('debug_kit', 'previous requests available') ?></p>
<p>
<?= count($requests) ?> <?= __d('debug_kit', 'requests available') ?>
<button type="button" onclick="toolbar.loadPanel('latest-history')"><?= __d('debug_kit', 'Reload') ?></button>
</p>
<ul class="history-list">
<li>
<?= $this->Html->link(
__d('debug_kit', 'Back to current request'),
['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $panel->request_id],
['class' => 'history-link', 'data-request' => $panel->request_id]
) ?>
<?php $url = ['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $panel->request_id] ?>
<a class="history-link" data-request="<?= $panel->request_id ?>" href="<?= $this->Url->build($url) ?>">
<span class="history-time"><?= h($panel->request->requested_at) ?></span>
<span class="history-bubble"><?= h($panel->request->method) ?></span>
<span class="history-bubble"><?= h($panel->request->status_code) ?></span>
<span class="history-bubble"><?= h($panel->request->content_type) ?></span>
<span class="history-url"><?= h($panel->request->url) ?></span>
</a>
</li>
<?php foreach ($requests as $request): ?>
<?php $url = ['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $request->id] ?>
Expand All @@ -47,13 +55,19 @@
<?php endif; ?>
</div>
<script type="text/html" id="list-template">
<p>
<button type="button" onclick="toolbar.loadPanel('latest-history')"><?= __d('debug_kit', 'Reload') ?></button>
</p>
<ul class="history-list">
<li>
<?= $this->Html->link(
__d('debug_kit', 'Back to current request'),
['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $panel->request_id],
['class' => 'history-link', 'data-request' => $panel->request_id]
) ?>
<?php $url = ['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $panel->request_id] ?>
<a class="history-link" data-request="<?= $panel->request_id ?>" href="<?= $this->Url->build($url) ?>">
<span class="history-time"><?= h($panel->request->requested_at) ?></span>
<span class="history-bubble"><?= h($panel->request->method) ?></span>
<span class="history-bubble"><?= h($panel->request->status_code) ?></span>
<span class="history-bubble"><?= h($panel->request->content_type) ?></span>
<span class="history-url"><?= h($panel->request->url) ?></span>
</a>
</li>
</ul>
</script>
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Controller/PanelsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,21 @@ public function testViewNotExists()
$this->assertResponseError();
$this->assertResponseContains('Error page');
}

/**
* @return void
*/
public function testLatestHistory()
{
$request = $this->getTableLocator()->get('DebugKit.Requests')->find('recent')->first();
if (!$request) {
$request = $this->makeRequest();
}
$panel = $this->makePanel($request, 'DebugKit.History', 'History');

$this->get('/debug-kit/panels/view/latest-history');
$this->assertRedirect([
'action' => 'view', $panel->id,
]);
}
}
13 changes: 13 additions & 0 deletions tests/TestCase/ToolbarServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ public function testSaveDataIgnoreDebugKitDashedUrl()

$bar = new ToolbarService($this->events, []);
$this->assertFalse($bar->saveData($request, $response));

$request = new Request([
'url' => '/debug-kit',
'params' => [],
]);
$response = new Response([
'statusCode' => 200,
'type' => 'text/html',
'body' => '<html><title>test</title><body><p>some text</p></body>',
]);

$bar = new ToolbarService($this->events, []);
$this->assertNotEmpty($bar->saveData($request, $response));
}

/**
Expand Down

0 comments on commit 10d7d9b

Please sign in to comment.