Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Package.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Deploy\Package;
7 
11 
15 class Package
16 {
20  private $packagePool;
21 
25  private $fileNameResolver;
26 
30  private $area;
31 
35  private $theme;
36 
40  private $locale;
41 
45  private $isVirtual;
46 
50  private $preProcessors;
51 
55  private $postProcessors;
56 
60  private $files = [];
61 
65  private $map = [];
66 
70  private $parent;
71 
75  private $parentPackages;
76 
80  private $state;
81 
85  private $params = [];
86 
90  const STATE_PROGRESS = 0;
91 
95  const STATE_COMPLETED = 1;
96 
100  const BASE_AREA = 'base';
101 
105  const BASE_THEME = 'Magento/base';
106 
110  const BASE_LOCALE = 'default';
111 
115  private $packageDefaultValues = [
116  'area' => self::BASE_AREA,
117  'theme' => self::BASE_THEME,
118  'locale' => self::BASE_LOCALE
119  ];
120 
132  public function __construct(
133  PackagePool $packagePool,
134  FileNameResolver $fileNameResolver,
135  $area,
136  $theme,
137  $locale,
138  $isVirtual = false,
139  array $preProcessors = [],
140  array $postProcessors = []
141  ) {
142  $this->packagePool = $packagePool;
143  $this->fileNameResolver = $fileNameResolver;
144  $this->area = $area;
145  $this->theme = $theme;
146  $this->locale = $locale;
147  $this->isVirtual = $isVirtual;
148  $this->preProcessors = $preProcessors;
149  $this->postProcessors = $postProcessors;
150  }
151 
155  public function getArea()
156  {
157  return $this->area;
158  }
159 
163  public function getParent()
164  {
165  return $this->parent;
166  }
167 
171  public function getTheme()
172  {
173  return $this->theme;
174  }
175 
179  public function getLocale()
180  {
181  return $this->locale;
182  }
183 
189  public function getPath()
190  {
191  return $this->getArea() . '/' . $this->getTheme() . '/' . $this->getLocale();
192  }
193 
201  public function isVirtual()
202  {
203  return $this->isVirtual;
204  }
205 
210  public function getParam($name)
211  {
212  return isset($this->params[$name]) ? $this->params[$name] : null;
213  }
214 
220  public function setParam($name, $value)
221  {
222  $this->params[$name] = $value;
223  return true;
224  }
225 
231  public function getThemeModel()
232  {
233  return $this->packagePool->getThemeModel($this->getArea(), $this->getTheme());
234  }
235 
242  public function getFile($fileId)
243  {
244  return isset($this->files[$fileId]) ? $this->files[$fileId] : false;
245  }
246 
253  public function addFile(PackageFile $file)
254  {
255  if (!$file->getLocale()) {
256  $file->setLocale($this->getLocale());
257  }
258  $this->files[$file->getFileId()] = $file;
259 
260  $deployedFilePath = $this->getPath() . '/'
261  . ($file->getModule() ? ($file->getModule() . '/') : '')
262  . $file->getDeployedFileName();
263  $file->setDeployedFilePath($deployedFilePath);
264 
265  return $file->getFileId();
266  }
267 
274  public function addFileToMap(PackageFile $file)
275  {
276  $fileId = $file->getDeployedFileId();
277  $this->map[$fileId] = [
278  'area' => $this->getArea(),
279  'theme' => $this->getTheme(),
280  'locale' => $this->getLocale()
281  ];
282  }
283 
289  public function getFiles()
290  {
291  return $this->files;
292  }
293 
300  public function getFilesByType($type)
301  {
302  $files = [];
304  foreach ($this->getFiles() as $fileId => $file) {
305  if (!$file->getFileName()) {
306  continue;
307  }
308  if ($file->getExtension() == $type) {
309  $files[$fileId] = $file;
310  }
311  }
312  return $files;
313  }
314 
321  public function deleteFile($fileId)
322  {
323  unset($this->files[$fileId]);
324  }
325 
334  public function aggregate(Package $parentPackage = null)
335  {
336  $inheritedFiles = $this->getParentFiles();
337  foreach ($inheritedFiles as $fileId => $file) {
339  if (!$this->getFile($fileId)) {
340  $file = clone $file;
341  $file->setPackage($this);
342  }
343  }
344  if ($parentPackage) {
345  $this->setParent($parentPackage);
346  }
347  return true;
348  }
349 
354  public function setParent($parent)
355  {
356  $this->parent = $parent;
357  return true;
358  }
359 
365  public function getMap()
366  {
367  return $this->map;
368  }
369 
373  public function getState()
374  {
375  return $this->state;
376  }
377 
382  public function setState($state)
383  {
384  $this->state = $state;
385  return true;
386  }
387 
391  public function getInheritanceLevel()
392  {
393  $level = 0;
394  $theme = $this->getThemeModel();
395  if ($theme) {
396  ++$level;
397  while ($theme = $theme->getParentTheme()) {
398  ++$level;
399  }
400  }
401  return $level;
402  }
403 
409  public function getResultMap()
410  {
411  $map = $this->getMap();
412  $parentMap = $this->getParentMap();
413  return array_merge($parentMap, $map);
414  }
415 
421  public function getParentMap()
422  {
423  $map = [];
424  foreach ($this->getParentPackages() as $parentPackage) {
425  $map = array_merge($map, $parentPackage->getMap());
426  }
427  return $map;
428  }
429 
436  public function getParentFiles($type = null)
437  {
438  $files = [];
439  foreach ($this->getParentPackages() as $parentPackage) {
440  if ($type === null) {
441  $files = array_merge($files, $parentPackage->getFiles());
442  } else {
443  $files = array_merge($files, $parentPackage->getFilesByType($type));
444  }
445  }
446  return $files;
447  }
448 
454  public function getParentPackages()
455  {
456  if ($this->parentPackages === null) {
457  $this->parentPackages = [];
458  $parentPaths = [];
459  $this->collectParentPaths(
460  $this,
461  $this->getArea(),
462  $this->getTheme(),
463  $this->getLocale(),
464  $parentPaths,
465  $this->getThemeModel()
466  );
467 
468  // collect packages in reverse order to have closer ancestor goes later
469  foreach (array_reverse($parentPaths) as $path) {
470  if ($package = $this->packagePool->getPackage($path)) {
471  $this->parentPackages[$path] = $package;
472  }
473  }
474  }
475 
476  return $this->parentPackages;
477  }
478 
482  public function getPreProcessors()
483  {
484  return $this->preProcessors;
485  }
486 
490  public function getPostProcessors()
491  {
492  return $this->postProcessors;
493  }
494 
506  private function collectParentPaths(
507  Package $package,
508  $area,
509  $theme,
510  $locale,
511  array & $result = [],
512  ThemeInterface $themeModel = null
513  ) {
514  if (($package->getArea() != $area) || ($package->getTheme() != $theme) || ($package->getLocale() != $locale)) {
515  $result[] = $area . '/' . $theme . '/' . $locale;
516  }
517 
518  if ($locale != $this->packageDefaultValues['locale']) {
519  $result[] = $area . '/' . $theme . '/' . $this->packageDefaultValues['locale'];
520  }
521 
522  if ($themeModel) {
523  if ($themeModel->getParentTheme()) {
524  $this->collectParentPaths(
525  $package,
526  $area,
527  $themeModel->getParentTheme()->getThemePath(),
528  $package->getLocale(),
529  $result,
530  $themeModel->getParentTheme()
531  );
532  } else {
533  $this->collectParentPaths(
534  $package,
535  $area,
536  $this->packageDefaultValues['theme'],
537  $package->getLocale(),
538  $result
539  );
540  }
541  } else {
542  if ($area != $this->packageDefaultValues['area']) {
543  $this->collectParentPaths(
544  $package,
545  $this->packageDefaultValues['area'],
546  $theme,
547  $package->getLocale(),
548  $result
549  );
550  }
551  }
552  }
553 }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
__construct(PackagePool $packagePool, FileNameResolver $fileNameResolver, $area, $theme, $locale, $isVirtual=false, array $preProcessors=[], array $postProcessors=[])
Definition: Package.php:132
addFile(PackageFile $file)
Definition: Package.php:253
addFileToMap(PackageFile $file)
Definition: Package.php:274
if(!isset($_GET['name'])) $name
Definition: log.php:14