Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Favicon.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
18  protected $faviconFile;
19 
23  protected $storeManager;
24 
28  protected $scopeConfig;
29 
34 
38  protected $mediaDirectory;
39 
46  public function __construct(
49  \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase,
50  \Magento\Framework\Filesystem $filesystem
51  ) {
52  $this->storeManager = $storeManager;
53  $this->scopeConfig = $scopeConfig;
54  $this->fileStorageDatabase = $fileStorageDatabase;
55  $this->mediaDirectory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
56  }
57 
61  public function getFaviconFile()
62  {
63  if (null === $this->faviconFile) {
64  $this->faviconFile = $this->prepareFaviconFile();
65  }
66  return $this->faviconFile;
67  }
68 
72  public function getDefaultFavicon()
73  {
74  return 'Magento_Theme::favicon.ico';
75  }
76 
80  protected function prepareFaviconFile()
81  {
82  $folderName = \Magento\Config\Model\Config\Backend\Image\Favicon::UPLOAD_DIR;
83  $scopeConfig = $this->scopeConfig->getValue(
84  'design/head/shortcut_icon',
86  );
87  $path = $folderName . '/' . $scopeConfig;
88  $faviconUrl = $this->storeManager->getStore()
89  ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $path;
90 
91  if ($scopeConfig !== null && $this->checkIsFile($path)) {
92  return $faviconUrl;
93  }
94 
95  return false;
96  }
97 
104  protected function checkIsFile($filename)
105  {
106  if ($this->fileStorageDatabase->checkDbUsage() && !$this->mediaDirectory->isFile($filename)) {
107  $this->fileStorageDatabase->saveFileToFilesystem($filename);
108  }
109  return $this->mediaDirectory->isFile($filename);
110  }
111 }
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase, \Magento\Framework\Filesystem $filesystem)
Definition: Favicon.php:46
$filesystem