Skip to content

Commit 630325c

Browse files
authored
Merge pull request #76 from vpietri/fix_dumper_ajax
Catch dumper in ajax calls
2 parents bbb5cbd + e1f6493 commit 630325c

File tree

18 files changed

+291
-139
lines changed

18 files changed

+291
-139
lines changed

Block/Tab/Panel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ADM\QuickDevBar\Block\Tab;
44

5+
use Magento\Framework\App\Config\ScopeConfigInterface;
56
use Magento\Framework\App\ObjectManager;
67

78
class Panel extends \Magento\Framework\View\Element\Template
@@ -240,4 +241,10 @@ public function formatTrace(array $bt)
240241
return $this->helper->getIDELinkForFile($bt['file'], $bt['line']);
241242
}
242243

244+
public function getQdbConfig($key, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
245+
246+
{
247+
return $this->helper->getQdbConfig($key, $scopeType, $scopeCode);
248+
}
249+
243250
}

Block/Tab/Wrapper.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,8 @@ public function getTabBlocks()
3333
if ($this->_mainTabs === null) {
3434
$this->_mainTabs=[];
3535
foreach ($this->getLayout()->getChildBlocks($this->getNameInLayout()) as $alias => $block) {
36-
if(!$block->getTitleBadge() && $block->getVisibleOnContent()) {
37-
continue;
38-
}
39-
40-
4136
$this->_mainTabs[$alias]=$block;
4237
}
43-
44-
45-
4638
}
4739

4840
return $this->_mainTabs;

Block/Toolbar.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ public function getAppearance()
4545
return $this->_qdnHelper->defaultAppearance();
4646
}
4747

48-
49-
50-
public function getAjaxUrl()
51-
{
52-
return $this->getUrl('quickdevbar/index/ajax');
53-
}
54-
5548
public function isAjaxLoading()
5649
{
5750
return $this->_qdnHelper->isAjaxLoading();

Controller/Tab/Ajax.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33

44
class Ajax extends \ADM\QuickDevBar\Controller\Index
55
{
6+
7+
protected \ADM\QuickDevBar\Helper\Register $qdbHelperRegister;
8+
9+
public function __construct(\Magento\Framework\App\Action\Context $context,
10+
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
11+
\Magento\Framework\View\LayoutFactory $layoutFactory,
12+
\ADM\QuickDevBar\Helper\Data $qdbHelper,
13+
\ADM\QuickDevBar\Helper\Register $qdbHelperRegister
14+
)
15+
{
16+
parent::__construct($context, $qdbHelper, $resultRawFactory, $layoutFactory);
17+
$this->qdbHelperRegister = $qdbHelperRegister;
18+
}
19+
20+
621
/**
722
*
823
* @return \Magento\Backend\Model\View\Result\Page
@@ -14,8 +29,13 @@ public function execute()
1429
try {
1530
$this->_view->loadLayout('quickdevbar');
1631

17-
if ($this->_view->getLayout()->getBlock($blockName)) {
18-
$output = $this->_view->getLayout()->getBlock($blockName)->toHtml();
32+
$block = $this->_view->getLayout()->getBlock($blockName);
33+
if ($block) {
34+
if($block->getNeedLoadData()) {
35+
$this->qdbHelperRegister->loadDataFromFile(true);
36+
}
37+
$block->setIsUpdateCall(true);
38+
$output = $block->toHtml();
1939
} else {
2040
$output = 'Cannot found block: '. $blockName;
2141
}

Helper/Data.php

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,10 @@ public function getModuleVersion($moduleName)
310310
return !empty($moduleInfo['setup_version']) ? $moduleInfo['setup_version'] : '???';
311311
}
312312

313-
protected function getWrapperFilename($ajax = false)
313+
protected function getWrapperBaseFilename($ajax = false)
314314
{
315315
$sessionId = $this->session->getSessionId();
316-
317-
$fileName = 'qdb_register_'.$sessionId.'.json';
318-
if($ajax) {
319-
$fileName = $this->_getRequest()->isAjax()
320-
? 'qdb_ajax_register_'.$sessionId.'_'.time().'.json'
321-
: 'qdb_unknown_register_'.$sessionId.'_'.time().'.json';
322-
}
323-
324-
return $this->getQdbTempDir() . $fileName;
316+
return 'qdb_register_' . (!$ajax ? 'std' : 'xhr') . '_' . $sessionId;
325317
}
326318

327319

@@ -335,34 +327,70 @@ protected function getQdbTempDir()
335327

336328
public function getWrapperContent($ajax = false)
337329
{
338-
//Clean old files
339-
/** @var \SplFileInfo $fileInfo */
330+
//Clean old files
331+
// /** @var \SplFileInfo $fileInfo */
332+
// foreach (new \DirectoryIterator($this->getQdbTempDir()) as $fileInfo) {
333+
// if($fileInfo->isFile() && time() - $fileInfo->getMTime() > 20) {
334+
// //TODO: unlink only files starting with 'qdb_register_' . $sessionId
335+
// unlink($fileInfo->getPathname());
336+
// }
337+
// }
338+
339+
$wrapperFiles = [];
340+
$filename = $this->getWrapperBaseFilename($ajax);
340341
foreach (new \DirectoryIterator($this->getQdbTempDir()) as $fileInfo) {
341-
if($fileInfo->isFile() && time() - $fileInfo->getMTime() > 20) {
342-
unlink($fileInfo->getPathname());
342+
if($fileInfo->isFile() && strpos($fileInfo->getFilename(), $filename)===0) {
343+
$wrapperFiles[] = $fileInfo->getPathname();
343344
}
344345
}
345346

346-
$filename = $this->getWrapperFilename($ajax);
347-
if(!file_exists($filename)) {
348-
throw new LocalizedException(__('No file for wrapper'));
347+
if(empty($wrapperFiles)) {
348+
throw new LocalizedException(__('No files for wrapper'));
349+
}
350+
351+
$serializer = new \Magento\Framework\Serialize\Serializer\Json();
352+
353+
$content = [];
354+
foreach ($wrapperFiles as $wrapperContent) {
355+
$jsonContent = file_get_contents($wrapperContent);
356+
if($jsonContent) {
357+
foreach ($serializer->unserialize($jsonContent) as $contentKey => $contentValue) {
358+
$content[$contentKey] = empty($content[$contentKey]) ? $contentValue : array_merge($content[$contentKey], $contentValue);
359+
}
360+
}
361+
//TODO: remove foreach
362+
break;
349363
}
350364

351-
$content = file_get_contents($filename);
352365
if(empty($content)) {
353366
throw new LocalizedException(__('No data registered'));
354367
}
355368

369+
/** @var \SplFileInfo $fileInfo */
370+
foreach (new \DirectoryIterator($this->getQdbTempDir()) as $fileInfo) {
371+
if($fileInfo->isFile()) {
372+
//TODO: unlink only files starting with 'qdb_register_' . $sessionId
373+
unlink($fileInfo->getPathname());
374+
}
375+
}
376+
356377
return $content;
357378
}
358379

359380

360381
public function setWrapperContent($content, $ajax = false)
361382
{
362-
file_put_contents($this->getWrapperFilename($ajax), $content);
383+
$filename = $this->getWrapperBaseFilename($ajax);
384+
if($ajax) {
385+
$filename .= time();
386+
}
387+
file_put_contents($this->getQdbTempDir() . $filename . '.json', $content);
363388
}
364389

365390
/**
391+
* TODO: To removed
392+
* Asymmetric behavior frontend/admin is no more necessary
393+
*
366394
* @return bool
367395
* @throws LocalizedException
368396
*/

Helper/Register.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ public function __construct(Context $context,
3636
$this->objectFactory = $objectFactory;
3737
$this->qdbHelper = $qdbHelper;
3838
$this->services = $services;
39-
if($this->qdbHelper->isToolbarAccessAllowed() && $this->qdbHelper->isAjaxLoading()) {
40-
register_shutdown_function([$this, 'dumpToFile']);
41-
}
4239
}
4340

4441

@@ -47,36 +44,35 @@ public function __construct(Context $context,
4744
*/
4845
public function dumpToFile()
4946
{
47+
$isAjax = $this->_getRequest()->isAjax();
5048
if($this->_getRequest() && $this->_getRequest()->getModuleName()=='quickdevbar') {
5149
return false;
5250
}
5351

5452
foreach ($this->services as $serviceKey => $serviceObj) {
53+
//TODO: Filter keys on $isAjax
54+
if($isAjax && $serviceKey!='dumps') {
55+
continue;
56+
}
57+
5558
$this->setRegisteredData($serviceKey, $serviceObj->pullData());
5659
}
5760
$content = $this->registeredData->convertToJson();
58-
$this->qdbHelper->setWrapperContent($content);
61+
62+
$this->qdbHelper->setWrapperContent($content, $isAjax);
5963
}
6064

6165
/**
6266
*
6367
*/
64-
public function loadDataFromFile()
68+
public function loadDataFromFile($ajax = false)
6569
{
66-
$wrapperContent = $this->qdbHelper->getWrapperContent();
67-
$this->setRegisteredJsonData($wrapperContent);
70+
$wrapperContent = $this->qdbHelper->getWrapperContent($ajax);
71+
$this->setRegisteredData($wrapperContent);
6872
$this->pullDataFromService = false;
6973
}
7074

7175

72-
/**
73-
* @param $data
74-
*/
75-
public function setRegisteredJsonData($data)
76-
{
77-
$serializer = new \Magento\Framework\Serialize\Serializer\Json();
78-
$this->setRegisteredData($serializer->unserialize($data));
79-
}
8076

8177
/**
8278
* @param null $key
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace ADM\QuickDevBar\Model\Config\Source;
4+
5+
class DumperHandler implements \Magento\Framework\Option\ArrayInterface
6+
{
7+
/**
8+
* Options getter
9+
*
10+
* @return array
11+
*/
12+
public function toOptionArray()
13+
{
14+
return [
15+
['value' => 0, 'label' => __('No')],
16+
['value' => 1, 'label' => __('Current page')],
17+
['value' => 2, 'label' => __('Current page and ajax calls')]
18+
];
19+
}
20+
}

Plugin/Framework/App/FrontController.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,67 @@
22

33
namespace ADM\QuickDevBar\Plugin\Framework\App;
44

5+
use ADM\QuickDevBar\Helper\Data;
6+
use ADM\QuickDevBar\Helper\Register;
7+
use ADM\QuickDevBar\Service\Dumper;
8+
use Magento\Framework\App\RequestInterface;
59
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
610
use Symfony\Component\VarDumper\VarDumper;
711
use Symfony\Component\VarDumper\Cloner\VarCloner;
812

913

1014
class FrontController
1115
{
16+
private $request;
17+
1218
private $qdbHelper;
1319

1420
private $dumper;
1521

22+
private Register $register;
23+
1624
/**
17-
* @param \ADM\QuickDevBar\Service\Dumper $dumper
25+
* @param RequestInterface $request
26+
* @param Data $qdbHelper
27+
* @param Register $register
28+
* @param Dumper $dumper
1829
*/
19-
public function __construct(\ADM\QuickDevBar\Helper\Data $qdbHelper,
20-
\ADM\QuickDevBar\Service\Dumper $dumper
30+
public function __construct(RequestInterface $request,
31+
Data $qdbHelper,
32+
Register $register,
33+
Dumper $dumper
2134
)
2235
{
36+
$this->request = $request;
2337
$this->qdbHelper = $qdbHelper;
38+
$this->register = $register;
2439
$this->dumper = $dumper;
2540
}
2641

2742
/**
43+
* Be careful, two usage:
44+
* - dumpToFile
45+
* - VarDumper::setHandler
46+
*
2847
* @param \Magento\Framework\AppInterface $subject
2948
* @return void
3049
*/
3150
public function beforeDispatch(\Magento\Framework\App\FrontControllerInterface $subject)
3251
{
33-
if($this->qdbHelper->getQdbConfig('handle_vardumper')) {
52+
53+
54+
if(!$this->qdbHelper->isToolbarAccessAllowed()) {
55+
return;
56+
}
57+
58+
if($this->qdbHelper->isAjaxLoading()) {
59+
register_shutdown_function([$this->register, 'dumpToFile']);
60+
}
61+
62+
if($enabledHandler = $this->qdbHelper->getQdbConfig('handle_vardumper')) {
63+
if($this->request->isAjax() && $enabledHandler<2) {
64+
return;
65+
}
3466
$prevHandler = VarDumper::setHandler($this->dumperHandler(...));
3567
}
3668
}
@@ -47,11 +79,12 @@ protected function dumperHandler($var)
4779
$dumper->setTheme('dark');
4880
$dumpBt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[2];
4981

82+
$ajaxReq = $this->request->isAjax() ? $this->request->getActionName() : null;
83+
5084
$output = $dumpBt['function'] != 'dd';
5185
$dumpOutput = $dumper->dump($cloner->cloneVar($var), $output);
5286
if($output) {
53-
$this->dumper->addDump($dumpOutput, $dumpBt);
87+
$this->dumper->addDump($dumpOutput, $dumpBt, $ajaxReq);
5488
}
55-
5689
}
5790
}

Service/Dumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public function pullData()
1616
return $this->dumps;
1717
}
1818

19-
public function addDump(string $output, array $bt)
19+
public function addDump(string $output, array $bt, $ajaxReq = null)
2020
{
21-
$this->dumps[] = ['dump'=>$output, 'bt'=> $bt];
21+
$this->dumps[] = ['dump'=>$output, 'bt'=> $bt, 'ajaxReq'=> $ajaxReq];
2222
}
2323
}

Service/Event/Manager.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public function __construct(array $services = [])
2727
*/
2828
public function addEvent($eventName, $data)
2929
{
30-
//$events = $this->getRegisteredData('events') ? $this->getRegisteredData('events') : [];
3130
if (!isset($this->events[$eventName])) {
3231
$this->events[$eventName] = ['event'=>$eventName,
3332
'nbr'=>0,
@@ -47,4 +46,4 @@ public function pullData()
4746
{
4847
return $this->events;
4948
}
50-
}
49+
}

0 commit comments

Comments
 (0)