123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Asset\PreProcessor;
- use Magento\Framework\ObjectManagerInterface;
- use Magento\Framework\View\Asset\File\FallbackContext;
- use Magento\Framework\View\Asset\LockerProcessInterface;
- use Magento\Framework\View\Asset\ContentProcessorInterface;
- use Magento\Framework\View\Asset\PreProcessor\AlternativeSource\AssetBuilder;
- /**
- * Class AlternativeSource
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class AlternativeSource implements AlternativeSourceInterface
- {
- /**
- * The key name of the processor class
- */
- const PROCESSOR_CLASS = 'class';
- /**
- * @var Helper\SortInterface
- */
- private $sorter;
- /**
- * @var array
- */
- private $alternatives;
- /**
- * @var ObjectManagerInterface
- */
- private $objectManager;
- /**
- * @var array
- */
- private $alternativesSorted;
- /**
- * @var LockerProcessInterface
- */
- private $lockerProcess;
- /**
- * @var string
- */
- private $lockName;
- /**
- * @var AssetBuilder
- */
- private $assetBuilder;
- /**
- * @var FilenameResolverInterface
- */
- private $filenameResolver;
- /**
- * Constructor
- *
- * @param FilenameResolverInterface $filenameResolver
- * @param ObjectManagerInterface $objectManager
- * @param LockerProcessInterface $lockerProcess
- * @param Helper\SortInterface $sorter
- * @param AssetBuilder $assetBuilder
- * @param string $lockName
- * @param array $alternatives
- */
- public function __construct(
- FilenameResolverInterface $filenameResolver,
- ObjectManagerInterface $objectManager,
- LockerProcessInterface $lockerProcess,
- Helper\SortInterface $sorter,
- AssetBuilder $assetBuilder,
- $lockName,
- array $alternatives = []
- ) {
- $this->objectManager = $objectManager;
- $this->lockerProcess = $lockerProcess;
- $this->sorter = $sorter;
- $this->alternatives = $alternatives;
- $this->lockName = $lockName;
- $this->assetBuilder = $assetBuilder;
- $this->filenameResolver = $filenameResolver;
- }
- /**
- * @inheritdoc
- * @throws \UnexpectedValueException
- */
- public function process(Chain $chain)
- {
- $path = $chain->getAsset()->getFilePath();
- $content = $chain->getContent();
- if (trim($content) !== '') {
- return;
- }
- try {
- $this->lockerProcess->lockProcess($this->lockName);
- $module = $chain->getAsset()->getModule();
- /** @var FallbackContext $context */
- $context = $chain->getAsset()->getContext();
- $chain->setContent($this->processContent($path, $content, $module, $context));
- } finally {
- $this->lockerProcess->unlockProcess();
- }
- }
- /**
- * Preparation of content for the destination file
- *
- * @param string $path
- * @param string $content
- * @param string $module
- * @param FallbackContext $context
- * @return string
- * @throws \UnexpectedValueException
- */
- private function processContent($path, $content, $module, FallbackContext $context)
- {
- if ($this->alternativesSorted === null) {
- $this->alternativesSorted = $this->sorter->sort($this->alternatives);
- }
- $path = $this->filenameResolver->resolve($path);
- foreach ($this->alternativesSorted as $name => $alternative) {
- $asset = $this->assetBuilder->setArea($context->getAreaCode())
- ->setTheme($context->getThemePath())
- ->setLocale($context->getLocale())
- ->setModule($module)
- ->setPath(preg_replace(
- '#\.' . preg_quote(pathinfo($path, PATHINFO_EXTENSION)) . '$#',
- '.' . $name,
- $path
- ))->build();
- $processor = $this->objectManager->get($alternative[self::PROCESSOR_CLASS]);
- if (!$processor instanceof ContentProcessorInterface) {
- throw new \UnexpectedValueException(
- '"' . $alternative[self::PROCESSOR_CLASS] . '" has to implement the ContentProcessorInterface.'
- );
- }
- $content = $processor->processContent($asset);
- if (trim($content) !== '') {
- return $content;
- }
- }
- return $content;
- }
- /**
- * @inheritdoc
- */
- public function getAlternativesExtensionsNames()
- {
- return array_keys($this->alternatives);
- }
- }
|