Skip to content

Commit 0503db9

Browse files
author
Yuriy
committedJul 24, 2024
11052-add-config
·
2.2.22.2.0
1 parent e45af36 commit 0503db9

File tree

3 files changed

+203
-50
lines changed

3 files changed

+203
-50
lines changed
 

‎Model/Config.php

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
16+
class Config
17+
{
18+
/**
19+
* General config
20+
*/
21+
public const XML_PATH_EXTENSION_ENABLED = 'mfrocketjavascript/general/enabled';
22+
public const XML_PATH_MERGE_FILES = 'mfrocketjavascript/general/merge_files';
23+
public const XML_PATH_MINIFY_FILES = 'mfrocketjavascript/general/minify_files';
24+
25+
/**
26+
* Deferred JavaScript config
27+
*/
28+
public const XML_PATH_DEFERRED_ENABLED = 'mfrocketjavascript/deferred_javascript/enabled';
29+
public const XML_PATH_DEFERRED_DISALLOWED_PAGES = 'mfrocketjavascript/deferred_javascript/disallowed_pages_for_deferred_js';
30+
public const XML_PATH_DEFERRED_IGNORE_JAVASCRIPT = 'mfrocketjavascript/deferred_javascript/ignore_deferred_javascript_with';
31+
32+
/**
33+
* JavaScript Bundling config
34+
*/
35+
public const XML_PATH_JAVASCRIPT_BUNDLING_ENABLED = 'mfrocketjavascript/javascript_bundling/enabled';
36+
public const XML_PATH_JAVASCRIPT_BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/javascript_bundling/enable_js_bundling_optimization';
37+
public const XML_PATH_JAVASCRIPT_BUNDLING_INCLUDED_IN_BUNDLING = 'mfrocketjavascript/javascript_bundling/included_in_bundling';
38+
39+
/**
40+
* Plumrocket AMP config
41+
*/
42+
public const XML_PATH_PLUMROCKET_AMP_ENABLED = 'pramp/general/enabled';
43+
44+
/**
45+
* @var ScopeConfigInterface
46+
*/
47+
private $scopeConfig;
48+
49+
/**
50+
* Config constructor.
51+
*
52+
* @param ScopeConfigInterface $scopeConfig
53+
*/
54+
public function __construct(
55+
ScopeConfigInterface $scopeConfig
56+
) {
57+
$this->scopeConfig = $scopeConfig;
58+
}
59+
60+
/**
61+
* Retrieve true if module is enabled
62+
*
63+
* @param string|null $storeId
64+
* @return bool
65+
*/
66+
public function isEnabled(string $storeId = null): bool
67+
{
68+
return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId);
69+
}
70+
71+
/**
72+
* Retrieve true if js files merged
73+
*
74+
* @param string|null $storeId
75+
* @return bool
76+
*/
77+
public function isMergeFiles(string $storeId = null): bool
78+
{
79+
return (bool)$this->getConfig(self::XML_PATH_MERGE_FILES, $storeId);
80+
}
81+
82+
/**
83+
* Retrieve true if js files minified
84+
*
85+
* @param string|null $storeId
86+
* @return bool
87+
*/
88+
public function isMinifyFiles(string $storeId = null): bool
89+
{
90+
return (bool)$this->getConfig(self::XML_PATH_MINIFY_FILES, $storeId);
91+
}
92+
93+
/**
94+
* Retrieve true if deferred is enabled
95+
*
96+
* @param string|null $storeId
97+
* @return bool
98+
*/
99+
public function isDeferredEnabled(string $storeId = null): bool
100+
{
101+
return (bool)$this->getConfig(self::XML_PATH_DEFERRED_ENABLED, $storeId);
102+
}
103+
104+
/**
105+
* Retrieve Disallowed Pages
106+
*
107+
* @param string|null $storeId
108+
* @return string
109+
*/
110+
public function getDisallowedPages(string $storeId = null): string
111+
{
112+
return (string)$this->getConfig(self::XML_PATH_DEFERRED_DISALLOWED_PAGES, $storeId);
113+
}
114+
115+
/**
116+
* Retrieve Ignore JS
117+
*
118+
* @param string|null $storeId
119+
* @return string
120+
*/
121+
public function getIgnoreJavaScript(string $storeId = null): string
122+
{
123+
return (string)$this->getConfig(self::XML_PATH_DEFERRED_IGNORE_JAVASCRIPT, $storeId);
124+
}
125+
126+
/**
127+
* Retrieve true if JS bundling is enabled
128+
*
129+
* @param string|null $storeId
130+
* @return bool
131+
*/
132+
public function isBundlingEnabled(string $storeId = null): bool
133+
{
134+
return (bool)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_ENABLED, $storeId);
135+
}
136+
137+
/**
138+
* Retrieve true if bundling optimization is enabled
139+
*
140+
* @param string|null $storeId
141+
* @return bool
142+
*/
143+
public function isBundlingOptimizationEnabled(string $storeId = null): bool
144+
{
145+
return (bool)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_OPTIMIZATION_ENABLED, $storeId);
146+
}
147+
148+
/**
149+
* Retrieve included in bundling JS
150+
*
151+
* @param string|null $storeId
152+
* @return string
153+
*/
154+
public function getIncludedInBundling(string $storeId = null): string
155+
{
156+
return (string)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_INCLUDED_IN_BUNDLING, $storeId);
157+
}
158+
159+
/**
160+
* Retrieve true if amp enabled
161+
*
162+
* @param string|null $storeId
163+
* @return bool
164+
*/
165+
public function isAmpRequest(string $storeId = null): bool
166+
{
167+
return (bool)$this->getConfig(self::XML_PATH_PLUMROCKET_AMP_ENABLED, $storeId);
168+
}
169+
170+
/**
171+
* Retrieve store config value
172+
*
173+
* @param string $path
174+
* @param string|null $storeId
175+
* @return mixed
176+
*/
177+
public function getConfig(string $path, string $storeId = null)
178+
{
179+
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
180+
}
181+
}

‎Model/Controller/ResultPlugin.php

Lines changed: 11 additions & 26 deletions
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/deferred_javascript/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,13 +143,7 @@ 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/deferred_javascript/enabled',
154-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
155-
);
146+
$enabled = $this->config->isEnabled() && $this->config->isDeferredEnabled();
156147

157148

158149
if ($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/deferred_javascript/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) {

‎Plugin/Deploy/Package/Bundle/RequireJsPlugin.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,28 @@
99
namespace Magefan\RocketJavaScript\Plugin\Deploy\Package\Bundle;
1010

1111
use Magento\Deploy\Package\Bundle\RequireJs;
12-
use Magento\Framework\App\Config\ScopeConfigInterface;
13-
use Magento\Store\Model\ScopeInterface;
12+
use Magefan\RocketJavaScript\Model\Config;
1413

1514
class RequireJsPlugin
1615
{
17-
18-
/**
19-
* @var string
20-
*/
21-
const BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/javascript_bundling/enabled';
22-
2316
/**
24-
* @var string
17+
* @var Config
2518
*/
26-
const INCLUDE_IN_BUNDLING = 'mfrocketjavascript/javascript_bundling/included_in_bundling';
27-
28-
/**
29-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
30-
*/
31-
protected $scopeConfig;
19+
protected $config;
3220

3321
/**
3422
* mixed
3523
*/
3624
protected $allowedFiles;
3725

3826
/**
39-
* Construct
40-
*
41-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
27+
* RequireJsPlugin constructor.
28+
* @param Config $config
4229
*/
43-
public function __construct(ScopeConfigInterface $scopeConfig)
44-
{
45-
$this->scopeConfig = $scopeConfig;
30+
public function __construct(
31+
Config $config
32+
) {
33+
$this->config = $config;
4634
}
4735

4836
/**
@@ -51,8 +39,7 @@ public function __construct(ScopeConfigInterface $scopeConfig)
5139
public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath, $sourcePath, $contentType)
5240
{
5341

54-
$jsOptimization = $this->scopeConfig->getValue(self::BUNDLING_OPTIMIZATION_ENABLED, ScopeInterface::SCOPE_STORE)
55-
&& $this->scopeConfig->getValue('mfrocketjavascript/general/enabled', ScopeInterface::SCOPE_STORE);
42+
$jsOptimization = $this->config->isBundlingEnabled() && $this->config->isEnabled();
5643
if ($jsOptimization) {
5744
$allowedFiles = $this->getAllowedFiles();
5845

@@ -74,7 +61,7 @@ public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath,
7461
public function getAllowedFiles()
7562
{
7663
if (null === $this->allowedFiles) {
77-
$includeInBundling = $this->scopeConfig->getValue(self::INCLUDE_IN_BUNDLING, ScopeInterface::SCOPE_STORE);
64+
$includeInBundling = $this->config->getIncludedInBundling();
7865
$allowedFiles = str_replace("\r", "\n", $includeInBundling);
7966
$allowedFiles = explode("\n", $allowedFiles);
8067

0 commit comments

Comments
 (0)
Please sign in to comment.