Favicon.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Favicon;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * Favicon implementation
  10. */
  11. class Favicon implements \Magento\Framework\View\Page\FaviconInterface
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $faviconFile;
  17. /**
  18. * @var \Magento\Store\Model\StoreManagerInterface
  19. */
  20. protected $storeManager;
  21. /**
  22. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  23. */
  24. protected $scopeConfig;
  25. /**
  26. * @var \Magento\MediaStorage\Helper\File\Storage\Database
  27. */
  28. protected $fileStorageDatabase;
  29. /**
  30. * @var \Magento\Framework\Filesystem\Directory\ReadInterface
  31. */
  32. protected $mediaDirectory;
  33. /**
  34. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  35. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  36. * @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase
  37. * @param \Magento\Framework\Filesystem $filesystem
  38. */
  39. public function __construct(
  40. \Magento\Store\Model\StoreManagerInterface $storeManager,
  41. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  42. \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase,
  43. \Magento\Framework\Filesystem $filesystem
  44. ) {
  45. $this->storeManager = $storeManager;
  46. $this->scopeConfig = $scopeConfig;
  47. $this->fileStorageDatabase = $fileStorageDatabase;
  48. $this->mediaDirectory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
  49. }
  50. /**
  51. * @return string
  52. */
  53. public function getFaviconFile()
  54. {
  55. if (null === $this->faviconFile) {
  56. $this->faviconFile = $this->prepareFaviconFile();
  57. }
  58. return $this->faviconFile;
  59. }
  60. /**
  61. * @return string
  62. */
  63. public function getDefaultFavicon()
  64. {
  65. return 'Magento_Theme::favicon.ico';
  66. }
  67. /**
  68. * @return string
  69. */
  70. protected function prepareFaviconFile()
  71. {
  72. $folderName = \Magento\Config\Model\Config\Backend\Image\Favicon::UPLOAD_DIR;
  73. $scopeConfig = $this->scopeConfig->getValue(
  74. 'design/head/shortcut_icon',
  75. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  76. );
  77. $path = $folderName . '/' . $scopeConfig;
  78. $faviconUrl = $this->storeManager->getStore()
  79. ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $path;
  80. if ($scopeConfig !== null && $this->checkIsFile($path)) {
  81. return $faviconUrl;
  82. }
  83. return false;
  84. }
  85. /**
  86. * If DB file storage is on - find there, otherwise - just file_exists
  87. *
  88. * @param string $filename relative file path
  89. * @return bool
  90. */
  91. protected function checkIsFile($filename)
  92. {
  93. if ($this->fileStorageDatabase->checkDbUsage() && !$this->mediaDirectory->isFile($filename)) {
  94. $this->fileStorageDatabase->saveFileToFilesystem($filename);
  95. }
  96. return $this->mediaDirectory->isFile($filename);
  97. }
  98. }