Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Bundle.php
Go to the documentation of this file.
1 <?php
7 
16 
23 class Bundle
24 {
28  const BUNDLE_JS_DIR = 'js/bundle';
29 
33  const ASSET_TYPE_JS = 'js';
34 
38  const ASSET_TYPE_HTML = 'html';
39 
45  private $pubStaticDir;
46 
53  private $bundleFactory;
54 
60  private $utilityFiles;
61 
67  private $excludedCache = [];
68 
74  public static $availableTypes = [
77  ];
78 
87  public function __construct(
89  BundleInterfaceFactory $bundleFactory,
90  BundleConfig $bundleConfig,
92  ) {
93  $this->pubStaticDir = $filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
94  $this->bundleFactory = $bundleFactory;
95  $this->bundleConfig = $bundleConfig;
96  $this->utilityFiles = $files;
97  }
98 
107  public function deploy($area, $theme, $locale)
108  {
109  $bundle = $this->bundleFactory->create([
110  'area' => $area,
111  'theme' => $theme,
112  'locale' => $locale
113  ]);
114 
115  // delete all previously created bundle files
116  $bundle->clear();
117  $files = [];
118  $mapFilePath = $area . '/' . $theme . '/' . $locale . '/' . RepositoryMap::RESULT_MAP_NAME;
119  if ($this->pubStaticDir->isFile($mapFilePath)) {
120  // map file is available in compact mode, so no need to scan filesystem one more time
121  $resultMap = $this->pubStaticDir->readFile($mapFilePath);
122  if ($resultMap) {
123  $files = json_decode($resultMap, true);
124  }
125  } else {
126  $packageDir = $this->pubStaticDir->getAbsolutePath($area . '/' . $theme . '/' . $locale);
127  $files = $this->utilityFiles->getFiles([$packageDir], '*.*');
128  }
129 
130  foreach ($files as $filePath => $sourcePath) {
131  if (is_array($sourcePath)) {
132  $filePath = str_replace(Repository::FILE_ID_SEPARATOR, '/', $filePath);
133  $sourcePath = $sourcePath['area']
134  . '/' . $sourcePath['theme']
135  . '/' . $sourcePath['locale']
136  . '/' . $filePath;
137  } else {
138  $sourcePath = str_replace('\\', '/', $sourcePath);
139  $sourcePath = $this->pubStaticDir->getRelativePath($sourcePath);
140  $filePath = substr($sourcePath, strlen($area . '/' . $theme . '/' . $locale) + 1);
141  }
142 
143  $contentType = pathinfo($filePath, PATHINFO_EXTENSION);
144  if (!in_array($contentType, self::$availableTypes)) {
145  continue;
146  }
147 
148  if ($this->hasMinVersion($filePath) || $this->isExcluded($filePath, $area, $theme)) {
149  continue;
150  }
151 
152  $bundle->addFile($filePath, $sourcePath, $contentType);
153  }
154  $bundle->flush();
155  }
156 
163  private function hasMinVersion($filePath)
164  {
165  if (in_array($filePath, $this->excludedCache)) {
166  return true;
167  }
168 
169  $info = pathinfo($filePath);
170  if (strpos($filePath, '.min.') !== false) {
171  $this->excludedCache[] = str_replace(".min.{$info['extension']}", ".{$info['extension']}", $filePath);
172  } else {
173  $pathToMinVersion = $info['dirname'] . '/' . $info['filename'] . '.min.' . $info['extension'];
174  if ($this->pubStaticDir->isExist($pathToMinVersion)) {
175  $this->excludedCache[] = $filePath;
176  return true;
177  }
178  }
179 
180  return false;
181  }
182 
191  private function isExcluded($filePath, $area, $theme)
192  {
193  $excludedFiles = $this->bundleConfig->getExcludedFiles($area, $theme);
194  foreach ($excludedFiles as $excludedFileId) {
195  $excludedFilePath = $this->prepareExcludePath($excludedFileId);
196  if ($excludedFilePath === $filePath) {
197  return true;
198  }
199  }
200 
201  $excludedDirs = $this->bundleConfig->getExcludedDirectories($area, $theme);
202  foreach ($excludedDirs as $directoryId) {
203  $directoryPath = $this->prepareExcludePath($directoryId);
204  if (strpos($filePath, $directoryPath) === 0) {
205  return true;
206  }
207  }
208  return false;
209  }
210 
217  private function prepareExcludePath($path)
218  {
219  if (strpos($path, Repository::FILE_ID_SEPARATOR) !== false) {
220  list($excludedModule, $excludedPath) = explode(Repository::FILE_ID_SEPARATOR, $path);
221  if ($excludedModule == 'Lib') {
222  return $excludedPath;
223  } else {
224  return $excludedModule . '/' . $excludedPath;
225  }
226  }
227  return $path;
228  }
229 }
$theme
__construct(Filesystem $filesystem, BundleInterfaceFactory $bundleFactory, BundleConfig $bundleConfig, Files $files)
Definition: Bundle.php:87
deploy($area, $theme, $locale)
Definition: Bundle.php:107
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$filesystem
foreach($appDirs as $dir) $files