Skip to content

Commit

Permalink
Merge pull request #76 from vpietri/fix_dumper_ajax
Browse files Browse the repository at this point in the history
Catch dumper in ajax calls
  • Loading branch information
vpietri authored Jun 13, 2024
2 parents bbb5cbd + e1f6493 commit 630325c
Show file tree
Hide file tree
Showing 18 changed files with 291 additions and 139 deletions.
7 changes: 7 additions & 0 deletions Block/Tab/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ADM\QuickDevBar\Block\Tab;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;

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

public function getQdbConfig($key, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)

{
return $this->helper->getQdbConfig($key, $scopeType, $scopeCode);
}

}
8 changes: 0 additions & 8 deletions Block/Tab/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,8 @@ public function getTabBlocks()
if ($this->_mainTabs === null) {
$this->_mainTabs=[];
foreach ($this->getLayout()->getChildBlocks($this->getNameInLayout()) as $alias => $block) {
if(!$block->getTitleBadge() && $block->getVisibleOnContent()) {
continue;
}


$this->_mainTabs[$alias]=$block;
}



}

return $this->_mainTabs;
Expand Down
7 changes: 0 additions & 7 deletions Block/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ public function getAppearance()
return $this->_qdnHelper->defaultAppearance();
}



public function getAjaxUrl()
{
return $this->getUrl('quickdevbar/index/ajax');
}

public function isAjaxLoading()
{
return $this->_qdnHelper->isAjaxLoading();
Expand Down
24 changes: 22 additions & 2 deletions Controller/Tab/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@

class Ajax extends \ADM\QuickDevBar\Controller\Index
{

protected \ADM\QuickDevBar\Helper\Register $qdbHelperRegister;

public function __construct(\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\Framework\View\LayoutFactory $layoutFactory,
\ADM\QuickDevBar\Helper\Data $qdbHelper,
\ADM\QuickDevBar\Helper\Register $qdbHelperRegister
)
{
parent::__construct($context, $qdbHelper, $resultRawFactory, $layoutFactory);
$this->qdbHelperRegister = $qdbHelperRegister;
}


/**
*
* @return \Magento\Backend\Model\View\Result\Page
Expand All @@ -14,8 +29,13 @@ public function execute()
try {
$this->_view->loadLayout('quickdevbar');

if ($this->_view->getLayout()->getBlock($blockName)) {
$output = $this->_view->getLayout()->getBlock($blockName)->toHtml();
$block = $this->_view->getLayout()->getBlock($blockName);
if ($block) {
if($block->getNeedLoadData()) {
$this->qdbHelperRegister->loadDataFromFile(true);
}
$block->setIsUpdateCall(true);
$output = $block->toHtml();
} else {
$output = 'Cannot found block: '. $blockName;
}
Expand Down
66 changes: 47 additions & 19 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,10 @@ public function getModuleVersion($moduleName)
return !empty($moduleInfo['setup_version']) ? $moduleInfo['setup_version'] : '???';
}

protected function getWrapperFilename($ajax = false)
protected function getWrapperBaseFilename($ajax = false)
{
$sessionId = $this->session->getSessionId();

$fileName = 'qdb_register_'.$sessionId.'.json';
if($ajax) {
$fileName = $this->_getRequest()->isAjax()
? 'qdb_ajax_register_'.$sessionId.'_'.time().'.json'
: 'qdb_unknown_register_'.$sessionId.'_'.time().'.json';
}

return $this->getQdbTempDir() . $fileName;
return 'qdb_register_' . (!$ajax ? 'std' : 'xhr') . '_' . $sessionId;
}


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

public function getWrapperContent($ajax = false)
{
//Clean old files
/** @var \SplFileInfo $fileInfo */
//Clean old files
// /** @var \SplFileInfo $fileInfo */
// foreach (new \DirectoryIterator($this->getQdbTempDir()) as $fileInfo) {
// if($fileInfo->isFile() && time() - $fileInfo->getMTime() > 20) {
// //TODO: unlink only files starting with 'qdb_register_' . $sessionId
// unlink($fileInfo->getPathname());
// }
// }

$wrapperFiles = [];
$filename = $this->getWrapperBaseFilename($ajax);
foreach (new \DirectoryIterator($this->getQdbTempDir()) as $fileInfo) {
if($fileInfo->isFile() && time() - $fileInfo->getMTime() > 20) {
unlink($fileInfo->getPathname());
if($fileInfo->isFile() && strpos($fileInfo->getFilename(), $filename)===0) {
$wrapperFiles[] = $fileInfo->getPathname();
}
}

$filename = $this->getWrapperFilename($ajax);
if(!file_exists($filename)) {
throw new LocalizedException(__('No file for wrapper'));
if(empty($wrapperFiles)) {
throw new LocalizedException(__('No files for wrapper'));
}

$serializer = new \Magento\Framework\Serialize\Serializer\Json();

$content = [];
foreach ($wrapperFiles as $wrapperContent) {
$jsonContent = file_get_contents($wrapperContent);
if($jsonContent) {
foreach ($serializer->unserialize($jsonContent) as $contentKey => $contentValue) {
$content[$contentKey] = empty($content[$contentKey]) ? $contentValue : array_merge($content[$contentKey], $contentValue);
}
}
//TODO: remove foreach
break;
}

$content = file_get_contents($filename);
if(empty($content)) {
throw new LocalizedException(__('No data registered'));
}

/** @var \SplFileInfo $fileInfo */
foreach (new \DirectoryIterator($this->getQdbTempDir()) as $fileInfo) {
if($fileInfo->isFile()) {
//TODO: unlink only files starting with 'qdb_register_' . $sessionId
unlink($fileInfo->getPathname());
}
}

return $content;
}


public function setWrapperContent($content, $ajax = false)
{
file_put_contents($this->getWrapperFilename($ajax), $content);
$filename = $this->getWrapperBaseFilename($ajax);
if($ajax) {
$filename .= time();
}
file_put_contents($this->getQdbTempDir() . $filename . '.json', $content);
}

/**
* TODO: To removed
* Asymmetric behavior frontend/admin is no more necessary
*
* @return bool
* @throws LocalizedException
*/
Expand Down
26 changes: 11 additions & 15 deletions Helper/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public function __construct(Context $context,
$this->objectFactory = $objectFactory;
$this->qdbHelper = $qdbHelper;
$this->services = $services;
if($this->qdbHelper->isToolbarAccessAllowed() && $this->qdbHelper->isAjaxLoading()) {
register_shutdown_function([$this, 'dumpToFile']);
}
}


Expand All @@ -47,36 +44,35 @@ public function __construct(Context $context,
*/
public function dumpToFile()
{
$isAjax = $this->_getRequest()->isAjax();
if($this->_getRequest() && $this->_getRequest()->getModuleName()=='quickdevbar') {
return false;
}

foreach ($this->services as $serviceKey => $serviceObj) {
//TODO: Filter keys on $isAjax
if($isAjax && $serviceKey!='dumps') {
continue;
}

$this->setRegisteredData($serviceKey, $serviceObj->pullData());
}
$content = $this->registeredData->convertToJson();
$this->qdbHelper->setWrapperContent($content);

$this->qdbHelper->setWrapperContent($content, $isAjax);
}

/**
*
*/
public function loadDataFromFile()
public function loadDataFromFile($ajax = false)
{
$wrapperContent = $this->qdbHelper->getWrapperContent();
$this->setRegisteredJsonData($wrapperContent);
$wrapperContent = $this->qdbHelper->getWrapperContent($ajax);
$this->setRegisteredData($wrapperContent);
$this->pullDataFromService = false;
}


/**
* @param $data
*/
public function setRegisteredJsonData($data)
{
$serializer = new \Magento\Framework\Serialize\Serializer\Json();
$this->setRegisteredData($serializer->unserialize($data));
}

/**
* @param null $key
Expand Down
20 changes: 20 additions & 0 deletions Model/Config/Source/DumperHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ADM\QuickDevBar\Model\Config\Source;

class DumperHandler implements \Magento\Framework\Option\ArrayInterface
{
/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
return [
['value' => 0, 'label' => __('No')],
['value' => 1, 'label' => __('Current page')],
['value' => 2, 'label' => __('Current page and ajax calls')]
];
}
}
45 changes: 39 additions & 6 deletions Plugin/Framework/App/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,67 @@

namespace ADM\QuickDevBar\Plugin\Framework\App;

use ADM\QuickDevBar\Helper\Data;
use ADM\QuickDevBar\Helper\Register;
use ADM\QuickDevBar\Service\Dumper;
use Magento\Framework\App\RequestInterface;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\VarDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;


class FrontController
{
private $request;

private $qdbHelper;

private $dumper;

private Register $register;

/**
* @param \ADM\QuickDevBar\Service\Dumper $dumper
* @param RequestInterface $request
* @param Data $qdbHelper
* @param Register $register
* @param Dumper $dumper
*/
public function __construct(\ADM\QuickDevBar\Helper\Data $qdbHelper,
\ADM\QuickDevBar\Service\Dumper $dumper
public function __construct(RequestInterface $request,
Data $qdbHelper,
Register $register,
Dumper $dumper
)
{
$this->request = $request;
$this->qdbHelper = $qdbHelper;
$this->register = $register;
$this->dumper = $dumper;
}

/**
* Be careful, two usage:
* - dumpToFile
* - VarDumper::setHandler
*
* @param \Magento\Framework\AppInterface $subject
* @return void
*/
public function beforeDispatch(\Magento\Framework\App\FrontControllerInterface $subject)
{
if($this->qdbHelper->getQdbConfig('handle_vardumper')) {


if(!$this->qdbHelper->isToolbarAccessAllowed()) {
return;
}

if($this->qdbHelper->isAjaxLoading()) {
register_shutdown_function([$this->register, 'dumpToFile']);
}

if($enabledHandler = $this->qdbHelper->getQdbConfig('handle_vardumper')) {
if($this->request->isAjax() && $enabledHandler<2) {
return;
}
$prevHandler = VarDumper::setHandler($this->dumperHandler(...));
}
}
Expand All @@ -47,11 +79,12 @@ protected function dumperHandler($var)
$dumper->setTheme('dark');
$dumpBt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[2];

$ajaxReq = $this->request->isAjax() ? $this->request->getActionName() : null;

$output = $dumpBt['function'] != 'dd';
$dumpOutput = $dumper->dump($cloner->cloneVar($var), $output);
if($output) {
$this->dumper->addDump($dumpOutput, $dumpBt);
$this->dumper->addDump($dumpOutput, $dumpBt, $ajaxReq);
}

}
}
4 changes: 2 additions & 2 deletions Service/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function pullData()
return $this->dumps;
}

public function addDump(string $output, array $bt)
public function addDump(string $output, array $bt, $ajaxReq = null)
{
$this->dumps[] = ['dump'=>$output, 'bt'=> $bt];
$this->dumps[] = ['dump'=>$output, 'bt'=> $bt, 'ajaxReq'=> $ajaxReq];
}
}
3 changes: 1 addition & 2 deletions Service/Event/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function __construct(array $services = [])
*/
public function addEvent($eventName, $data)
{
//$events = $this->getRegisteredData('events') ? $this->getRegisteredData('events') : [];
if (!isset($this->events[$eventName])) {
$this->events[$eventName] = ['event'=>$eventName,
'nbr'=>0,
Expand All @@ -47,4 +46,4 @@ public function pullData()
{
return $this->events;
}
}
}
12 changes: 12 additions & 0 deletions doc/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
Changelog: Quick Developer Toolbar for Magento2
====================================
0.2.3
* Catch VarDumper in ajax calls
* Code refactoring

0.2.2
* Use config to handle VarDumper
* Move handler for VarDumper
* Do not catch dd()

0.2.1
* Remove hard log /tmp/debug.log

0.2.0
* Full compatibility with FPC
* Full code refactoring
Expand Down
Loading

0 comments on commit 630325c

Please sign in to comment.