Placeholder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\View\Asset;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\View\Asset\ContextInterface;
  9. use Magento\Framework\View\Asset\File\NotFoundException;
  10. use Magento\Framework\View\Asset\LocalInterface;
  11. use Magento\Framework\View\Asset\Repository;
  12. /**
  13. * A locally available image placeholder file asset that can be referred with a file type
  14. */
  15. class Placeholder implements LocalInterface
  16. {
  17. /**
  18. * Type of placeholder
  19. *
  20. * @var string
  21. */
  22. private $type;
  23. /**
  24. * Filevpath of placeholder
  25. *
  26. * @var string
  27. */
  28. private $filePath;
  29. /**
  30. * @var string
  31. */
  32. private $contentType = 'image';
  33. /**
  34. * @var ContextInterface
  35. */
  36. private $context;
  37. /**
  38. * @var Repository
  39. */
  40. private $assetRepo;
  41. /**
  42. * Core store config
  43. *
  44. * @var ScopeConfigInterface
  45. */
  46. private $scopeConfig;
  47. /**
  48. * Placeholder constructor.
  49. *
  50. * @param ContextInterface $context
  51. * @param ScopeConfigInterface $scopeConfig
  52. * @param Repository $assetRepo
  53. * @param string $type
  54. */
  55. public function __construct(
  56. ContextInterface $context,
  57. ScopeConfigInterface $scopeConfig,
  58. Repository $assetRepo,
  59. $type
  60. ) {
  61. $this->context = $context;
  62. $this->scopeConfig = $scopeConfig;
  63. $this->assetRepo = $assetRepo;
  64. $this->type = $type;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getUrl()
  70. {
  71. if ($this->getFilePath() !== null) {
  72. $result = $this->context->getBaseUrl() . '/' . $this->getModule() . '/' . $this->getFilePath();
  73. } else {
  74. $result = $this->assetRepo->getUrl("Magento_Catalog::images/product/placeholder/{$this->type}.jpg");
  75. }
  76. return $result;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getContentType()
  82. {
  83. return $this->contentType;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getPath()
  89. {
  90. if ($this->getFilePath() !== null) {
  91. $result = $this->getContext()->getPath()
  92. . DIRECTORY_SEPARATOR . $this->getModule()
  93. . DIRECTORY_SEPARATOR . $this->getFilePath();
  94. } else {
  95. $defaultPlaceholder = $this->assetRepo->createAsset(
  96. "Magento_Catalog::images/product/placeholder/{$this->type}.jpg"
  97. );
  98. try {
  99. $result = $defaultPlaceholder->getSourceFile();
  100. } catch (NotFoundException $e) {
  101. $result = null;
  102. }
  103. }
  104. return $result;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getSourceFile()
  110. {
  111. return $this->getPath();
  112. }
  113. /**
  114. * Get source content type
  115. *
  116. * @return string
  117. */
  118. public function getSourceContentType()
  119. {
  120. return $this->contentType;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getContent()
  126. {
  127. return null;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function getFilePath()
  133. {
  134. if ($this->filePath !== null) {
  135. return $this->filePath;
  136. }
  137. // check if placeholder defined in config
  138. $isConfigPlaceholder = $this->scopeConfig->getValue(
  139. "catalog/placeholder/{$this->type}_placeholder",
  140. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  141. );
  142. $this->filePath = $isConfigPlaceholder;
  143. return $this->filePath;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. * @return ContextInterface
  148. */
  149. public function getContext()
  150. {
  151. return $this->context;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getModule()
  157. {
  158. return 'placeholder';
  159. }
  160. }