|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Csp\Model\Collector\CspWhitelistXml; |
| 10 | + |
| 11 | +use Magento\Framework\Config\FileResolverInterface; |
| 12 | +use Magento\Framework\Filesystem; |
| 13 | +use Magento\Framework\View\Design\ThemeInterface; |
| 14 | +use Magento\Framework\View\DesignInterface; |
| 15 | +use Magento\Framework\View\Design\Theme\CustomizationInterface; |
| 16 | +use Magento\Framework\View\Design\Theme\CustomizationInterfaceFactory; |
| 17 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 18 | +use Magento\Framework\Filesystem\Directory\ReadInterface as DirectoryRead; |
| 19 | +use Magento\Framework\Config\CompositeFileIteratorFactory; |
| 20 | + |
| 21 | +/** |
| 22 | + * Combines configuration files from both modules and current theme. |
| 23 | + */ |
| 24 | +class FileResolver implements FileResolverInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var FileResolverInterface |
| 28 | + */ |
| 29 | + private $moduleFileResolver; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ThemeInterface |
| 33 | + */ |
| 34 | + private $theme; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var CustomizationInterfaceFactory |
| 38 | + */ |
| 39 | + private $themeInfoFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var DirectoryRead |
| 43 | + */ |
| 44 | + private $rootDir; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var CompositeFileIteratorFactory |
| 48 | + */ |
| 49 | + private $iteratorFactory; |
| 50 | + |
| 51 | + /** |
| 52 | + * @param FileResolverInterface $moduleFileResolver |
| 53 | + * @param DesignInterface $design |
| 54 | + * @param CustomizationInterfaceFactory $customizationFactory |
| 55 | + * @param Filesystem $filesystem |
| 56 | + * @param CompositeFileIteratorFactory $iteratorFactory |
| 57 | + */ |
| 58 | + public function __construct( |
| 59 | + FileResolverInterface $moduleFileResolver, |
| 60 | + DesignInterface $design, |
| 61 | + CustomizationInterfaceFactory $customizationFactory, |
| 62 | + Filesystem $filesystem, |
| 63 | + CompositeFileIteratorFactory $iteratorFactory |
| 64 | + ) { |
| 65 | + $this->moduleFileResolver = $moduleFileResolver; |
| 66 | + $this->theme = $design->getDesignTheme(); |
| 67 | + $this->themeInfoFactory = $customizationFactory; |
| 68 | + $this->rootDir = $filesystem->getDirectoryRead(DirectoryList::ROOT); |
| 69 | + $this->iteratorFactory = $iteratorFactory; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @inheritDoc |
| 74 | + */ |
| 75 | + public function get($filename, $scope) |
| 76 | + { |
| 77 | + $configs = $this->moduleFileResolver->get($filename, $scope); |
| 78 | + if ($scope === 'global') { |
| 79 | + $files = []; |
| 80 | + $theme = $this->theme; |
| 81 | + while ($theme) { |
| 82 | + /** @var CustomizationInterface $info */ |
| 83 | + $info = $this->themeInfoFactory->create(['theme' => $theme]); |
| 84 | + $file = $info->getThemeFilesPath() .'/etc/' .$filename; |
| 85 | + if ($this->rootDir->isExist($file)) { |
| 86 | + $files[] = $file; |
| 87 | + } |
| 88 | + $theme = $theme->getParentTheme(); |
| 89 | + } |
| 90 | + $configs = $this->iteratorFactory->create( |
| 91 | + ['paths' => array_reverse($files), 'existingIterator' => $configs] |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return $configs; |
| 96 | + } |
| 97 | +} |
0 commit comments