123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Asset\Bundle;
- use Magento\Framework\View\Asset;
- use Magento\Framework\Filesystem;
- use Magento\Framework\View\Asset\Bundle;
- use Magento\Framework\View\Asset\LocalInterface;
- use Magento\Framework\App\Filesystem\DirectoryList;
- /**
- * BundleService model
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @deprecated 101.0.0
- * @see \Magento\Deploy\Service\Bundle
- */
- class Manager
- {
- const BUNDLE_JS_DIR = 'js/bundle';
- const BUNDLE_PATH = '/js/bundle/bundle';
- const ASSET_TYPE_JS = 'js';
- const ASSET_TYPE_HTML = 'html';
- /**
- * @var \Magento\Framework\Filesystem
- */
- protected $filesystem;
- /**
- * @var \Magento\Framework\View\Asset\Bundle
- */
- protected $bundle;
- /**
- * @var \Magento\Framework\View\Asset\Bundle\ConfigInterface
- */
- protected $bundleConfig;
- /**
- * @var \Magento\Framework\View\Asset\ConfigInterface
- */
- protected $assetConfig;
- /**
- * @var array
- */
- protected $excluded = [];
- /**
- * @var array
- */
- public static $availableTypes = [self::ASSET_TYPE_JS, self::ASSET_TYPE_HTML];
- /**
- * @var Asset\Minification
- */
- private $minification;
- /**
- * @param Filesystem $filesystem
- * @param Bundle $bundle
- * @param Bundle\ConfigInterface $bundleConfig
- * @param Asset\ConfigInterface $assetConfig
- * @param Asset\Minification $minification
- */
- public function __construct(
- Filesystem $filesystem,
- Bundle $bundle,
- Bundle\ConfigInterface $bundleConfig,
- Asset\ConfigInterface $assetConfig,
- Asset\Minification $minification
- ) {
- $this->filesystem = $filesystem;
- $this->assetConfig = $assetConfig;
- $this->bundleConfig = $bundleConfig;
- $this->bundle = $bundle;
- $this->minification = $minification;
- }
- /**
- * Check if asset in exclude list
- *
- * @param LocalInterface $asset
- * @return bool
- */
- protected function isExcluded(LocalInterface $asset)
- {
- $excludedFiles = array_merge(
- $this->bundleConfig->getConfig($asset->getContext())->getExcludedFiles(),
- $this->excluded
- );
- foreach ($excludedFiles as $file) {
- if ($this->isExcludedFile($file, $asset)) {
- return true;
- }
- }
- foreach ($this->bundleConfig->getConfig($asset->getContext())->getExcludedDir() as $directory) {
- if ($this->isExcludedDirectory($directory, $asset)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Check if asset file in excluded directory
- *
- * @param string $directoryPath
- * @param LocalInterface $asset
- * @return bool
- */
- protected function isExcludedDirectory($directoryPath, $asset)
- {
- /** @var $asset LocalInterface */
- $assetDirectory = dirname($asset->getFilePath());
- $assetDirectory .= substr($assetDirectory, -1) != '/' ? '/' : '';
- $directoryPath .= substr($directoryPath, -1) != '/' ? '/' : '';
- /** @var $asset LocalInterface */
- $directoryPathInfo = $this->splitPath($directoryPath);
- if ($directoryPathInfo && $this->compareModules($directoryPathInfo, $asset)) {
- return strpos($assetDirectory, $directoryPathInfo['excludedPath']) === 0;
- }
- return false;
- }
- /**
- * Check if asset file is excluded
- *
- * @param string $filePath
- * @param LocalInterface $asset
- * @return bool
- */
- protected function isExcludedFile($filePath, $asset)
- {
- /** @var $asset LocalInterface */
- $filePathInfo = $this->splitPath($filePath);
- if ($filePathInfo && $this->compareModules($filePathInfo, $asset)) {
- return $asset->getFilePath() == $filePathInfo['excludedPath'];
- }
- return false;
- }
- /**
- * Compare asset module with excluded module
- *
- * @param array $filePathInfo
- * @param LocalInterface $asset
- * @return bool
- */
- protected function compareModules($filePathInfo, $asset)
- {
- /** @var $asset LocalInterface */
- if (($filePathInfo['excludedModule'] == 'Lib' && $asset->getModule() == '')
- || ($filePathInfo['excludedModule'] == $asset->getModule())
- ) {
- return true;
- }
- return false;
- }
- /**
- * Get excluded module and path from complex string
- *
- * @param string $path
- * @return array|bool
- */
- protected function splitPath($path)
- {
- if (strpos($path, '::') !== false) {
- list($excludedModule, $excludedPath) = explode('::', $path);
- return [
- 'excludedModule' => $excludedModule,
- 'excludedPath' => $excludedPath,
- ];
- }
- return false;
- }
- /**
- * Add asset to the bundle
- *
- * @param LocalInterface $asset
- * @return bool
- */
- public function addAsset(LocalInterface $asset)
- {
- if (!$this->isValidAsset($asset)) {
- return false;
- }
- $this->bundle->addAsset($asset);
- return true;
- }
- /**
- * @param LocalInterface $asset
- * @return bool
- */
- protected function isAssetMinification(LocalInterface $asset)
- {
- $sourceFile = $asset->getSourceFile();
- $extension = $asset->getContentType();
- if (in_array($sourceFile, $this->excluded)) {
- return false;
- }
- if (strpos($sourceFile, '.min.') === false) {
- $info = pathinfo($asset->getPath());
- $assetMinifiedPath = $info['dirname'] . '/' . $info['filename'] . '.min.' . $info['extension'];
- if ($this->filesystem->getDirectoryRead(DirectoryList::APP)->isExist($assetMinifiedPath)) {
- $this->excluded[] = $sourceFile;
- return false;
- }
- } else {
- $this->excluded[] = $this->filesystem->getDirectoryRead(DirectoryList::APP)
- ->getAbsolutePath(str_replace(".min.$extension", ".$extension", $asset->getPath()));
- }
- return true;
- }
- /**
- * @param LocalInterface $asset
- * @return bool
- */
- protected function isValidAsset(LocalInterface $asset)
- {
- if ($this->isValidType($asset)
- && $this->isAssetMinification($asset)
- && !$this->isExcluded($asset)
- ) {
- return true;
- }
- return false;
- }
- /**
- * @param LocalInterface $asset
- * @return bool
- */
- protected function isValidType(LocalInterface $asset)
- {
- $type = $asset->getContentType();
- if (!in_array($type, self::$availableTypes)) {
- return false;
- }
- return true;
- }
- /**
- * Flush bundle
- *
- * @return void
- */
- public function flush()
- {
- $this->bundle->flush();
- }
- }
|