Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Checksum.php
Go to the documentation of this file.
1 <?php
7 
11 
18 class Checksum implements \Magento\Framework\View\Asset\MergeStrategyInterface
19 {
23  protected $strategy;
24 
28  protected $filesystem;
29 
33  private $assetSource;
34 
39  public function __construct(
41  \Magento\Framework\Filesystem $filesystem
42  ) {
43  $this->strategy = $strategy;
44  $this->filesystem = $filesystem;
45  }
46 
51  private function getAssetSource()
52  {
53  if (!$this->assetSource) {
54  $this->assetSource = ObjectManager::getInstance()->get(Source::class);
55  }
56  return $this->assetSource;
57  }
58 
62  public function merge(array $assetsToMerge, \Magento\Framework\View\Asset\LocalInterface $resultAsset)
63  {
64  $rootDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);
65  $mTime = null;
67  foreach ($assetsToMerge as $asset) {
68  $sourceFile = $this->getAssetSource()->findSource($asset);
69  $mTime .= $rootDir->stat($rootDir->getRelativePath($sourceFile))['mtime'];
70  }
71 
72  if (null === $mTime) {
73  return; // nothing to merge
74  }
75 
76  $dat = $resultAsset->getPath() . '.dat';
77  $targetDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
78  if (!$targetDir->isExist($dat) || strcmp($mTime, $targetDir->readFile($dat)) !== 0) {
79  $this->strategy->merge($assetsToMerge, $resultAsset);
80  $targetDir->writeFile($dat, $mTime);
81  }
82  }
83 }
merge(array $assetsToMerge, LocalInterface $resultAsset)
__construct(\Magento\Framework\View\Asset\MergeStrategyInterface $strategy, \Magento\Framework\Filesystem $filesystem)
Definition: Checksum.php:39
$rootDir
Definition: website.php:12