-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathReactInjectPlugin.php
executable file
·180 lines (163 loc) · 7.83 KB
/
ReactInjectPlugin.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace React\React;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Asset\GroupedCollection;
use Magento\Framework\View\Page\Config;
use Magento\Framework\View\Page\Config\Metadata\MsApplicationTileImage;
use Magento\Framework\View\Page\Config\Renderer;
/**
* Page config Renderer model Plugin
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ReactInjectPlugin extends Renderer
{
/**
* @param Config $pageConfig
* @param \Magento\Framework\View\Asset\MergeService $assetMergeService
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Psr\Log\LoggerInterface $logger
* @param MsApplicationTileImage|null $msApplicationTileImage
*/
public function __construct(
Config $pageConfig,
\Magento\Framework\View\Asset\MergeService $assetMergeService,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Framework\Escaper $escaper,
\Magento\Framework\Stdlib\StringUtils $string,
\Psr\Log\LoggerInterface $logger,
MsApplicationTileImage $msApplicationTileImage = null
) {
parent::__construct($pageConfig, $assetMergeService, $urlBuilder, $escaper, $string, $logger, $msApplicationTileImage);
}
/**
* Render HTML tags referencing corresponding URLs
*
* @param \Magento\Framework\View\Asset\PropertyGroup $group
* @return string
*/
protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $group)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$config = $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$reactEnabled = boolval($config->getValue('react_vue_config/react/enable'));
$vueEnabled = boolval($config->getValue('react_vue_config/vue/enable'));
/* remove default magento Junky JS */
$removeAdobeJSJunk = boolval($config->getValue('react_vue_config/junk/remove'));
$removeCSSjunk = boolval($config->getValue('react_vue_config/css/remove'));
$state = $objectManager->get('Magento\Framework\App\State');
$store = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$area = $state->getAreaCode();
$pageFilter = ['checkout', 'customer'];
$actionFilter = ['catalog_category_view',
'cms_index_index',
'cms_page_view',
'catalog_product_view',
'catalogsearch_result_index'];
$request = $objectManager->get(\Magento\Framework\App\Request\Http::class);
$actionName = $request->getFullActionName();
@header("Action-Name: $actionName");
$requestURL = $_SERVER['REQUEST_URI'];
$removeProtection = boolval(boolval(strpos($requestURL, 'checkout')) || boolval(strpos($requestURL, 'customer')) || $area === 'adminhtml');
@header("React-Protection: $removeProtection");
$block = $objectManager->get(\Magento\Framework\View\Element\Template::class);
$assets = $this->processMerge($group->getAll(), $group);
$attributes = $this->getGroupAttributes($group);
$result = '';
$template = '';
$assetOptimized = false;
$assetOptimized2 = false;
try {
/** @var $asset \Magento\Framework\View\Asset\AssetInterface */
//Changes Start
$baseURL = $store->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
foreach ($assets as $key => $asset) {
if (in_array($actionName, $actionFilter) && strpos($asset->getUrl(), 'styles-m')) {
// http://**/static/version1642788857/frontend/Magento/luma/en_US/css/styles-m.css
$optimisedCSSFileUrl = $baseURL . 'styles-m.css';
$optimisedCSSFilePath = BP . '/pub/static/styles-m.css';
if (file_exists($optimisedCSSFilePath)) {
//echo $optimisedCSSFileUrl;
$assetOptimized = $optimisedCSSFileUrl;
unset($assets[$key]);
} else {
@header("Optimised-CSS: false");
}
}
if (in_array($actionName, $actionFilter) && strpos($asset->getUrl(), 'styles-l')) {
// http://**/static/version1642788857/frontend/Magento/luma/en_US/css/styles-l.css
$optimisedCSSFileUrlLarge = $baseURL . 'styles-l.css';
$optimisedCSSFilePathLarge = BP . '/pub/static/styles-l.css';
if (file_exists($optimisedCSSFilePathLarge)) {
//echo $optimisedCSSFileUrl;
$assetOptimized2 = $optimisedCSSFileUrlLarge;
unset($assets[$key]);
} else {
@header("Optimised-CSS: false");
}
}
}
foreach ($assets as $key => $asset) {
if (strpos($asset->getUrl(), 'js/react')) {
unset($assets[$key]);
if ($reactEnabled) {
array_unshift($assets, $asset);
}
} else if (strpos($asset->getUrl(), 'vue')) {
unset($assets[$key]);
if ($vueEnabled) {
array_unshift($assets, $asset);
}
} else if (strpos($asset->getUrl(), 'require')) {
if ($removeAdobeJSJunk)
//dd($removeAdobeJSJunk);
{
unset($assets[$key]);
}
// junk True ; protection False
// echo "require " . (string) $removeProtection;
if (!$removeAdobeJSJunk || !in_array($actionName, $actionFilter));
array_unshift($assets, $asset);
} else if (strpos($asset->getUrl(), 'styles-')) {
unset($assets[$key]);
if (!$removeCSSjunk || !in_array($actionName, $actionFilter)) {
array_unshift($assets, $asset);
}
}
}
//we need execute it one more time to make scripts the same order
foreach ($assets as $key => $asset) {
if (strpos($asset->getUrl(), 'require')) {
unset($assets[$key]);
array_unshift($assets, $asset);
}
}
foreach ($assets as $key => $asset) {
if (strpos($asset->getUrl(), 'js/react') || strpos($asset->getUrl(), 'vue')) {
unset($assets[$key]);
array_unshift($assets, $asset);
}
}
//Changes Ends
foreach ($assets as $asset) {
$template = $this->getAssetTemplate(
$group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE),
$this->addDefaultAttributes($this->getAssetContentType($asset), $attributes)
);
$result .= sprintf($template, $asset->getUrl());
}
} catch (LocalizedException $e) {
$this->logger->critical($e);
$result .= sprintf($template, $this->urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']));
}
if ($assetOptimized !== false) {
$result = '<link rel="stylesheet" type="text/css" media="all" href="' . $assetOptimized . '" />' . "\n" . $result;
}
if ($assetOptimized2 !== false) {
$result = '<link rel="stylesheet" type="text/css" media="screen and (min-width: 768px)" href="' . $assetOptimized2 . '" />' . "\n" . $result;
}
return $result;
}
}