Publisher.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\View\Asset;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem\Directory\WriteFactory;
  9. use Magento\Framework\View\Asset;
  10. /**
  11. * A publishing service for view assets
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Publisher
  17. {
  18. /**
  19. * @var \Magento\Framework\Filesystem
  20. */
  21. protected $filesystem;
  22. /**
  23. * @var MaterializationStrategy\Factory
  24. */
  25. private $materializationStrategyFactory;
  26. /**
  27. * @var WriteFactory
  28. */
  29. private $writeFactory;
  30. /**
  31. * @param \Magento\Framework\Filesystem $filesystem
  32. * @param MaterializationStrategy\Factory $materializationStrategyFactory
  33. * @param WriteFactory $writeFactory
  34. */
  35. public function __construct(
  36. \Magento\Framework\Filesystem $filesystem,
  37. MaterializationStrategy\Factory $materializationStrategyFactory,
  38. WriteFactory $writeFactory
  39. ) {
  40. $this->filesystem = $filesystem;
  41. $this->materializationStrategyFactory = $materializationStrategyFactory;
  42. $this->writeFactory = $writeFactory;
  43. }
  44. /**
  45. * @param Asset\LocalInterface $asset
  46. * @return bool
  47. */
  48. public function publish(Asset\LocalInterface $asset)
  49. {
  50. $dir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
  51. if ($dir->isExist($asset->getPath())) {
  52. return true;
  53. }
  54. return $this->publishAsset($asset);
  55. }
  56. /**
  57. * Publish the asset
  58. *
  59. * @param Asset\LocalInterface $asset
  60. * @return bool
  61. */
  62. private function publishAsset(Asset\LocalInterface $asset)
  63. {
  64. $targetDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
  65. $fullSource = $asset->getSourceFile();
  66. $source = basename($fullSource);
  67. $sourceDir = $this->writeFactory->create(dirname($fullSource));
  68. $destination = $asset->getPath();
  69. $strategy = $this->materializationStrategyFactory->create($asset);
  70. return $strategy->publishFile($sourceDir, $targetDir, $source, $destination);
  71. }
  72. }