Logo.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Block\Html\Header;
  7. /**
  8. * Logo page header block
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Logo extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Current template name
  17. *
  18. * @var string
  19. */
  20. protected $_template = 'Magento_Theme::html/header/logo.phtml';
  21. /**
  22. * @var \Magento\MediaStorage\Helper\File\Storage\Database
  23. */
  24. protected $_fileStorageHelper;
  25. /**
  26. * @param \Magento\Framework\View\Element\Template\Context $context
  27. * @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageHelper
  28. * @param array $data
  29. */
  30. public function __construct(
  31. \Magento\Framework\View\Element\Template\Context $context,
  32. \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageHelper,
  33. array $data = []
  34. ) {
  35. $this->_fileStorageHelper = $fileStorageHelper;
  36. parent::__construct($context, $data);
  37. }
  38. /**
  39. * Check if current url is url for home page
  40. *
  41. * @deprecated 101.0.1 This function is no longer used. It was previously used by
  42. * Magento/Theme/view/frontend/templates/html/header/logo.phtml
  43. * to check if the logo should be clickable on the homepage.
  44. *
  45. * @return bool
  46. */
  47. public function isHomePage()
  48. {
  49. $currentUrl = $this->getUrl('', ['_current' => true]);
  50. $urlRewrite = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
  51. return $currentUrl == $urlRewrite;
  52. }
  53. /**
  54. * Get logo image URL
  55. *
  56. * @return string
  57. */
  58. public function getLogoSrc()
  59. {
  60. if (empty($this->_data['logo_src'])) {
  61. $this->_data['logo_src'] = $this->_getLogoUrl();
  62. }
  63. return $this->_data['logo_src'];
  64. }
  65. /**
  66. * Retrieve logo text
  67. *
  68. * @return string
  69. */
  70. public function getLogoAlt()
  71. {
  72. if (empty($this->_data['logo_alt'])) {
  73. $this->_data['logo_alt'] = $this->_scopeConfig->getValue(
  74. 'design/header/logo_alt',
  75. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  76. );
  77. }
  78. return $this->_data['logo_alt'];
  79. }
  80. /**
  81. * Retrieve logo width
  82. *
  83. * @return int
  84. */
  85. public function getLogoWidth()
  86. {
  87. if (empty($this->_data['logo_width'])) {
  88. $this->_data['logo_width'] = $this->_scopeConfig->getValue(
  89. 'design/header/logo_width',
  90. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  91. );
  92. }
  93. return (int)$this->_data['logo_width'] ? : (int)$this->getLogoImgWidth();
  94. }
  95. /**
  96. * Retrieve logo height
  97. *
  98. * @return int
  99. */
  100. public function getLogoHeight()
  101. {
  102. if (empty($this->_data['logo_height'])) {
  103. $this->_data['logo_height'] = $this->_scopeConfig->getValue(
  104. 'design/header/logo_height',
  105. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  106. );
  107. }
  108. return (int)$this->_data['logo_height'] ? : (int)$this->getLogoImgHeight();
  109. }
  110. /**
  111. * Retrieve logo image URL
  112. *
  113. * @return string
  114. */
  115. protected function _getLogoUrl()
  116. {
  117. $folderName = \Magento\Config\Model\Config\Backend\Image\Logo::UPLOAD_DIR;
  118. $storeLogoPath = $this->_scopeConfig->getValue(
  119. 'design/header/logo_src',
  120. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  121. );
  122. $path = $folderName . '/' . $storeLogoPath;
  123. $logoUrl = $this->_urlBuilder
  124. ->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]) . $path;
  125. if ($storeLogoPath !== null && $this->_isFile($path)) {
  126. $url = $logoUrl;
  127. } elseif ($this->getLogoFile()) {
  128. $url = $this->getViewFileUrl($this->getLogoFile());
  129. } else {
  130. $url = $this->getViewFileUrl('images/logo.svg');
  131. }
  132. return $url;
  133. }
  134. /**
  135. * If DB file storage is on - find there, otherwise - just file_exists
  136. *
  137. * @param string $filename relative path
  138. * @return bool
  139. */
  140. protected function _isFile($filename)
  141. {
  142. if ($this->_fileStorageHelper->checkDbUsage() && !$this->getMediaDirectory()->isFile($filename)) {
  143. $this->_fileStorageHelper->saveFileToFilesystem($filename);
  144. }
  145. return $this->getMediaDirectory()->isFile($filename);
  146. }
  147. }