11<?php
2+ /**
3+ * Copyright © Magefan (support@magefan.com). 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+
29namespace Magefan \RocketJavaScript \Plugin \Deploy \Package \Bundle ;
310
411use Magento \Deploy \Package \Bundle \RequireJs ;
12+ use Magento \Framework \App \Config \ScopeConfigInterface ;
13+ use Magento \Store \Model \ScopeInterface ;
514
615class RequireJsPlugin
716{
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+ */
831 protected $ scopeConfig ;
932
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 )
1144 {
1245 $ this ->scopeConfig = $ scopeConfig ;
1346 }
1447
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+ {
1653
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 );
1855 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 ();
3357
3458 $ include = false ;
3559 foreach ($ allowedFiles as $ allowedFile ) {
@@ -42,8 +66,35 @@ public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath,
4266 if (!$ include ) {
4367 return true ;
4468 }
45-
4669 }
4770 return $ proceed ($ filePath , $ sourcePath , $ contentType );
4871 }
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