|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Csp\Model\Deploy\Package\Processor\PostProcessor; |
| 9 | + |
| 10 | +use Magento\Deploy\Package\Package; |
| 11 | +use Magento\Deploy\Package\PackageFileFactory; |
| 12 | +use Magento\Deploy\Service\DeployStaticFile; |
| 13 | +use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter; |
| 14 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 15 | +use Magento\Framework\Exception\FileSystemException; |
| 16 | +use Magento\Framework\FileSystem; |
| 17 | +use Magento\Framework\View\Asset\Minification; |
| 18 | +use Magento\Framework\View\Asset\RepositoryMap; |
| 19 | +use Magento\Csp\Model\SubresourceIntegrityFactory; |
| 20 | +use Magento\Csp\Model\SubresourceIntegrity\HashGenerator; |
| 21 | +use Magento\Framework\Filesystem\DriverInterface; |
| 22 | +use Magento\Csp\Model\SubresourceIntegrityCollector; |
| 23 | + |
| 24 | +class Map extends \Magento\Deploy\Package\Processor\PostProcessor\Map |
| 25 | +{ |
| 26 | + |
| 27 | + /** |
| 28 | + * @var HashGenerator |
| 29 | + */ |
| 30 | + private HashGenerator $hashGenerator; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var SubresourceIntegrityCollector |
| 34 | + */ |
| 35 | + private SubresourceIntegrityCollector $integrityCollector; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var SubresourceIntegrityFactory |
| 39 | + */ |
| 40 | + private SubresourceIntegrityFactory $integrityFactory; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Minification |
| 44 | + */ |
| 45 | + private Minification $minification; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var DriverInterface |
| 49 | + */ |
| 50 | + private DriverInterface $driver; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var FileSystem |
| 54 | + */ |
| 55 | + private FileSystem $filesystem; |
| 56 | + |
| 57 | + /** |
| 58 | + * @param DeployStaticFile $deployStaticFile |
| 59 | + * @param PhpFormatter $formatter |
| 60 | + * @param PackageFileFactory $packageFileFactory |
| 61 | + * @param Minification $minification |
| 62 | + * @param SubresourceIntegrityFactory $integrityFactory |
| 63 | + * @param HashGenerator $hashGenerator |
| 64 | + * @param DriverInterface $driver |
| 65 | + * @param SubresourceIntegrityCollector $integrityCollector |
| 66 | + * @param FileSystem $filesystem |
| 67 | + */ |
| 68 | + public function __construct( |
| 69 | + DeployStaticFile $deployStaticFile, |
| 70 | + PhpFormatter $formatter, |
| 71 | + PackageFileFactory $packageFileFactory, |
| 72 | + Minification $minification, |
| 73 | + SubresourceIntegrityFactory $integrityFactory, |
| 74 | + HashGenerator $hashGenerator, |
| 75 | + DriverInterface $driver, |
| 76 | + SubresourceIntegrityCollector $integrityCollector, |
| 77 | + Filesystem $filesystem |
| 78 | + ) { |
| 79 | + $this->minification = $minification; |
| 80 | + $this->integrityFactory = $integrityFactory; |
| 81 | + $this->hashGenerator = $hashGenerator; |
| 82 | + $this->driver = $driver; |
| 83 | + $this->integrityCollector = $integrityCollector; |
| 84 | + $this->filesystem = $filesystem; |
| 85 | + parent::__construct($deployStaticFile, $formatter, $packageFileFactory, $minification); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @inheritdoc |
| 90 | + * @throws FileSystemException |
| 91 | + */ |
| 92 | + public function process(Package $package, array $options) |
| 93 | + { |
| 94 | + parent::process($package, $options); |
| 95 | + $fileName = $this->minification->addMinifiedSign(RepositoryMap::REQUIRE_JS_MAP_NAME); |
| 96 | + $path = $package->getPath(); |
| 97 | + $relativePath = $path . DIRECTORY_SEPARATOR . $fileName; |
| 98 | + |
| 99 | + if ($this->fileExists($relativePath)) { |
| 100 | + $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); |
| 101 | + $absolutePath = $dir->getAbsolutePath($relativePath); |
| 102 | + $fileContent = $this->driver->fileGetContents($absolutePath); |
| 103 | + |
| 104 | + if ($fileContent) { |
| 105 | + $integrity = $this->integrityFactory->create( |
| 106 | + [ |
| 107 | + "data" => [ |
| 108 | + 'hash' => $this->hashGenerator->generate($fileContent), |
| 109 | + 'path' => $relativePath |
| 110 | + ] |
| 111 | + ] |
| 112 | + ); |
| 113 | + $this->integrityCollector->collect($integrity); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Check if file exist |
| 120 | + * |
| 121 | + * @param string $path |
| 122 | + * @return bool |
| 123 | + * @throws FileSystemException |
| 124 | + */ |
| 125 | + private function fileExists(string $path): bool |
| 126 | + { |
| 127 | + $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); |
| 128 | + return $dir->isExist($path); |
| 129 | + } |
| 130 | +} |
0 commit comments