ThemeDependencyChecker.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Theme;
  7. use Magento\Theme\Model\Theme\Data\Collection as ThemeCollection;
  8. /**
  9. * Class checks theme dependencies
  10. */
  11. class ThemeDependencyChecker
  12. {
  13. /**
  14. * Theme Collection
  15. *
  16. * @var ThemeCollection
  17. */
  18. private $themeCollection;
  19. /**
  20. * Provider for themes registered in db
  21. *
  22. * @var ThemeProvider
  23. */
  24. private $themeProvider;
  25. /**
  26. * Package name finder
  27. *
  28. * @var ThemePackageInfo
  29. */
  30. private $themePackageInfo;
  31. /**
  32. * Constructor
  33. *
  34. * @param ThemeCollection $themeCollection
  35. * @param ThemeProvider $themeProvider
  36. * @param ThemePackageInfo $themePackageInfo,
  37. */
  38. public function __construct(
  39. ThemeCollection $themeCollection,
  40. ThemeProvider $themeProvider,
  41. ThemePackageInfo $themePackageInfo
  42. ) {
  43. $this->themeCollection = $themeCollection;
  44. $this->themeProvider = $themeProvider;
  45. $this->themePackageInfo = $themePackageInfo;
  46. }
  47. /**
  48. * Check theme by package name(s) if has child virtual and physical theme
  49. *
  50. * @param string[] $packages
  51. * @return string[]
  52. */
  53. public function checkChildThemeByPackagesName($packages)
  54. {
  55. $themePaths = [];
  56. foreach ($packages as $package) {
  57. $themePath = $this->themePackageInfo->getFullThemePath($package);
  58. if ($themePath) {
  59. $themePaths[] = $themePath;
  60. }
  61. }
  62. if ($themePaths) {
  63. return $this->checkChildTheme($themePaths);
  64. }
  65. return [];
  66. }
  67. /**
  68. * Check theme if has child virtual and physical theme
  69. *
  70. * @param string[] $themePaths
  71. * @return string[]
  72. */
  73. public function checkChildTheme($themePaths)
  74. {
  75. $messages = [];
  76. $themeHasVirtualChildren = [];
  77. $themeHasPhysicalChildren = [];
  78. $parentChildMap = $this->getParentChildThemeMap();
  79. foreach ($themePaths as $themePath) {
  80. $theme = $this->themeProvider->getThemeByFullPath($themePath);
  81. if ($theme->hasChildThemes()) {
  82. $themeHasVirtualChildren[] = $themePath;
  83. }
  84. if (isset($parentChildMap[$themePath])) {
  85. $themeHasPhysicalChildren[] = $themePath;
  86. }
  87. }
  88. if (!empty($themeHasVirtualChildren)) {
  89. $text = count($themeHasVirtualChildren) > 1 ? ' are parents of' : ' is a parent of';
  90. $messages[] = implode(', ', $themeHasVirtualChildren) . $text . ' virtual theme.'
  91. . ' Parent themes cannot be uninstalled.';
  92. }
  93. if (!empty($themeHasPhysicalChildren)) {
  94. $text = count($themeHasPhysicalChildren) > 1 ? ' are parents of' : ' is a parent of';
  95. $messages[] = implode(', ', $themeHasPhysicalChildren) . $text . ' physical theme.'
  96. . ' Parent themes cannot be uninstalled.';
  97. }
  98. return $messages;
  99. }
  100. /**
  101. * Obtain a parent theme -> children themes map from the filesystem
  102. *
  103. * @return array
  104. */
  105. private function getParentChildThemeMap()
  106. {
  107. $map = [];
  108. $this->themeCollection->resetConstraints();
  109. $this->themeCollection->clear();
  110. /** @var \Magento\Theme\Model\Theme\Data $theme */
  111. foreach ($this->themeCollection as $theme) {
  112. if ($theme->getParentTheme()) {
  113. $map[$theme->getParentTheme()->getFullPath()][] = $theme->getFullPath();
  114. }
  115. }
  116. return $map;
  117. }
  118. }