Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplyThemeCustomizationObserver.php
Go to the documentation of this file.
1 <?php
8 
13 
19 {
23  private $currentTheme;
24 
28  private $pageAssets;
29 
33  protected $assetRepo;
34 
38  protected $logger;
39 
46  public function __construct(
47  \Magento\Framework\View\DesignInterface $design,
48  \Magento\Framework\View\Asset\GroupedCollection $assets,
49  \Magento\Framework\View\Asset\Repository $assetRepo,
50  \Psr\Log\LoggerInterface $logger
51  ) {
52  $this->currentTheme = $design->getDesignTheme();
53  $this->pageAssets = $assets;
54  $this->assetRepo = $assetRepo;
55  $this->logger = $logger;
56  }
57 
65  public function execute(\Magento\Framework\Event\Observer $observer)
66  {
68  foreach ($this->currentTheme->getCustomization()->getFiles() as $themeFile) {
69  try {
70  $service = $themeFile->getCustomizationService();
71  if ($service instanceof \Magento\Framework\View\Design\Theme\Customization\FileAssetInterface) {
72  $identifier = $themeFile->getData('file_path');
73  $dirPath = \Magento\Framework\View\Design\Theme\Customization\Path::DIR_NAME
74  . '/' . $this->currentTheme->getId();
75  $asset = $this->assetRepo->createArbitrary(
76  $identifier,
77  $dirPath,
79  \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
80  );
81  $this->pageAssets->add($identifier, $asset);
82  }
83  } catch (\InvalidArgumentException $e) {
84  $this->logger->critical($e);
85  }
86  }
87  }
88 }
__construct(\Magento\Framework\View\DesignInterface $design, \Magento\Framework\View\Asset\GroupedCollection $assets, \Magento\Framework\View\Asset\Repository $assetRepo, \Psr\Log\LoggerInterface $logger)