DeployStaticFile.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Service;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Filesystem\File\WriteInterface;
  10. use Magento\Framework\View\Asset\Minification;
  11. use Magento\Framework\View\Asset\Repository;
  12. use Magento\Framework\App\View\Asset\Publisher;
  13. use Magento\Framework\View\Asset\PreProcessor\FileNameResolver;
  14. use Magento\Framework\Filesystem\Directory\ReadInterface;
  15. /**
  16. * Deploy static file service
  17. */
  18. class DeployStaticFile
  19. {
  20. /**
  21. * @var Filesystem
  22. */
  23. private $filesystem;
  24. /**
  25. * @var \Magento\Framework\View\Asset\Repository
  26. */
  27. private $assetRepo;
  28. /**
  29. * @var Publisher
  30. */
  31. private $assetPublisher;
  32. /**
  33. * @var FileNameResolver
  34. */
  35. private $fileNameResolver;
  36. /**
  37. * @var Minification
  38. */
  39. private $minification;
  40. /**
  41. * Public static files directory read interface
  42. *
  43. * @var ReadInterface
  44. */
  45. private $tmpDir;
  46. /**
  47. * DeployStaticFile constructor
  48. *
  49. * @param Filesystem $filesystem
  50. * @param Repository $assetRepo
  51. * @param Publisher $assetPublisher
  52. * @param FileNameResolver $fileNameResolver
  53. * @param Minification $minification
  54. */
  55. public function __construct(
  56. Filesystem $filesystem,
  57. Repository $assetRepo,
  58. Publisher $assetPublisher,
  59. FileNameResolver $fileNameResolver,
  60. Minification $minification
  61. ) {
  62. $this->filesystem = $filesystem;
  63. $this->assetRepo = $assetRepo;
  64. $this->assetPublisher = $assetPublisher;
  65. $this->fileNameResolver = $fileNameResolver;
  66. $this->pubStaticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
  67. $this->minification = $minification;
  68. $this->tmpDir = $filesystem->getDirectoryWrite(DirectoryList::TMP_MATERIALIZATION_DIR);
  69. }
  70. /**
  71. * @param string $fileName
  72. * @param array $params ['area' =>, 'theme' =>, 'locale' =>, 'module' =>]
  73. * @return string
  74. */
  75. public function deployFile($fileName, array $params = [])
  76. {
  77. $params['publish'] = true;
  78. $asset = $this->assetRepo->createAsset($this->resolveFile($fileName), $params);
  79. $this->assetPublisher->publish($asset);
  80. return $asset->getPath();
  81. }
  82. /**
  83. * @param string $path
  84. * @return void
  85. */
  86. public function deleteFile($path)
  87. {
  88. if ($this->pubStaticDir->isExist($path)) {
  89. $absolutePath = $this->pubStaticDir->getAbsolutePath($path);
  90. if (is_link($absolutePath)) {
  91. $this->pubStaticDir->getDriver()->deleteFile($absolutePath);
  92. } else {
  93. if ($this->pubStaticDir->getDriver()->isFile($absolutePath)) {
  94. $this->pubStaticDir->getDriver()->deleteFile($absolutePath);
  95. } else {
  96. $this->pubStaticDir->getDriver()->deleteDirectory($absolutePath);
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Read resolved file from pub static directory
  103. *
  104. * @param string $fileName
  105. * @param string $filePath
  106. * @return string|false
  107. */
  108. public function readFile($fileName, $filePath)
  109. {
  110. $fileName = $this->minification->addMinifiedSign($fileName);
  111. $relativePath = $filePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName);
  112. if ($this->pubStaticDir->isFile($relativePath)) {
  113. return $this->pubStaticDir->readFile($relativePath);
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * @param string $fileName
  120. * @param string $filePath
  121. * @return WriteInterface
  122. */
  123. public function openFile($fileName, $filePath)
  124. {
  125. $relativePath = $filePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName);
  126. return $this->pubStaticDir->openFile($relativePath, 'w+');
  127. }
  128. /**
  129. * Write resolved file to pub static directory
  130. *
  131. * @param string $fileName
  132. * @param string $filePath
  133. * @param string $content
  134. * @return int The number of bytes that were written.
  135. */
  136. public function writeFile($fileName, $filePath, $content)
  137. {
  138. $relativePath = $filePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName);
  139. return $this->pubStaticDir->writeFile($relativePath, $content);
  140. }
  141. /**
  142. * Copy resolved $fileName from $targetPath to $destinationPath
  143. *
  144. * @param string $fileName
  145. * @param string $sourcePath
  146. * @param string $targetPath
  147. * @return bool
  148. */
  149. public function copyFile($fileName, $sourcePath, $targetPath)
  150. {
  151. $fileName = $this->minification->addMinifiedSign($fileName);
  152. return $this->pubStaticDir->copyFile(
  153. $sourcePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName),
  154. $targetPath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName)
  155. );
  156. }
  157. /**
  158. * Read file from tmp directory
  159. *
  160. * @param string $fileName
  161. * @param string $filePath
  162. * @return string
  163. */
  164. public function readTmpFile($fileName, $filePath)
  165. {
  166. $relativePath = $filePath . DIRECTORY_SEPARATOR . $fileName;
  167. return $this->tmpDir->isFile($relativePath) ? $this->tmpDir->readFile($relativePath) : false;
  168. }
  169. /**
  170. * Write file to tmp directory
  171. *
  172. * @param string $fileName
  173. * @param string $filePath
  174. * @param string $content
  175. * @return int The number of bytes that were written.
  176. */
  177. public function writeTmpFile($fileName, $filePath, $content)
  178. {
  179. $relativePath = $filePath . DIRECTORY_SEPARATOR . $this->resolveFile($fileName);
  180. return $this->tmpDir->writeFile($relativePath, $content);
  181. }
  182. /**
  183. * Resolve filename
  184. *
  185. * @param string $fileName
  186. * @return string
  187. */
  188. private function resolveFile($fileName)
  189. {
  190. $compiledFile = str_replace(
  191. Repository::FILE_ID_SEPARATOR,
  192. '/',
  193. $this->fileNameResolver->resolve($fileName)
  194. );
  195. return $compiledFile;
  196. }
  197. }