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