Skip to content

Commit 98f808d

Browse files
authored
Merge pull request #11 from magefan/11052-speed-optimization-extension-v2
11052 speed optimization extension v2
2 parents 2828249 + 095ccff commit 98f808d

File tree

11 files changed

+933
-65
lines changed

11 files changed

+933
-65
lines changed

Model/Config.php

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Magefan\RocketJavaScript\Model;
12+
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Framework\View\Asset\Config as MagentoConfig;
16+
17+
class Config
18+
{
19+
/**
20+
* General config
21+
*/
22+
public const XML_PATH_EXTENSION_ENABLED = 'mfrocketjavascript/general/enabled';
23+
public const XML_PATH_MERGE_FILES = 'mfrocketjavascript/general/merge_files';
24+
public const XML_PATH_MINIFY_FILES = 'mfrocketjavascript/general/minify_files';
25+
26+
/**
27+
* Deferred JavaScript config
28+
*/
29+
public const XML_PATH_DEFERRED_ENABLED = 'mfrocketjavascript/deferred_javascript/enabled';
30+
public const XML_PATH_DEFERRED_DISALLOWED_PAGES = 'mfrocketjavascript/deferred_javascript/disallowed_pages_for_deferred_js';
31+
public const XML_PATH_DEFERRED_IGNORE_JAVASCRIPT = 'mfrocketjavascript/deferred_javascript/ignore_deferred_javascript_with';
32+
33+
/**
34+
* JavaScript Bundling config
35+
*/
36+
public const XML_PATH_JAVASCRIPT_BUNDLING_ENABLED = 'mfrocketjavascript/javascript_bundling/enabled';
37+
public const XML_PATH_JAVASCRIPT_BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/javascript_bundling/enable_js_bundling_optimization';
38+
public const XML_PATH_JAVASCRIPT_BUNDLING_INCLUDED_IN_BUNDLING = 'mfrocketjavascript/javascript_bundling/included_in_bundling';
39+
40+
/**
41+
* Plumrocket AMP config
42+
*/
43+
public const XML_PATH_PLUMROCKET_AMP_ENABLED = 'pramp/general/enabled';
44+
45+
/**
46+
* @var ScopeConfigInterface
47+
*/
48+
private $scopeConfig;
49+
50+
/**
51+
* Config constructor.
52+
*
53+
* @param ScopeConfigInterface $scopeConfig
54+
*/
55+
public function __construct(
56+
ScopeConfigInterface $scopeConfig
57+
) {
58+
$this->scopeConfig = $scopeConfig;
59+
}
60+
61+
/**
62+
* Retrieve true if module is enabled
63+
*
64+
* @param string|null $storeId
65+
* @return bool
66+
*/
67+
public function isEnabled(string $storeId = null): bool
68+
{
69+
return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId);
70+
}
71+
72+
/**
73+
* Retrieve true if deferred is enabled
74+
*
75+
* @param string|null $storeId
76+
* @return bool
77+
*/
78+
public function isDeferredEnabled(string $storeId = null): bool
79+
{
80+
return (bool)$this->getConfig(self::XML_PATH_DEFERRED_ENABLED, $storeId);
81+
}
82+
83+
/**
84+
* Retrieve Disallowed Pages
85+
*
86+
* @param string|null $storeId
87+
* @return string
88+
*/
89+
public function getDisallowedPages(string $storeId = null): string
90+
{
91+
return (string)$this->getConfig(self::XML_PATH_DEFERRED_DISALLOWED_PAGES, $storeId);
92+
}
93+
94+
/**
95+
* Retrieve Ignore JS
96+
*
97+
* @param string|null $storeId
98+
* @return string
99+
*/
100+
public function getIgnoreJavaScript(string $storeId = null): string
101+
{
102+
return (string)$this->getConfig(self::XML_PATH_DEFERRED_IGNORE_JAVASCRIPT, $storeId);
103+
}
104+
105+
/**
106+
* Retrieve true if JS bundling is enabled
107+
*
108+
* @param string|null $storeId
109+
* @return bool
110+
*/
111+
public function isBundlingEnabled(string $storeId = null): bool
112+
{
113+
return (bool)$this->getConfig(MagentoConfig::XML_PATH_JS_BUNDLING, $storeId);
114+
}
115+
116+
/**
117+
* Retrieve true if bundling optimization is enabled
118+
*
119+
* @param string|null $storeId
120+
* @return bool
121+
*/
122+
public function isBundlingOptimizationEnabled(string $storeId = null): bool
123+
{
124+
return (bool)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_OPTIMIZATION_ENABLED, $storeId);
125+
}
126+
127+
/**
128+
* Retrieve included in bundling JS
129+
*
130+
* @param string|null $storeId
131+
* @return string
132+
*/
133+
public function getIncludedInBundling(string $storeId = null): string
134+
{
135+
return (string)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_INCLUDED_IN_BUNDLING, $storeId);
136+
}
137+
138+
/**
139+
* Retrieve true if amp enabled
140+
*
141+
* @param string|null $storeId
142+
* @return bool
143+
*/
144+
public function isAmpRequest(string $storeId = null): bool
145+
{
146+
return (bool)$this->getConfig(self::XML_PATH_PLUMROCKET_AMP_ENABLED, $storeId);
147+
}
148+
149+
/**
150+
* Retrieve store config value
151+
*
152+
* @param string $path
153+
* @param string|null $storeId
154+
* @return mixed
155+
*/
156+
public function getConfig(string $path, string $storeId = null)
157+
{
158+
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
159+
}
160+
}

Model/Config/Backend/DevSettings.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Magefan\RocketJavaScript\Model\Config\Backend;
12+
13+
use Magento\Framework\App\Config\Value;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
16+
use Magento\Framework\App\RequestInterface;
17+
use Magento\Framework\Model\Context;
18+
use Magento\Framework\Registry;
19+
use Magento\Framework\App\Cache\TypeListInterface;
20+
use Magento\Framework\Model\ResourceModel\AbstractResource;
21+
use Magento\Framework\Data\Collection\AbstractDb;
22+
23+
class DevSettings extends Value
24+
{
25+
const KEY_VALUE_RELATION = [
26+
'mfrocketjavascript/general/merge_files' => 'dev/js/merge_files',
27+
'mfrocketjavascript/general/minify_files' => 'dev/js/minify_files',
28+
'mfrocketjavascript/javascript_bundling/enabled' => 'dev/js/enable_js_bundling',
29+
];
30+
31+
/**
32+
* @var RequestInterface
33+
*/
34+
private $request;
35+
36+
/**
37+
* DevSettings constructor.
38+
* @param Context $context
39+
* @param Registry $registry
40+
* @param ScopeConfigInterface $config
41+
* @param TypeListInterface $cacheTypeList
42+
* @param RequestInterface $request
43+
* @param AbstractResource|null $resource
44+
* @param AbstractDb|null $resourceCollection
45+
* @param array $data
46+
*/
47+
public function __construct(
48+
Context $context,
49+
Registry $registry,
50+
ScopeConfigInterface $config,
51+
TypeListInterface $cacheTypeList,
52+
RequestInterface $request,
53+
AbstractResource $resource = null,
54+
AbstractDb $resourceCollection = null,
55+
array $data = []
56+
) {
57+
$this->request = $request;
58+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
59+
}
60+
61+
/**
62+
* @return DevSettings|void
63+
*/
64+
public function afterLoad()
65+
{
66+
$scopeTypes = [
67+
StoreScopeInterface::SCOPE_WEBSITES,
68+
StoreScopeInterface::SCOPE_WEBSITE,
69+
StoreScopeInterface::SCOPE_STORES,
70+
StoreScopeInterface::SCOPE_STORE
71+
];
72+
73+
$scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
74+
$scopeCode = null;
75+
76+
foreach ($scopeTypes as $scope) {
77+
$param = $this->request->getParam($scope);
78+
if ($param) {
79+
$scopeType = $scope;
80+
$scopeCode = $param;
81+
}
82+
}
83+
84+
$devSettingsValue = $this->_config->getValue(
85+
self::KEY_VALUE_RELATION[$this->getData('path')],
86+
$scopeType,
87+
$scopeCode
88+
);
89+
$this->setData('value', $devSettingsValue);
90+
}
91+
}

Model/Controller/ResultPlugin.php

+12-27
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ class ResultPlugin
2424
protected $request;
2525

2626
/**
27-
* Core store config
28-
*
29-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
27+
* @var \Magefan\RocketJavaScript\Model\Config
3028
*/
31-
protected $scopeConfig;
29+
protected $config;
3230

3331
/**
3432
* @var bool
@@ -41,17 +39,18 @@ class ResultPlugin
4139
protected $storeManager;
4240

4341
/**
44-
* @param \Magento\Framework\App\RequestInterface $request
45-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
42+
* ResultPlugin constructor.
43+
* @param \Magento\Framework\App\RequestInterface $request
44+
* @param \Magefan\RocketJavaScript\Model\Config $config
4645
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
4746
*/
4847
public function __construct(
4948
\Magento\Framework\App\RequestInterface $request,
50-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
49+
\Magefan\RocketJavaScript\Model\Config $config,
5150
\Magento\Store\Model\StoreManagerInterface $storeManager = null
5251
) {
5352
$this->request = $request;
54-
$this->scopeConfig = $scopeConfig;
53+
$this->config = $config;
5554

5655
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
5756
$this->storeManager = $storeManager ?: $objectManager->get(
@@ -80,9 +79,7 @@ public function aroundRenderResult(
8079
return $result;
8180
}
8281

83-
$ignoredStrings = $this->scopeConfig->getValue(
84-
'mfrocketjavascript/general/ignore_deferred_javascript_with',
85-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE) ?: '';
82+
$ignoredStrings = $this->config->getIgnoreJavaScript() ?: '';
8683
$ignoredStrings = explode("\n", str_replace("\r", "\n", $ignoredStrings));
8784
foreach ($ignoredStrings as $key => $ignoredString) {
8885
$ignoredString = trim($ignoredString);
@@ -146,15 +143,9 @@ public function aroundRenderResult(
146143

147144
private function isEnabled()
148145
{
149-
$enabled = $this->scopeConfig->getValue(
150-
'mfrocketjavascript/general/enabled',
151-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
152-
) && $this->scopeConfig->getValue(
153-
'mfrocketjavascript/general/enable_deferred_javascript',
154-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
155-
);
146+
$enabled = $this->config->isEnabled() && $this->config->isDeferredEnabled();
147+
156148

157-
158149
if ($enabled) {
159150

160151
/* check if Amasty AMP enabled */
@@ -163,10 +154,7 @@ private function isEnabled()
163154
}
164155

165156
/* check if Plumrocket AMP enabled */
166-
$isAmpRequest = $this->scopeConfig->getValue(
167-
'pramp/general/enabled',
168-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
169-
);
157+
$isAmpRequest = $this->config->isAmpRequest();
170158

171159
if ($isAmpRequest) {
172160
/* We know that using objectManager is not a not a good practice,
@@ -193,10 +181,7 @@ private function isAllowedOnPage()
193181
}
194182
$this->allowedOnPage = false;
195183

196-
$spPages = $this->scopeConfig->getValue(
197-
'mfrocketjavascript/general/disallowed_pages_for_deferred_js',
198-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
199-
);
184+
$spPages = $this->config->getDisallowedPages();
200185
$spPages = explode("\n", str_replace("\r", "\n", $spPages));
201186

202187
foreach ($spPages as $key => $path) {

0 commit comments

Comments
 (0)