File.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Asset;
  7. /**
  8. * A locally available static view file asset that can be referred with a file path
  9. *
  10. * This class is a value object with lazy loading of some of its data (content, physical file path)
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class File implements MergeableInterface
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $filePath;
  21. /**
  22. * @var string
  23. */
  24. protected $module;
  25. /**
  26. * @var string
  27. */
  28. protected $contentType;
  29. /**
  30. * @var ContextInterface
  31. */
  32. protected $context;
  33. /**
  34. * @var Source
  35. */
  36. protected $source;
  37. /**
  38. * @var string|bool
  39. */
  40. private $resolvedFile;
  41. /**
  42. * @var Minification
  43. */
  44. private $minification;
  45. /**
  46. * @var string
  47. */
  48. private $sourceContentType;
  49. /**
  50. * @param Source $source
  51. * @param ContextInterface $context
  52. * @param string $filePath
  53. * @param string $module
  54. * @param string $contentType
  55. * @param Minification $minification
  56. */
  57. public function __construct(
  58. Source $source,
  59. ContextInterface $context,
  60. $filePath,
  61. $module,
  62. $contentType,
  63. Minification $minification
  64. ) {
  65. $this->source = $source;
  66. $this->context = $context;
  67. $this->filePath = $filePath;
  68. $this->module = $module;
  69. $this->contentType = $contentType;
  70. $this->minification = $minification;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getUrl()
  76. {
  77. return $this->context->getBaseUrl() . $this->getPath();
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getSourceUrl()
  83. {
  84. return $this->context->getBaseUrl() . $this->getRelativeSourceFilePath();
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getContentType()
  90. {
  91. return $this->contentType;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getPath()
  97. {
  98. $result = '';
  99. $result = $this->join($result, $this->context->getPath());
  100. $result = $this->join($result, $this->module);
  101. $result = $this->join($result, $this->filePath);
  102. $result = $this->minification->addMinifiedSign($result);
  103. return $result;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function getRelativeSourceFilePath()
  109. {
  110. $path = $this->filePath;
  111. $sourcePath = $this->source->findSource($this);
  112. if ($sourcePath) {
  113. $origExt = pathinfo($path, PATHINFO_EXTENSION);
  114. $ext = pathinfo($sourcePath, PATHINFO_EXTENSION);
  115. $path = str_replace('.' . $origExt, '.' . $ext, $this->filePath);
  116. }
  117. $result = '';
  118. $result = $this->join($result, $this->context->getPath());
  119. $result = $this->join($result, $this->module);
  120. $result = $this->join($result, $path);
  121. return $result;
  122. }
  123. /**
  124. * Subroutine for building path
  125. *
  126. * @param string $path
  127. * @param string $item
  128. * @return string
  129. */
  130. private function join($path, $item)
  131. {
  132. return trim($path . ($item ? '/' . $item : ''), '/');
  133. }
  134. /**
  135. * {@inheritdoc}
  136. * @throws File\NotFoundException if file cannot be resolved
  137. */
  138. public function getSourceFile()
  139. {
  140. if (null === $this->resolvedFile) {
  141. $this->resolvedFile = $this->source->getFile($this);
  142. if (false === $this->resolvedFile) {
  143. throw new File\NotFoundException("Unable to resolve the source file for '{$this->getPath()}'");
  144. }
  145. }
  146. return $this->resolvedFile;
  147. }
  148. /**
  149. * Get source content type
  150. *
  151. * @return string
  152. * @since 101.0.0
  153. */
  154. public function getSourceContentType()
  155. {
  156. if ($this->sourceContentType === null) {
  157. $this->sourceContentType = $this->source->getSourceContentType($this);
  158. }
  159. return $this->sourceContentType;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function getContent()
  165. {
  166. $content = $this->source->getContent($this);
  167. if (false === $content) {
  168. throw new File\NotFoundException("Unable to get content for '{$this->getPath()}'");
  169. }
  170. return $content;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function getFilePath()
  176. {
  177. return $this->filePath;
  178. }
  179. /**
  180. * {@inheritdoc}
  181. * @return File\Context
  182. */
  183. public function getContext()
  184. {
  185. return $this->context;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getModule()
  191. {
  192. return $this->module;
  193. }
  194. }