Checksum.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Asset\MergeStrategy;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\View\Asset\Source;
  10. /**
  11. * Skip merging if all of the files that need to be merged were not modified
  12. *
  13. * Each file will be resolved and its mtime will be checked.
  14. * Then combination of all mtimes will be compared to a special .dat file that contains mtimes from previous merging
  15. */
  16. class Checksum implements \Magento\Framework\View\Asset\MergeStrategyInterface
  17. {
  18. /**
  19. * @var \Magento\Framework\View\Asset\MergeStrategyInterface
  20. */
  21. protected $strategy;
  22. /**
  23. * @var \Magento\Framework\Filesystem
  24. */
  25. protected $filesystem;
  26. /**
  27. * @var Source
  28. */
  29. private $assetSource;
  30. /**
  31. * @param \Magento\Framework\View\Asset\MergeStrategyInterface $strategy
  32. * @param \Magento\Framework\Filesystem $filesystem
  33. */
  34. public function __construct(
  35. \Magento\Framework\View\Asset\MergeStrategyInterface $strategy,
  36. \Magento\Framework\Filesystem $filesystem
  37. ) {
  38. $this->strategy = $strategy;
  39. $this->filesystem = $filesystem;
  40. }
  41. /**
  42. * @deprecated 101.0.0
  43. * @return Source
  44. */
  45. private function getAssetSource()
  46. {
  47. if (!$this->assetSource) {
  48. $this->assetSource = ObjectManager::getInstance()->get(Source::class);
  49. }
  50. return $this->assetSource;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function merge(array $assetsToMerge, \Magento\Framework\View\Asset\LocalInterface $resultAsset)
  56. {
  57. $rootDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);
  58. $mTime = null;
  59. /** @var \Magento\Framework\View\Asset\MergeableInterface $asset */
  60. foreach ($assetsToMerge as $asset) {
  61. $sourceFile = $this->getAssetSource()->findSource($asset);
  62. $mTime .= $rootDir->stat($rootDir->getRelativePath($sourceFile))['mtime'];
  63. }
  64. if (null === $mTime) {
  65. return; // nothing to merge
  66. }
  67. $dat = $resultAsset->getPath() . '.dat';
  68. $targetDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
  69. if (!$targetDir->isExist($dat) || strcmp($mTime, $targetDir->readFile($dat)) !== 0) {
  70. $this->strategy->merge($assetsToMerge, $resultAsset);
  71. $targetDir->writeFile($dat, $mTime);
  72. }
  73. }
  74. }