forked from magento/ece-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThemeResolver.php
96 lines (87 loc) · 2.84 KB
/
ThemeResolver.php
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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MagentoCloud\StaticContent;
use Magento\MagentoCloud\Filesystem\DirectoryList;
use Psr\Log\LoggerInterface;
use Magento\MagentoCloud\Filesystem\Driver\File;
/**
* Resolves themes to their correct names
*/
class ThemeResolver
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var string[]
*/
private $themes;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Takes in name of a theme, compares it against the names and corrects if necessary.
*
* @param string $themeName
* @return string
*/
public function resolve(string $themeName): string
{
$availableThemes = $this->getThemes();
if (!in_array($themeName, $availableThemes)) {
$this->logger->warning('Theme ' . $themeName . ' does not exist, attempting to resolve.');
$themeNamePosition = array_search(
strtolower($themeName),
array_map('strtolower', $availableThemes)
);
if (false !== $themeNamePosition) {
$this->logger->warning(
'Theme found as ' . $availableThemes[$themeNamePosition] . '. Using corrected name instead.'
);
return $availableThemes[$themeNamePosition];
} else {
$this->logger->error('Unable to resolve theme.');
return '';
}
}
return $themeName;
}
/**
* @return array
* @codeCoverageIgnore
* @throws \ReflectionException
*/
protected function getThemes(): array
{
$this->logger->debug('Finding available themes.');
if (empty($this->themes)) {
if (class_exists(\Magento\Framework\Component\ComponentRegistrar::class)) {
$reflectionClass = new \ReflectionClass(\Magento\Framework\Component\ComponentRegistrar::class);
$property = $reflectionClass->getProperty('paths');
$property->setAccessible(true);
$this->themes = array_keys(
$property->getValue($reflectionClass)[\Magento\Framework\Component\ComponentRegistrar::THEME]
);
foreach ($this->themes as &$aTheme) {
$aTheme = substr(
$aTheme,
strpos($aTheme, '/') + 1
);
}
} else {
$this->logger->warning('Unable to find themes, cannot find Magento class.');
}
}
$this->logger->debug('End of finding available themes.');
return $this->themes;
}
}