Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Manager.php
Go to the documentation of this file.
1 <?php
8 
14 
21 class Manager
22 {
23  const BUNDLE_JS_DIR = 'js/bundle';
24 
25  const BUNDLE_PATH = '/js/bundle/bundle';
26 
27  const ASSET_TYPE_JS = 'js';
28 
29  const ASSET_TYPE_HTML = 'html';
30 
34  protected $filesystem;
35 
39  protected $bundle;
40 
44  protected $bundleConfig;
45 
49  protected $assetConfig;
50 
54  protected $excluded = [];
55 
60 
64  private $minification;
65 
73  public function __construct(
78  Asset\Minification $minification
79  ) {
80  $this->filesystem = $filesystem;
81  $this->assetConfig = $assetConfig;
82  $this->bundleConfig = $bundleConfig;
83  $this->bundle = $bundle;
84  $this->minification = $minification;
85  }
86 
93  protected function isExcluded(LocalInterface $asset)
94  {
95  $excludedFiles = array_merge(
96  $this->bundleConfig->getConfig($asset->getContext())->getExcludedFiles(),
98  );
99  foreach ($excludedFiles as $file) {
100  if ($this->isExcludedFile($file, $asset)) {
101  return true;
102  }
103  }
104 
105  foreach ($this->bundleConfig->getConfig($asset->getContext())->getExcludedDir() as $directory) {
106  if ($this->isExcludedDirectory($directory, $asset)) {
107  return true;
108  }
109  }
110  return false;
111  }
112 
120  protected function isExcludedDirectory($directoryPath, $asset)
121  {
123  $assetDirectory = dirname($asset->getFilePath());
124  $assetDirectory .= substr($assetDirectory, -1) != '/' ? '/' : '';
125  $directoryPath .= substr($directoryPath, -1) != '/' ? '/' : '';
126 
128  $directoryPathInfo = $this->splitPath($directoryPath);
129  if ($directoryPathInfo && $this->compareModules($directoryPathInfo, $asset)) {
130  return strpos($assetDirectory, $directoryPathInfo['excludedPath']) === 0;
131  }
132  return false;
133  }
134 
142  protected function isExcludedFile($filePath, $asset)
143  {
145  $filePathInfo = $this->splitPath($filePath);
146  if ($filePathInfo && $this->compareModules($filePathInfo, $asset)) {
147  return $asset->getFilePath() == $filePathInfo['excludedPath'];
148  }
149  return false;
150  }
151 
159  protected function compareModules($filePathInfo, $asset)
160  {
162  if (($filePathInfo['excludedModule'] == 'Lib' && $asset->getModule() == '')
163  || ($filePathInfo['excludedModule'] == $asset->getModule())
164  ) {
165  return true;
166  }
167  return false;
168  }
169 
176  protected function splitPath($path)
177  {
178  if (strpos($path, '::') !== false) {
179  list($excludedModule, $excludedPath) = explode('::', $path);
180  return [
181  'excludedModule' => $excludedModule,
182  'excludedPath' => $excludedPath,
183  ];
184  }
185  return false;
186  }
187 
194  public function addAsset(LocalInterface $asset)
195  {
196  if (!$this->isValidAsset($asset)) {
197  return false;
198  }
199 
200  $this->bundle->addAsset($asset);
201  return true;
202  }
203 
208  protected function isAssetMinification(LocalInterface $asset)
209  {
210  $sourceFile = $asset->getSourceFile();
211  $extension = $asset->getContentType();
212  if (in_array($sourceFile, $this->excluded)) {
213  return false;
214  }
215 
216  if (strpos($sourceFile, '.min.') === false) {
217  $info = pathinfo($asset->getPath());
218  $assetMinifiedPath = $info['dirname'] . '/' . $info['filename'] . '.min.' . $info['extension'];
219  if ($this->filesystem->getDirectoryRead(DirectoryList::APP)->isExist($assetMinifiedPath)) {
220  $this->excluded[] = $sourceFile;
221  return false;
222  }
223  } else {
224  $this->excluded[] = $this->filesystem->getDirectoryRead(DirectoryList::APP)
225  ->getAbsolutePath(str_replace(".min.$extension", ".$extension", $asset->getPath()));
226  }
227 
228  return true;
229  }
230 
235  protected function isValidAsset(LocalInterface $asset)
236  {
237  if ($this->isValidType($asset)
238  && $this->isAssetMinification($asset)
239  && !$this->isExcluded($asset)
240  ) {
241  return true;
242  }
243  return false;
244  }
245 
250  protected function isValidType(LocalInterface $asset)
251  {
252  $type = $asset->getContentType();
253  if (!in_array($type, self::$availableTypes)) {
254  return false;
255  }
256 
257  return true;
258  }
259 
265  public function flush()
266  {
267  $this->bundle->flush();
268  }
269 }
isAssetMinification(LocalInterface $asset)
Definition: Manager.php:208
$type
Definition: item.phtml:13
__construct(Filesystem $filesystem, Bundle $bundle, Bundle\ConfigInterface $bundleConfig, Asset\ConfigInterface $assetConfig, Asset\Minification $minification)
Definition: Manager.php:73
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52