DebugHints.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Plugin for the template engine factory that makes a decision of whether to activate debugging hints or not
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Developer\Model\TemplateEngine\Plugin;
  9. use Magento\Developer\Helper\Data as DevHelper;
  10. use Magento\Developer\Model\TemplateEngine\Decorator\DebugHintsFactory;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\View\TemplateEngineFactory;
  13. use Magento\Framework\View\TemplateEngineInterface;
  14. use Magento\Store\Model\ScopeInterface;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. use Magento\Framework\App\Request\Http;
  17. class DebugHints
  18. {
  19. /**
  20. * XPath of configuration of the debug block names
  21. */
  22. const XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS = 'dev/debug/template_hints_blocks';
  23. /**
  24. * @var ScopeConfigInterface
  25. */
  26. protected $scopeConfig;
  27. /**
  28. * @var StoreManagerInterface
  29. */
  30. protected $storeManager;
  31. /**
  32. * @var DevHelper
  33. */
  34. protected $devHelper;
  35. /**
  36. * @var DebugHintsFactory
  37. */
  38. protected $debugHintsFactory;
  39. /**
  40. * XPath of configuration of the debug hints
  41. *
  42. * Allowed values:
  43. * dev/debug/template_hints_storefront
  44. * dev/debug/template_hints_admin
  45. *
  46. * @var string
  47. */
  48. protected $debugHintsPath;
  49. /**
  50. * XPath of configuration of the debug hints show with parameter
  51. *
  52. * dev/debug/template_hints_storefront_show_with_parameter
  53. *
  54. * @var string
  55. */
  56. private $debugHintsWithParam;
  57. /**
  58. * XPath of configuration of the debug hints URL parameter
  59. *
  60. * dev/debug/template_hints_parameter_value
  61. *
  62. * @var string
  63. */
  64. private $debugHintsParameter;
  65. /**
  66. * @param ScopeConfigInterface $scopeConfig
  67. * @param StoreManagerInterface $storeManager
  68. * @param DevHelper $devHelper
  69. * @param DebugHintsFactory $debugHintsFactory
  70. * @param string $debugHintsPath
  71. * @param Http $http
  72. * @param string $debugHintsWithParam
  73. * @param string $debugHintsParameter
  74. */
  75. public function __construct(
  76. ScopeConfigInterface $scopeConfig,
  77. StoreManagerInterface $storeManager,
  78. DevHelper $devHelper,
  79. DebugHintsFactory $debugHintsFactory,
  80. $debugHintsPath,
  81. Http $http = null,
  82. $debugHintsWithParam = null,
  83. $debugHintsParameter = null
  84. ) {
  85. $this->scopeConfig = $scopeConfig;
  86. $this->storeManager = $storeManager;
  87. $this->devHelper = $devHelper;
  88. $this->debugHintsFactory = $debugHintsFactory;
  89. $this->debugHintsPath = $debugHintsPath;
  90. $this->http = $http ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
  91. \Magento\Framework\App\Request\Http::class
  92. );
  93. $this->debugHintsWithParam = $debugHintsWithParam;
  94. $this->debugHintsParameter = $debugHintsParameter;
  95. }
  96. /**
  97. * Wrap template engine instance with the debugging hints decorator, depending of the store configuration
  98. *
  99. * @param TemplateEngineFactory $subject
  100. * @param TemplateEngineInterface $invocationResult
  101. *
  102. * @return TemplateEngineInterface
  103. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  104. */
  105. public function afterCreate(
  106. TemplateEngineFactory $subject,
  107. TemplateEngineInterface $invocationResult
  108. ) {
  109. $storeCode = $this->storeManager->getStore()->getCode();
  110. if ($this->scopeConfig->getValue($this->debugHintsPath, ScopeInterface::SCOPE_STORE, $storeCode)
  111. && $this->devHelper->isDevAllowed()) {
  112. $debugHintsWithParam = $this->scopeConfig->getValue(
  113. $this->debugHintsWithParam,
  114. ScopeInterface::SCOPE_STORE,
  115. $storeCode
  116. );
  117. $debugHintsParameter = $this->scopeConfig->getValue(
  118. $this->debugHintsParameter,
  119. ScopeInterface::SCOPE_STORE,
  120. $storeCode
  121. );
  122. $debugHintsParameterInUrl = $this->http->getParam('templatehints');
  123. $showHints = false;
  124. if (!$debugHintsWithParam) {
  125. $showHints = true;
  126. }
  127. if ($debugHintsWithParam && $debugHintsParameter == $debugHintsParameterInUrl) {
  128. $showHints = true;
  129. }
  130. if ($showHints) {
  131. $showBlockHints = $this->scopeConfig->getValue(
  132. self::XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS,
  133. ScopeInterface::SCOPE_STORE,
  134. $storeCode
  135. );
  136. return $this->debugHintsFactory->create([
  137. 'subject' => $invocationResult,
  138. 'showBlockHints' => $showBlockHints,
  139. ]);
  140. }
  141. }
  142. return $invocationResult;
  143. }
  144. }