ThemeValidator.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model;
  7. use Magento\Framework\App\Config\ValueInterface;
  8. use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
  9. use Magento\Framework\View\DesignInterface;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. /**
  13. * Class ThemeValidator
  14. */
  15. class ThemeValidator
  16. {
  17. /**
  18. * Store Manager
  19. *
  20. * @var StoreManagerInterface $storeManager
  21. */
  22. private $storeManager;
  23. /**
  24. * Provider for themes registered in db
  25. *
  26. * @var ThemeProviderInterface $themeProvider
  27. */
  28. private $themeProvider;
  29. /**
  30. * Configuration Data
  31. *
  32. * @var ValueInterface $configData
  33. */
  34. private $configData;
  35. /**
  36. * @param StoreManagerInterface $storeManager
  37. * @param ThemeProviderInterface $themeProvider
  38. * @param ValueInterface $configData
  39. */
  40. public function __construct(
  41. StoreManagerInterface $storeManager,
  42. ThemeProviderInterface $themeProvider,
  43. ValueInterface $configData
  44. ) {
  45. $this->storeManager = $storeManager;
  46. $this->themeProvider = $themeProvider;
  47. $this->configData = $configData;
  48. }
  49. /**
  50. * Validate the theme if being in use in default, website, or store.
  51. *
  52. * @param string[] $themePaths
  53. * @return array
  54. */
  55. public function validateIsThemeInUse($themePaths)
  56. {
  57. $messages = [];
  58. $themesById = [];
  59. foreach ($themePaths as $themePath) {
  60. $theme = $this->themeProvider->getThemeByFullPath($themePath);
  61. $themesById[$theme->getId()] = $themePath;
  62. }
  63. $configData = $this->configData
  64. ->getCollection()
  65. ->addFieldToFilter('path', DesignInterface::XML_PATH_THEME_ID)
  66. ->addFieldToFilter('value', ['in' => array_keys($themesById)]);
  67. foreach ($configData as $row) {
  68. switch ($row['scope']) {
  69. case 'default':
  70. $messages[] = '<error>' . $themesById[$row['value']] . ' is in use in default config' . '</error>';
  71. break;
  72. case ScopeInterface::SCOPE_WEBSITES:
  73. $messages[] = '<error>' . $themesById[$row['value']] . ' is in use in website '
  74. . $this->storeManager->getWebsite($row['scope_id'])->getName() . '</error>';
  75. break;
  76. case ScopeInterface::SCOPE_STORES:
  77. $messages[] = '<error>' . $themesById[$row['value']] . ' is in use in store '
  78. . $this->storeManager->getStore($row['scope_id'])->getName() . '</error>';
  79. break;
  80. }
  81. }
  82. return $messages;
  83. }
  84. }