Skip to content

Commit

Permalink
Disable the output of the toolbar ajax query when the toolbar is disa…
Browse files Browse the repository at this point in the history
…bled
  • Loading branch information
guvra committed Feb 6, 2023
1 parent d2138eb commit 3a41b5d
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 74 deletions.
4 changes: 2 additions & 2 deletions Block/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Magento\Framework\App\Request\Http as MagentoRequest;
use Magento\Framework\DataObject;
use Magento\Framework\HTTP\PhpEnvironment\Response as MagentoResponse;
use Magento\Framework\View\Element\Template as MagentoTemplateBlock;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use RuntimeException;
use Smile\DebugToolbar\Block\Zone\Summary;
Expand All @@ -17,7 +17,7 @@
/**
* Main Debug Toolbar Block
*/
class Toolbar extends MagentoTemplateBlock
class Toolbar extends Template
{
protected DataHelper $dataHelper;
protected SummaryFactory $blockSummaryFactory;
Expand Down
23 changes: 3 additions & 20 deletions Block/Toolbars.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,21 @@

namespace Smile\DebugToolbar\Block;

use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\View\Element\Template as MagentoTemplateBlock;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Smile\DebugToolbar\Helper\Data as DataHelper;

/**
* Main Debug Toolbar Block
*/
class Toolbars extends MagentoTemplateBlock
class Toolbars extends Template
{
protected DataHelper $dataHelper;

public function __construct(Context $context, DataHelper $dataHelper, array $data = [])
public function __construct(Context $context, array $data = [])
{
parent::__construct($context, $data);
$this->dataHelper = $dataHelper;

$this->setData('cache_lifetime', 0);
$this->setTemplate('toolbars.phtml');
}

/**
* Return the list of the toolbars.
*
* @return string[]
* @throws FileSystemException
*/
public function getToolbarList(): array
{
return $this->dataHelper->getContentToolbars();
}

/**
* @inheritdoc
*/
Expand Down
4 changes: 2 additions & 2 deletions Block/Zone/AbstractZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Smile\DebugToolbar\Block\Zone;

use Magento\Framework\View\Element\Template as MagentoTemplateBlock;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Smile\DebugToolbar\Formatter\FormatterFactory;
use Smile\DebugToolbar\Helper\Data as DataHelper;
Expand All @@ -14,7 +14,7 @@
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
abstract class AbstractZone extends MagentoTemplateBlock
abstract class AbstractZone extends Template
{
protected DataHelper $dataHelper;
protected FormatterFactory $formatterFactory;
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## WIP

- Disable the output of the toolbar ajax query when the toolbar is disabled
- Remove use of AbstractAction class (deprecated)

## [6.1.0] - 2023-02-02
[6.1.0]: https://github.com/Smile-SA/magento2-module-debug-toolbar/compare/6.0.0...6.1.0

Expand Down
10 changes: 8 additions & 2 deletions Controller/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

namespace Smile\DebugToolbar\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;

class Index extends Action implements HttpGetActionInterface
class Index implements HttpGetActionInterface
{
protected ResultFactory $resultFactory;

public function __construct(ResultFactory $resultFactory)
{
$this->resultFactory = $resultFactory;
}

/**
* @inheritdoc
*/
Expand Down
14 changes: 0 additions & 14 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\PageCache\Model\Config as PageCacheConfig;
use RuntimeException;
Expand Down Expand Up @@ -159,9 +158,6 @@ public function getNewTableId(): string

/**
* Get the toolbar storage folder.
*
* @throws FileSystemException
* @throws RuntimeException
*/
public function getToolbarFolder(): string
{
Expand All @@ -176,9 +172,6 @@ public function getToolbarFolder(): string

/**
* Save the current toolbar.
*
* @throws FileSystemException
* @throws RuntimeException
*/
public function saveToolbar(Toolbar $toolbarBlock): void
{
Expand All @@ -189,9 +182,6 @@ public function saveToolbar(Toolbar $toolbarBlock): void

/**
* Clean the old toolbars.
*
* @throws FileSystemException
* @throws RuntimeException
*/
public function cleanOldToolbars(int $nbToKeep): void
{
Expand All @@ -213,8 +203,6 @@ public function cleanOldToolbars(int $nbToKeep): void
* Get the list of all the stored toolbars.
*
* @return string[]
* @throws FileSystemException
* @throws RuntimeException
*/
public function getListToolbars(): array
{
Expand All @@ -237,8 +225,6 @@ public function getListToolbars(): array
* Get the content of all the stored toolbars.
*
* @return string[]
* @throws FileSystemException
* @throws RuntimeException
*/
public function getContentToolbars(): array
{
Expand Down
2 changes: 1 addition & 1 deletion Observer/AddToolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Smile\DebugToolbar\Helper\Profiler as ProfilerHelper;

/**
* Display the toolbar.
* Save the toolbar data to a file.
*
* @SuppressWarnings(PMD.CouplingBetweenObjects)
*/
Expand Down
24 changes: 13 additions & 11 deletions Observer/AddZones.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
use Smile\DebugToolbar\Block\Zone\Summary;

/**
* Add the zones.
* Add zone blocks to the toolbar.
*/
class AddZones implements ObserverInterface
{
protected array $blockFactories = [];
protected array $blockFactories;

public function __construct(
CacheFactory $cacheBlockFactory,
Expand All @@ -38,15 +38,17 @@ public function __construct(
RequestFactory $requestBlockFactory,
ResponseFactory $responseBlockFactory
) {
$this->blockFactories[] = $genericBlockFactory;
$this->blockFactories[] = $requestBlockFactory;
$this->blockFactories[] = $responseBlockFactory;
$this->blockFactories[] = $layoutBlockFactory;
$this->blockFactories[] = $mysqlBlockFactory;
$this->blockFactories[] = $cacheBlockFactory;
$this->blockFactories[] = $profilerBlockFactory;
$this->blockFactories[] = $observerBlockFactory;
$this->blockFactories[] = $preferenceBlockFactory;
$this->blockFactories = [
$genericBlockFactory,
$requestBlockFactory,
$responseBlockFactory,
$layoutBlockFactory,
$mysqlBlockFactory,
$cacheBlockFactory,
$profilerBlockFactory,
$observerBlockFactory,
$preferenceBlockFactory,
];
}

/**
Expand Down
10 changes: 5 additions & 5 deletions ViewModel/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
namespace Smile\DebugToolbar\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Smile\DebugToolbar\Helper\Config;
use Smile\DebugToolbar\Helper\Config as ConfigHelper;

class Loader implements ArgumentInterface
{
protected Config $config;
protected ConfigHelper $configHelper;

public function __construct(Config $config)
public function __construct(ConfigHelper $configHelper)
{
$this->config = $config;
$this->configHelper = $configHelper;
}

/**
* Check whether the module is enabled.
*/
public function isToolbarEnabled(): bool
{
return $this->config->isEnabled();
return $this->configHelper->isEnabled();
}
}
39 changes: 39 additions & 0 deletions ViewModel/Toolbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Smile\DebugToolbar\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Smile\DebugToolbar\Helper\Config as ConfigHelper;
use Smile\DebugToolbar\Helper\Data as DataHelper;

class Toolbar implements ArgumentInterface
{
protected DataHelper $dataHelper;
protected ConfigHelper $configHelper;

public function __construct(DataHelper $dataHelper, ConfigHelper $configHelper)
{
$this->dataHelper = $dataHelper;
$this->configHelper = $configHelper;
}

/**
* Check whether the module is enabled.
*/
public function isToolbarEnabled(): bool
{
return $this->configHelper->isEnabled();
}

/**
* Get the toolbars list.
*
* @return string[]
*/
public function getToolbarList(): array
{
return $this->dataHelper->getContentToolbars();
}
}
6 changes: 5 additions & 1 deletion view/base/layout/smile_debug_toolbar_index_index.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<container name="root" label="Root">
<block class="Smile\DebugToolbar\Block\Toolbars" name="smile_debug_toolbar_toolbars" template="Smile_DebugToolbar::toolbars.phtml" cacheable="false"/>
<block class="Smile\DebugToolbar\Block\Toolbars" name="smile_debug_toolbar_toolbars" template="Smile_DebugToolbar::toolbars.phtml" cacheable="false">
<arguments>
<argument name="viewModel" xsi:type="object">Smile\DebugToolbar\ViewModel\Toolbar</argument>
</arguments>
</block>
</container>
</layout>
39 changes: 23 additions & 16 deletions view/base/templates/toolbars.phtml
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
<?php

use Smile\DebugToolbar\Block\Toolbars;
use Magento\Framework\View\Element\Template;
use Smile\DebugToolbar\ViewModel\Toolbar;

/** @var Toolbars $block */
$toolbars = $block->getToolbarList();
foreach ($toolbars as $toolbarContent): ?>
<?= /* @noEscape */ $toolbarContent . "\n" ?>
<?php endforeach ?>
<div class="smile-toolbar" id="st-modal-display">
<div class="st-main" id="st-modal-main" onclick="event.stopPropagation();">
<div class="st-title">
<div class="st-close" id="st-modal-close"></div>
<h1 id="st-modal-title">My Modal Title</h1>
/** @var Template $block */
/** @var Toolbar $viewModel */
$viewModel = $block->getData('viewModel');
$toolbars = $viewModel->getToolbarList();
?>

<?php if ($viewModel->isToolbarEnabled()): ?>
<?php foreach ($toolbars as $toolbarContent): ?>
<?= /* @noEscape */ $toolbarContent . "\n" ?>
<?php endforeach ?>
<div class="smile-toolbar" id="st-modal-display">
<div class="st-main" id="st-modal-main" onclick="event.stopPropagation();">
<div class="st-title">
<div class="st-close" id="st-modal-close"></div>
<h1 id="st-modal-title">My Modal Title</h1>
</div>
<div id="st-modal-content">My Modal Content</div>
</div>
<div id="st-modal-content">My Modal Content</div>
</div>
</div>
<script>
smileToolbarInit();
</script>
<script>
smileToolbarInit();
</script>
<?php endif ?>

0 comments on commit 3a41b5d

Please sign in to comment.