InitialThemeSource.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Source;
  7. use Magento\Framework\App\Config\ConfigSourceInterface;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\DataObject\Factory as DataObjectFactory;
  10. use Magento\Theme\Model\ResourceModel\Theme;
  11. use Magento\Theme\Model\ResourceModel\ThemeFactory;
  12. /**
  13. * Class InitialThemeSource.
  14. *
  15. * Retrieves theme configurations by path.
  16. */
  17. class InitialThemeSource implements ConfigSourceInterface
  18. {
  19. /**
  20. * A deployment config.
  21. *
  22. * @var DeploymentConfig
  23. */
  24. private $deploymentConfig;
  25. /**
  26. * A theme factory.
  27. *
  28. * @var ThemeFactory
  29. */
  30. private $themeFactory;
  31. /**
  32. * A data object factory.
  33. *
  34. * @var DataObjectFactory
  35. */
  36. private $dataObjectFactory;
  37. /**
  38. * Array with theme data.
  39. *
  40. * @var array
  41. */
  42. private $data;
  43. /**
  44. * @param DeploymentConfig $deploymentConfig A deployment config
  45. * @param ThemeFactory $themeFactory A theme factory
  46. * @param DataObjectFactory $dataObjectFactory A data object factory
  47. */
  48. public function __construct(
  49. DeploymentConfig $deploymentConfig,
  50. ThemeFactory $themeFactory,
  51. DataObjectFactory $dataObjectFactory
  52. ) {
  53. $this->deploymentConfig = $deploymentConfig;
  54. $this->themeFactory = $themeFactory;
  55. $this->dataObjectFactory = $dataObjectFactory;
  56. }
  57. /**
  58. * Retrieves configuration data array.
  59. * Example:
  60. *
  61. * ```php
  62. * ['adminhtml/Magento/backend' =>
  63. * [
  64. * 'parent_id' => NULL,
  65. * 'theme_path' => 'Magento/backend',
  66. * 'theme_title' => 'Magento 2 backend',
  67. * 'is_featured' => '0',
  68. * 'area' => 'adminhtml',
  69. * 'type' => '0',
  70. * 'code' => 'Magento/backend',
  71. * ]
  72. * ]
  73. * ```
  74. *
  75. * @param string $path The path to theme configuration.
  76. * @return array The data array with theme configurations.
  77. */
  78. public function get($path = '')
  79. {
  80. if (!$this->deploymentConfig->isDbAvailable()) {
  81. return [];
  82. }
  83. if (!$this->data) {
  84. $rawThemes = $this->fetchThemes();
  85. $themes = [];
  86. foreach ($rawThemes as $themeRow) {
  87. unset($themeRow['theme_id'], $themeRow['preview_image']);
  88. $themePath = $themeRow['area'] . '/' . $themeRow['theme_path'];
  89. $themes[$themePath] = $themeRow;
  90. if (isset($rawThemes[$themeRow['parent_id']]['code'])) {
  91. $themes[$themePath]['parent_id'] = $rawThemes[$themeRow['parent_id']]['code'];
  92. }
  93. }
  94. $this->data = $this->dataObjectFactory->create($themes);
  95. }
  96. return $this->data->getData($path) ?: [];
  97. }
  98. /**
  99. * Fetches themes from data source.
  100. *
  101. * @return array An associative list with found themes
  102. */
  103. private function fetchThemes()
  104. {
  105. /** @var Theme $theme */
  106. $theme = $this->themeFactory->create();
  107. $select = $theme->getConnection()->select()
  108. ->from($theme->getMainTable())
  109. ->order('theme_id');
  110. return $theme->getConnection()->fetchAssoc($select);
  111. }
  112. }