From 3a41b5d7ecdc4c35cbe019eaeaaf14fda8d8c755 Mon Sep 17 00:00:00 2001 From: guvra Date: Mon, 6 Feb 2023 10:16:08 +0100 Subject: [PATCH] Disable the output of the toolbar ajax query when the toolbar is disabled --- Block/Toolbar.php | 4 +- Block/Toolbars.php | 23 ++--------- Block/Zone/AbstractZone.php | 4 +- CHANGELOG.md | 5 +++ Controller/Index/Index.php | 10 ++++- Helper/Data.php | 14 ------- Observer/AddToolbar.php | 2 +- Observer/AddZones.php | 24 ++++++------ ViewModel/Loader.php | 10 ++--- ViewModel/Toolbar.php | 39 +++++++++++++++++++ .../smile_debug_toolbar_index_index.xml | 6 ++- view/base/templates/toolbars.phtml | 39 +++++++++++-------- 12 files changed, 106 insertions(+), 74 deletions(-) create mode 100644 ViewModel/Toolbar.php diff --git a/Block/Toolbar.php b/Block/Toolbar.php index 62c7256..51c5b3f 100644 --- a/Block/Toolbar.php +++ b/Block/Toolbar.php @@ -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; @@ -17,7 +17,7 @@ /** * Main Debug Toolbar Block */ -class Toolbar extends MagentoTemplateBlock +class Toolbar extends Template { protected DataHelper $dataHelper; protected SummaryFactory $blockSummaryFactory; diff --git a/Block/Toolbars.php b/Block/Toolbars.php index b08200d..a6e39d0 100644 --- a/Block/Toolbars.php +++ b/Block/Toolbars.php @@ -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 */ diff --git a/Block/Zone/AbstractZone.php b/Block/Zone/AbstractZone.php index 3f85cda..5c55bef 100644 --- a/Block/Zone/AbstractZone.php +++ b/Block/Zone/AbstractZone.php @@ -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; @@ -14,7 +14,7 @@ * * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ -abstract class AbstractZone extends MagentoTemplateBlock +abstract class AbstractZone extends Template { protected DataHelper $dataHelper; protected FormatterFactory $formatterFactory; diff --git a/CHANGELOG.md b/CHANGELOG.md index 392c3f6..0a4729e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Controller/Index/Index.php b/Controller/Index/Index.php index 646b38f..41d3372 100644 --- a/Controller/Index/Index.php +++ b/Controller/Index/Index.php @@ -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 */ diff --git a/Helper/Data.php b/Helper/Data.php index 8dc161d..9d25288 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -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; @@ -159,9 +158,6 @@ public function getNewTableId(): string /** * Get the toolbar storage folder. - * - * @throws FileSystemException - * @throws RuntimeException */ public function getToolbarFolder(): string { @@ -176,9 +172,6 @@ public function getToolbarFolder(): string /** * Save the current toolbar. - * - * @throws FileSystemException - * @throws RuntimeException */ public function saveToolbar(Toolbar $toolbarBlock): void { @@ -189,9 +182,6 @@ public function saveToolbar(Toolbar $toolbarBlock): void /** * Clean the old toolbars. - * - * @throws FileSystemException - * @throws RuntimeException */ public function cleanOldToolbars(int $nbToKeep): void { @@ -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 { @@ -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 { diff --git a/Observer/AddToolbar.php b/Observer/AddToolbar.php index c3b22d3..7801e7f 100644 --- a/Observer/AddToolbar.php +++ b/Observer/AddToolbar.php @@ -20,7 +20,7 @@ use Smile\DebugToolbar\Helper\Profiler as ProfilerHelper; /** - * Display the toolbar. + * Save the toolbar data to a file. * * @SuppressWarnings(PMD.CouplingBetweenObjects) */ diff --git a/Observer/AddZones.php b/Observer/AddZones.php index 3308131..50090e3 100644 --- a/Observer/AddZones.php +++ b/Observer/AddZones.php @@ -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, @@ -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, + ]; } /** diff --git a/ViewModel/Loader.php b/ViewModel/Loader.php index 9c8f641..331b928 100644 --- a/ViewModel/Loader.php +++ b/ViewModel/Loader.php @@ -5,15 +5,15 @@ 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; } /** @@ -21,6 +21,6 @@ public function __construct(Config $config) */ public function isToolbarEnabled(): bool { - return $this->config->isEnabled(); + return $this->configHelper->isEnabled(); } } diff --git a/ViewModel/Toolbar.php b/ViewModel/Toolbar.php new file mode 100644 index 0000000..e9e97be --- /dev/null +++ b/ViewModel/Toolbar.php @@ -0,0 +1,39 @@ +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(); + } +} diff --git a/view/base/layout/smile_debug_toolbar_index_index.xml b/view/base/layout/smile_debug_toolbar_index_index.xml index ed25c07..1b07e5d 100644 --- a/view/base/layout/smile_debug_toolbar_index_index.xml +++ b/view/base/layout/smile_debug_toolbar_index_index.xml @@ -1,6 +1,10 @@ - + + + Smile\DebugToolbar\ViewModel\Toolbar + + diff --git a/view/base/templates/toolbars.phtml b/view/base/templates/toolbars.phtml index dbe6897..c0402a0 100644 --- a/view/base/templates/toolbars.phtml +++ b/view/base/templates/toolbars.phtml @@ -1,21 +1,28 @@ getToolbarList(); -foreach ($toolbars as $toolbarContent): ?> - - -
-
-
-
-

My Modal Title

+/** @var Template $block */ +/** @var Toolbar $viewModel */ +$viewModel = $block->getData('viewModel'); +$toolbars = $viewModel->getToolbarList(); +?> + +isToolbarEnabled()): ?> + + + +
+
+
+
+

My Modal Title

+
+
My Modal Content
-
My Modal Content
-
- + +