Skip to content

Commit 10d7d9b

Browse files
authored
Merge pull request #835 from raul338/dashboard_toolbar
Show Toolbar on Dashboard - Reload Request History
2 parents 8fbe61b + 0d7a40c commit 10d7d9b

File tree

8 files changed

+108
-24
lines changed

8 files changed

+108
-24
lines changed

config/routes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
'/toolbar/*',
1616
['controller' => 'Requests', 'action' => 'view']
1717
);
18+
$routes->connect(
19+
'/panels/view/latest-history',
20+
['controller' => 'Panels', 'action' => 'latestHistory']
21+
);
1822
$routes->connect(
1923
'/panels/view/*',
2024
['controller' => 'Panels', 'action' => 'view']

src/Controller/PanelsController.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,40 @@ public function index($requestId = null)
8383
public function view($id = null)
8484
{
8585
$this->set('sort', $this->request->getCookie('debugKit_sort'));
86-
$panel = $this->Panels->get($id);
86+
$panel = $this->Panels->get($id, ['contain' => ['Requests']]);
8787

8888
$this->set('panel', $panel);
8989
// @codingStandardsIgnoreStart
9090
$this->set(@unserialize($panel->content));
9191
// @codingStandardsIgnoreEnd
9292
}
93+
94+
/**
95+
* Get Latest request history panel
96+
*
97+
* @return \Cake\Http\Response|null
98+
*/
99+
public function latestHistory()
100+
{
101+
/** @var array{id:string}|null $request */
102+
$request = $this->Panels->Requests->find('recent')
103+
->select(['id'])
104+
->disableHydration()
105+
->first();
106+
if (!$request) {
107+
throw new NotFoundException('No requests found');
108+
}
109+
/** @var array{id:string}|null $historyPanel */
110+
$historyPanel = $this->Panels->find('byRequest', ['requestId' => $request['id']])
111+
->where(['title' => 'History'])
112+
->select(['id'])
113+
->first();
114+
if (!$historyPanel) {
115+
throw new NotFoundException('History Panel from latest request not found');
116+
}
117+
118+
return $this->redirect([
119+
'action' => 'view', $historyPanel['id'],
120+
]);
121+
}
93122
}

src/Model/Entity/Panel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* @property string $title
2424
* @property string $element
2525
* @property string $content
26+
*
27+
* @property \DebugKit\Model\Entity\Request $request
2628
*/
2729
class Panel extends Entity
2830
{

src/Model/Table/PanelsTable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* The panels table collects the information for each panel on
2222
* each request.
2323
*
24+
* @property \DebugKit\Model\Table\RequestsTable&\Cake\ORM\Association\BelongsTo $Requests
2425
* @method \DebugKit\Model\Entity\Panel get($primaryKey, $options = [])
2526
* @method \DebugKit\Model\Entity\Panel newEntity($data = null, array $options = [])
2627
* @method \DebugKit\Model\Entity\Panel[] newEntities(array $data, array $options = [])

src/ToolbarService.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,23 @@ public function initializePanels()
230230
*/
231231
public function saveData(ServerRequest $request, ResponseInterface $response)
232232
{
233-
$ignorePathsPattern = $this->getConfig('ignorePathsPattern');
234233
$path = $request->getUri()->getPath();
235-
$statusCode = $response->getStatusCode();
234+
$dashboardUrl = '/debug-kit';
235+
if (strpos($path, 'debug_kit') !== false || strpos($path, 'debug-kit') !== false) {
236+
if (!($path === $dashboardUrl || $path === $dashboardUrl . '/')) {
237+
// internal debug-kit request
238+
return false;
239+
}
240+
// debug-kit dashboard, save request and show toolbar
241+
}
236242

243+
$ignorePathsPattern = $this->getConfig('ignorePathsPattern');
244+
$statusCode = $response->getStatusCode();
237245
if (
238-
strpos($path, 'debug_kit') !== false ||
239-
strpos($path, 'debug-kit') !== false ||
240-
(
241-
$ignorePathsPattern &&
242-
$statusCode >= 200 &&
243-
$statusCode <= 299 &&
244-
preg_match($ignorePathsPattern, $path)
245-
)
246+
$ignorePathsPattern &&
247+
$statusCode >= 200 &&
248+
$statusCode <= 299 &&
249+
preg_match($ignorePathsPattern, $path)
246250
) {
247251
return false;
248252
}

templates/element/history_panel.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* @since DebugKit 1.1
1212
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
1514
/**
1615
* @var \DebugKit\View\AjaxView $this
1716
* @var \DebugKit\Model\Entity\Panel $panel
@@ -20,16 +19,25 @@
2019
?>
2120
<div id="request-history">
2221
<?php if (empty($requests)): ?>
23-
<p class="warning"><?= __d('debug_kit', 'No previous requests logged.') ?></p>
22+
<p class="warning">
23+
<?= __d('debug_kit', 'No requests logged.') ?>
24+
<button type="button" onclick="toolbar.loadPanel('latest-history')"><?= __d('debug_kit', 'Reload') ?></button>
25+
</p>
2426
<?php else: ?>
25-
<p><?= count($requests) ?> <?= __d('debug_kit', 'previous requests available') ?></p>
27+
<p>
28+
<?= count($requests) ?> <?= __d('debug_kit', 'requests available') ?>
29+
<button type="button" onclick="toolbar.loadPanel('latest-history')"><?= __d('debug_kit', 'Reload') ?></button>
30+
</p>
2631
<ul class="history-list">
2732
<li>
28-
<?= $this->Html->link(
29-
__d('debug_kit', 'Back to current request'),
30-
['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $panel->request_id],
31-
['class' => 'history-link', 'data-request' => $panel->request_id]
32-
) ?>
33+
<?php $url = ['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $panel->request_id] ?>
34+
<a class="history-link" data-request="<?= $panel->request_id ?>" href="<?= $this->Url->build($url) ?>">
35+
<span class="history-time"><?= h($panel->request->requested_at) ?></span>
36+
<span class="history-bubble"><?= h($panel->request->method) ?></span>
37+
<span class="history-bubble"><?= h($panel->request->status_code) ?></span>
38+
<span class="history-bubble"><?= h($panel->request->content_type) ?></span>
39+
<span class="history-url"><?= h($panel->request->url) ?></span>
40+
</a>
3341
</li>
3442
<?php foreach ($requests as $request): ?>
3543
<?php $url = ['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $request->id] ?>
@@ -47,13 +55,19 @@
4755
<?php endif; ?>
4856
</div>
4957
<script type="text/html" id="list-template">
58+
<p>
59+
<button type="button" onclick="toolbar.loadPanel('latest-history')"><?= __d('debug_kit', 'Reload') ?></button>
60+
</p>
5061
<ul class="history-list">
5162
<li>
52-
<?= $this->Html->link(
53-
__d('debug_kit', 'Back to current request'),
54-
['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $panel->request_id],
55-
['class' => 'history-link', 'data-request' => $panel->request_id]
56-
) ?>
63+
<?php $url = ['plugin' => 'DebugKit', 'controller' => 'Panels', 'action' => 'index', $panel->request_id] ?>
64+
<a class="history-link" data-request="<?= $panel->request_id ?>" href="<?= $this->Url->build($url) ?>">
65+
<span class="history-time"><?= h($panel->request->requested_at) ?></span>
66+
<span class="history-bubble"><?= h($panel->request->method) ?></span>
67+
<span class="history-bubble"><?= h($panel->request->status_code) ?></span>
68+
<span class="history-bubble"><?= h($panel->request->content_type) ?></span>
69+
<span class="history-url"><?= h($panel->request->url) ?></span>
70+
</a>
5771
</li>
5872
</ul>
5973
</script>

tests/TestCase/Controller/PanelsControllerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,21 @@ public function testViewNotExists()
9595
$this->assertResponseError();
9696
$this->assertResponseContains('Error page');
9797
}
98+
99+
/**
100+
* @return void
101+
*/
102+
public function testLatestHistory()
103+
{
104+
$request = $this->getTableLocator()->get('DebugKit.Requests')->find('recent')->first();
105+
if (!$request) {
106+
$request = $this->makeRequest();
107+
}
108+
$panel = $this->makePanel($request, 'DebugKit.History', 'History');
109+
110+
$this->get('/debug-kit/panels/view/latest-history');
111+
$this->assertRedirect([
112+
'action' => 'view', $panel->id,
113+
]);
114+
}
98115
}

tests/TestCase/ToolbarServiceTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ public function testSaveDataIgnoreDebugKitDashedUrl()
162162

163163
$bar = new ToolbarService($this->events, []);
164164
$this->assertFalse($bar->saveData($request, $response));
165+
166+
$request = new Request([
167+
'url' => '/debug-kit',
168+
'params' => [],
169+
]);
170+
$response = new Response([
171+
'statusCode' => 200,
172+
'type' => 'text/html',
173+
'body' => '<html><title>test</title><body><p>some text</p></body>',
174+
]);
175+
176+
$bar = new ToolbarService($this->events, []);
177+
$this->assertNotEmpty($bar->saveData($request, $response));
165178
}
166179

167180
/**

0 commit comments

Comments
 (0)