AbstractHelper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Helper;
  7. /**
  8. * Abstract helper
  9. *
  10. * @SuppressWarnings(PHPMD.NumberOfChildren)
  11. */
  12. abstract class AbstractHelper
  13. {
  14. /**
  15. * Helper module name
  16. *
  17. * @var string
  18. */
  19. protected $_moduleName;
  20. /**
  21. * Request object
  22. *
  23. * @var \Magento\Framework\App\RequestInterface
  24. */
  25. protected $_request;
  26. /**
  27. * @var \Magento\Framework\Module\Manager
  28. */
  29. protected $_moduleManager;
  30. /**
  31. * @var \Psr\Log\LoggerInterface
  32. */
  33. protected $_logger;
  34. /**
  35. * @var \Magento\Framework\UrlInterface
  36. */
  37. protected $_urlBuilder;
  38. /**
  39. * @var \Magento\Framework\HTTP\Header
  40. */
  41. protected $_httpHeader;
  42. /**
  43. * Event manager
  44. *
  45. * @var \Magento\Framework\Event\ManagerInterface
  46. */
  47. protected $_eventManager;
  48. /**
  49. * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
  50. */
  51. protected $_remoteAddress;
  52. /**
  53. * @var \Magento\Framework\Url\EncoderInterface
  54. */
  55. protected $urlEncoder;
  56. /**
  57. * @var \Magento\Framework\Url\DecoderInterface
  58. */
  59. protected $urlDecoder;
  60. /**
  61. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  62. */
  63. protected $scopeConfig;
  64. /**
  65. * @var \Magento\Framework\Cache\ConfigInterface
  66. */
  67. protected $_cacheConfig;
  68. /**
  69. * @param Context $context
  70. */
  71. public function __construct(Context $context)
  72. {
  73. $this->_moduleManager = $context->getModuleManager();
  74. $this->_logger = $context->getLogger();
  75. $this->_request = $context->getRequest();
  76. $this->_urlBuilder = $context->getUrlBuilder();
  77. $this->_httpHeader = $context->getHttpHeader();
  78. $this->_eventManager = $context->getEventManager();
  79. $this->_remoteAddress = $context->getRemoteAddress();
  80. $this->_cacheConfig = $context->getCacheConfig();
  81. $this->urlEncoder = $context->getUrlEncoder();
  82. $this->urlDecoder = $context->getUrlDecoder();
  83. $this->scopeConfig = $context->getScopeConfig();
  84. }
  85. /**
  86. * Retrieve request object
  87. *
  88. * @return \Magento\Framework\App\RequestInterface
  89. */
  90. protected function _getRequest()
  91. {
  92. return $this->_request;
  93. }
  94. /**
  95. * Retrieve helper module name
  96. *
  97. * @return string
  98. */
  99. protected function _getModuleName()
  100. {
  101. if (!$this->_moduleName) {
  102. $class = get_class($this);
  103. $this->_moduleName = substr($class, 0, strpos($class, '\\Helper'));
  104. }
  105. return str_replace('\\', '_', $this->_moduleName);
  106. }
  107. /**
  108. * Check whether or not the module output is enabled in Configuration
  109. *
  110. * @param string $moduleName Full module name
  111. * @return boolean
  112. * use \Magento\Framework\Module\Manager::isOutputEnabled()
  113. */
  114. public function isModuleOutputEnabled($moduleName = null)
  115. {
  116. if ($moduleName === null) {
  117. $moduleName = $this->_getModuleName();
  118. }
  119. return $this->_moduleManager->isOutputEnabled($moduleName);
  120. }
  121. /**
  122. * Retrieve url
  123. *
  124. * @param string $route
  125. * @param array $params
  126. * @return string
  127. */
  128. protected function _getUrl($route, $params = [])
  129. {
  130. return $this->_urlBuilder->getUrl($route, $params);
  131. }
  132. }