DesignTheme.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Config\Processor;
  7. use Magento\Framework\App\Config\Spi\PreProcessorInterface;
  8. use Magento\Framework\Stdlib\ArrayManager;
  9. use Magento\Framework\View\Design\Theme\ListInterface;
  10. use Magento\Framework\View\DesignInterface;
  11. /**
  12. * Allows to convert configurations from \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID variables.
  13. *
  14. * Detects the design theme configuration data (path \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID)
  15. * and convert theme identifier from theme_full_path (Ex. "frontend/Magento/blank") to theme_id.
  16. */
  17. class DesignTheme implements PreProcessorInterface
  18. {
  19. /**
  20. * @var ArrayManager
  21. */
  22. private $arrayManager;
  23. /**
  24. * @var ListInterface
  25. */
  26. private $themeList;
  27. /**
  28. * @param ArrayManager $arrayManager
  29. * @param ListInterface $themeList
  30. */
  31. public function __construct(
  32. ArrayManager $arrayManager,
  33. ListInterface $themeList
  34. ) {
  35. $this->arrayManager = $arrayManager;
  36. $this->themeList = $themeList;
  37. }
  38. /**
  39. * Change value from theme_full_path (Ex. "frontend/Magento/blank") to theme_id field for every existed scope.
  40. * All other values leave without changes.
  41. *
  42. * @param array $config
  43. * @return array
  44. */
  45. public function process(array $config)
  46. {
  47. foreach ($config as $scope => &$item) {
  48. if ($scope === \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
  49. $item = $this->changeThemeFullPathToIdentifier($item);
  50. } else {
  51. foreach ($item as &$scopeItems) {
  52. $scopeItems = $this->changeThemeFullPathToIdentifier($scopeItems);
  53. }
  54. }
  55. }
  56. return $config;
  57. }
  58. /**
  59. * Check \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID config path
  60. * and convert theme_full_path (Ex. "frontend/Magento/blank") to theme_id
  61. *
  62. * @param array $configItems
  63. * @return array
  64. */
  65. private function changeThemeFullPathToIdentifier($configItems)
  66. {
  67. $theme = null;
  68. $themeIdentifier = $this->arrayManager->get(DesignInterface::XML_PATH_THEME_ID, $configItems);
  69. if (!empty($themeIdentifier)) {
  70. if (!is_numeric($themeIdentifier)) {
  71. // workaround for case when db is not available
  72. try {
  73. $theme = $this->themeList->getThemeByFullPath($themeIdentifier);
  74. } catch (\DomainException $domainException) {
  75. $theme = null;
  76. }
  77. }
  78. if ($theme && $theme->getId()) {
  79. return $this->arrayManager->set(DesignInterface::XML_PATH_THEME_ID, $configItems, $theme->getId());
  80. }
  81. }
  82. return $configItems;
  83. }
  84. }