Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PackagePool.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Deploy\Package;
7 
13 
18 {
22  private $collector;
23 
27  private $themes;
28 
32  private $packageFactory;
33 
37  private $packages = [];
38 
42  private $collected = false;
43 
51  public function __construct(
52  Collector $collector,
53  ListInterface $themeCollection,
54  PackageFactory $packageFactory
55  ) {
56  $this->collector = $collector;
57  $themeCollection->clear()->resetConstraints();
58  $this->themes = $themeCollection->getItems();
59  $this->packageFactory = $packageFactory;
60  }
61 
66  public function getPackage($path)
67  {
68  $this->collect();
69  return isset($this->packages[$path]) ? $this->packages[$path] : null;
70  }
71 
75  public function getPackages()
76  {
77  $this->collect();
78  return $this->packages;
79  }
80 
86  public function getThemeModel($areaCode, $themePath)
87  {
88  $theme = $this->getThemeByFullPath($areaCode . '/' . $themePath);
89  if ($theme && !$theme->getThemePath()) {
90  $theme->setThemePath($themePath);
91  }
92  return $theme;
93  }
94 
99  public function getPackagesForDeployment(array $options)
100  {
101  $this->collect();
102  $this->ensurePackagesForRequiredLocales($options);
103 
104  $toSkip = [];
105  $toDeploy = [];
106  foreach ($this->packages as $path => $package) {
107  if ($this->checkPackageSkip($package, $options)) {
108  $toSkip[$path] = $package;
109  continue;
110  } else {
111  $toDeploy[$path] = $package;
112  }
113  }
114 
115  foreach ($toSkip as $path => $package) {
116  if (!$this->isAncestorForDeployedPackages($package, $toDeploy)) {
117  unset($this->packages[$path]);
118  }
119  }
120 
121  return $this->packages;
122  }
123 
131  private function isAncestorForDeployedPackages(Package $excludedPackage, array $deployedPackages)
132  {
133  foreach ($deployedPackages as $deployedPackage) {
134  $parents = $deployedPackage->getParentPackages();
135  if (array_key_exists($excludedPackage->getPath(), $parents)) {
136  return true;
137  }
138  }
139  return false;
140  }
141 
146  private function getThemeByFullPath($fullPath)
147  {
148  foreach ($this->themes as $theme) {
149  if ($theme->getFullPath() === $fullPath) {
150  return $theme;
151  }
152  }
153  return null;
154  }
155 
160  private function collect($recollect = false)
161  {
162  if (!$this->collected || $recollect) {
163  $this->packages = $this->collector->collect();
164  $this->collected = true;
165  }
166  }
167 
174  private function ensurePackagesForRequiredLocales(array $options)
175  {
176  $this->ensureRequiredLocales($options);
177 
178  $resultPackages = $this->packages;
179 
181  foreach ($this->themes as $theme) {
182  $inheritedThemes = $theme->getInheritedThemes();
183  foreach ($resultPackages as $package) {
184  if ($package->getTheme() === Package::BASE_THEME) {
185  continue;
186  }
187  foreach ($inheritedThemes as $inheritedTheme) {
188  if ($package->getTheme() === $inheritedTheme->getThemePath()
189  && $package->getArea() === $inheritedTheme->getArea()
190  ) {
191  $this->ensurePackage([
192  'area' => $package->getArea(),
193  'theme' => $theme->getThemePath(),
194  'locale' => $package->getLocale(),
195  'isVirtual' => $package->getLocale() == Package::BASE_LOCALE
196  ]);
197  }
198  }
199  }
200  }
201  }
202 
209  private function ensureRequiredLocales(array $options)
210  {
211  if (empty($options[Options::LANGUAGE]) || $options[Options::LANGUAGE][0] === 'all') {
212  $forcedLocales = [AppInterface::DISTRO_LOCALE_CODE];
213  } else {
214  $forcedLocales = $options[Options::LANGUAGE];
215  }
216 
217  $resultPackages = $this->packages;
218  foreach ($resultPackages as $package) {
219  if ($package->getTheme() === Package::BASE_THEME) {
220  continue;
221  }
222  foreach ($forcedLocales as $locale) {
223  $this->ensurePackage([
224  'area' => $package->getArea(),
225  'theme' => $package->getTheme(),
226  'locale' => $locale
227  ]);
228  }
229  }
230  }
231 
239  private function checkPackageSkip(Package $package, array $options)
240  {
241  return !$this->canDeployArea($package, $options)
242  || !$this->canDeployTheme($package, $options)
243  || !$this->canDeployLocale($package, $options);
244  }
245 
251  private function canDeployArea(Package $package, array $options)
252  {
253  $area = $package->getArea();
254 
255  if ($area == 'install') {
256  return false;
257  }
258  if ($area == Package::BASE_AREA) {
259  return true;
260  }
261  $exclude = $this->getOption(Options::EXCLUDE_AREA, $options);
262  $include = $this->getOption(Options::AREA, $options);
263  return $this->isIncluded($area, $include, $exclude);
264  }
265 
271  private function canDeployTheme(Package $package, array $options)
272  {
273  $theme = $package->getTheme();
274 
275  if ($theme == Package::BASE_THEME) {
276  return true;
277  }
278  $exclude = $this->getOption(Options::EXCLUDE_THEME, $options);
279  $include = $this->getOption(Options::THEME, $options);
280  return $this->isIncluded($theme, $include, $exclude);
281  }
282 
288  private function canDeployLocale(Package $package, array $options)
289  {
290  $locale = $package->getLocale();
291  if ($locale == Package::BASE_LOCALE) {
292  return true;
293  }
294  $exclude = $this->getOption(Options::EXCLUDE_LANGUAGE, $options);
295  $include = $this->getOption(Options::LANGUAGE, $options);
296  return $this->isIncluded($locale, $include, $exclude);
297  }
298 
305  private function isIncluded($entity, array $includedEntities, array $excludedEntities)
306  {
307  $result = true;
308  if ($includedEntities[0] === 'all' && $excludedEntities[0] === 'none') {
309  $result = true;
310  } elseif ($excludedEntities[0] !== 'none') {
311  $result = !in_array($entity, $excludedEntities);
312  } elseif ($includedEntities[0] !== 'all') {
313  $result = in_array($entity, $includedEntities);
314  }
315  return $result;
316  }
317 
323  private function getOption($name, $options)
324  {
325  return isset($options[$name]) ? $options[$name] : null;
326  }
327 
332  private function ensurePackage(array $params)
333  {
334  $packagePath = "{$params['area']}/{$params['theme']}/{$params['locale']}";
335  if (!isset($this->packages[$packagePath])) {
336  $this->packages[$packagePath] = $this->packageFactory->create($params);
337  }
338  }
339 }
__construct(Collector $collector, ListInterface $themeCollection, PackageFactory $packageFactory)
Definition: PackagePool.php:51
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getPackagesForDeployment(array $options)
Definition: PackagePool.php:99
$entity
Definition: element.phtml:22
$theme
getThemeModel($areaCode, $themePath)
Definition: PackagePool.php:86
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
if(!isset($_GET['name'])) $name
Definition: log.php:14