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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Theme\Model\Theme\Plugin;
use Magento\Backend\App\AbstractAction;
use Magento\Framework\App\RequestInterface;
use Magento\Theme\Model\Theme\Registration as ThemeRegistration;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\State as AppState;
use Magento\Theme\Model\Theme\Collection as ThemeCollection;
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeLoader;
use Magento\Framework\Config\Theme;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Registration
{
/**
* @var \Magento\Theme\Model\Theme\Registration
*/
protected $themeRegistration;
/**
* @var \Magento\Theme\Model\Theme\Collection
*/
protected $themeCollection;
/**
* @var \Magento\Theme\Model\ResourceModel\Theme\Collection
*/
protected $themeLoader;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* @var \Magento\Framework\App\State
*/
protected $appState;
/**
* @param ThemeRegistration $themeRegistration
* @param ThemeCollection $themeCollection
* @param ThemeLoader $themeLoader
* @param LoggerInterface $logger
* @param AppState $appState
*/
public function __construct(
ThemeRegistration $themeRegistration,
ThemeCollection $themeCollection,
ThemeLoader $themeLoader,
LoggerInterface $logger,
AppState $appState
) {
$this->themeRegistration = $themeRegistration;
$this->themeCollection = $themeCollection;
$this->themeLoader = $themeLoader;
$this->logger = $logger;
$this->appState = $appState;
}
/**
* Add new theme from filesystem and update existing
*
* @param AbstractAction $subject
* @param RequestInterface $request
*
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeDispatch(
AbstractAction $subject,
RequestInterface $request
) {
try {
if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
$this->themeRegistration->register();
$this->updateThemeData();
}
} catch (LocalizedException $e) {
$this->logger->critical($e);
}
}
/**
* Update theme data
*
* @return void
*/
protected function updateThemeData()
{
$themesFromConfig = $this->themeCollection->loadData();
/** @var \Magento\Theme\Model\Theme $themeFromConfig */
foreach ($themesFromConfig as $themeFromConfig) {
/** @var \Magento\Theme\Model\Theme $themeFromDb */
$themeFromDb = $this->themeLoader->getThemeByFullPath(
$themeFromConfig->getArea()
. Theme::THEME_PATH_SEPARATOR
. $themeFromConfig->getThemePath()
);
if ($themeFromConfig->getParentTheme()) {
$parentThemeFromDb = $this->themeLoader->getThemeByFullPath(
$themeFromConfig->getParentTheme()->getFullPath()
);
$themeFromDb->setParentId($parentThemeFromDb->getId());
}
$themeFromDb->setThemeTitle($themeFromConfig->getThemeTitle());
$themeFromDb->save();
}
}
}