1
1
<?php
2
+ /**
3
+ * Copyright © Magefan ([email protected] ). All rights reserved.
4
+ * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5
+ *
6
+ * Glory to Ukraine! Glory to the heroes!
7
+ */
8
+
2
9
namespace Magefan \RocketJavaScript \Plugin \Deploy \Package \Bundle ;
3
10
4
11
use Magento \Deploy \Package \Bundle \RequireJs ;
12
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
13
+ use Magento \Store \Model \ScopeInterface ;
5
14
6
15
class RequireJsPlugin
7
16
{
17
+
18
+ /**
19
+ * @var string
20
+ */
21
+ const BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/general/enable_js_bundling_optimization ' ;
22
+
23
+ /**
24
+ * @var string
25
+ */
26
+ const INCLUDE_IN_BUNDLING = 'mfrocketjavascript/general/included_in_bundling ' ;
27
+
28
+ /**
29
+ * @var \Magento\Framework\App\Config\ScopeConfigInterface
30
+ */
8
31
protected $ scopeConfig ;
9
32
10
- public function __construct (\Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig )
33
+ /**
34
+ * mixed
35
+ */
36
+ protected $ allowedFiles ;
37
+
38
+ /**
39
+ * Construct
40
+ *
41
+ * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
42
+ */
43
+ public function __construct (ScopeConfigInterface $ scopeConfig )
11
44
{
12
45
$ this ->scopeConfig = $ scopeConfig ;
13
46
}
14
47
15
- public function aroundAddFile (RequireJs $ subject , callable $ proceed , $ filePath , $ sourcePath , $ contentType ) {
48
+ /**
49
+ * Improve bundling
50
+ */
51
+ public function aroundAddFile (RequireJs $ subject , callable $ proceed , $ filePath , $ sourcePath , $ contentType )
52
+ {
16
53
17
- $ jsOptimization = $ this ->scopeConfig ->getValue (' mfrocketjavascript/general/enable_javaScript_bundling_optimization ' , \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE );
54
+ $ jsOptimization = $ this ->scopeConfig ->getValue (self :: BUNDLING_OPTIMIZATION_ENABLED , ScopeInterface::SCOPE_STORE );
18
55
if ($ jsOptimization ) {
19
- $ includeInBundling = $ this ->scopeConfig ->getValue ('mfrocketjavascript/general/included_in_bundling ' , \Magento \Store \Model \ScopeInterface::SCOPE_STORE );
20
- $ allowedFiles = str_replace ("\r" ,"\n" , $ includeInBundling );
21
- $ allowedFiles = explode ("\n" , $ allowedFiles );
22
-
23
- foreach ($ allowedFiles as $ key => $ allowedFile ) {
24
- $ allowedFiles [$ key ] = trim ($ allowedFile );
25
- if (empty ($ allowedFiles [$ key ])) {
26
- unset($ allowedFiles [$ key ]);
27
- }
28
- }
29
-
30
- foreach ($ allowedFiles as $ allowed ) {
31
- $ allowedFiles [] = str_replace ('.min.js ' , '.js ' , $ allowed );
32
- }
56
+ $ allowedFiles = $ this ->getAllowedFiles ();
33
57
34
58
$ include = false ;
35
59
foreach ($ allowedFiles as $ allowedFile ) {
@@ -42,8 +66,35 @@ public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath,
42
66
if (!$ include ) {
43
67
return true ;
44
68
}
45
-
46
69
}
47
70
return $ proceed ($ filePath , $ sourcePath , $ contentType );
48
71
}
49
- }
72
+
73
+ public function getAllowedFiles ()
74
+ {
75
+ if (null === $ this ->allowedFiles ) {
76
+ $ includeInBundling = $ this ->scopeConfig ->getValue (self ::INCLUDE_IN_BUNDLING , ScopeInterface::SCOPE_STORE );
77
+ $ allowedFiles = str_replace ("\r" , "\n" , $ includeInBundling );
78
+ $ allowedFiles = explode ("\n" , $ allowedFiles );
79
+
80
+ foreach ($ allowedFiles as $ key => $ allowedFile ) {
81
+ $ allowedFiles [$ key ] = trim ($ allowedFile );
82
+ if (empty ($ allowedFiles [$ key ])) {
83
+ unset($ allowedFiles [$ key ]);
84
+ }
85
+ }
86
+
87
+ foreach ($ allowedFiles as $ allowed ) {
88
+ if (false !== strpos ($ allowed , '.min.js ' )) {
89
+ $ allowedFiles [] = str_replace ('.min.js ' , '.js ' , $ allowed );
90
+ } else {
91
+ $ allowedFiles [] = str_replace ('.js ' , '.min.js ' , $ allowed );
92
+ }
93
+ }
94
+
95
+ $ this ->allowedFiles = $ allowedFiles ;
96
+ }
97
+
98
+ return $ this ->allowedFiles ;
99
+ }
100
+ }
0 commit comments