From 2fd85be2b35ab03820329aaa2bee9dec5ceece5a Mon Sep 17 00:00:00 2001 From: Yuriy Date: Fri, 31 May 2024 12:15:26 +0300 Subject: [PATCH 1/4] 11052-speed-optimization-extension --- Model/Config/Backend/DevSettings.php | 91 ++++ Model/Controller/ResultPlugin.php | 8 +- Observer/AdminSystemConfigChangedSection.php | 461 ++++++++++++++++++ .../Deploy/Package/Bundle/RequireJsPlugin.php | 4 +- .../Controller/Result/JsFooterPlugin.php | 59 +++ Setup/Patch/Schema/ChangePath.php | 80 +++ etc/adminhtml/events.xml | 14 + etc/adminhtml/system.xml | 47 +- etc/config.xml | 8 +- etc/frontend/di.xml | 3 + 10 files changed, 756 insertions(+), 19 deletions(-) create mode 100644 Model/Config/Backend/DevSettings.php create mode 100644 Observer/AdminSystemConfigChangedSection.php create mode 100644 Plugin/Frontend/Magento/Theme/Controller/Result/JsFooterPlugin.php create mode 100644 Setup/Patch/Schema/ChangePath.php create mode 100644 etc/adminhtml/events.xml diff --git a/Model/Config/Backend/DevSettings.php b/Model/Config/Backend/DevSettings.php new file mode 100644 index 0000000..4ba32be --- /dev/null +++ b/Model/Config/Backend/DevSettings.php @@ -0,0 +1,91 @@ + 'dev/js/merge_files', + 'mfrocketjavascript/general/minify_files' => 'dev/js/minify_files', + 'mfrocketjavascript/javascript_bundling/enabled' => 'dev/js/enable_js_bundling', + ]; + + /** + * @var RequestInterface + */ + private $request; + + /** + * DevSettings constructor. + * @param Context $context + * @param Registry $registry + * @param ScopeConfigInterface $config + * @param TypeListInterface $cacheTypeList + * @param RequestInterface $request + * @param AbstractResource|null $resource + * @param AbstractDb|null $resourceCollection + * @param array $data + */ + public function __construct( + Context $context, + Registry $registry, + ScopeConfigInterface $config, + TypeListInterface $cacheTypeList, + RequestInterface $request, + AbstractResource $resource = null, + AbstractDb $resourceCollection = null, + array $data = [] + ) { + $this->request = $request; + parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); + } + + /** + * @return DevSettings|void + */ + public function afterLoad() + { + $scopeTypes = [ + StoreScopeInterface::SCOPE_WEBSITES, + StoreScopeInterface::SCOPE_WEBSITE, + StoreScopeInterface::SCOPE_STORES, + StoreScopeInterface::SCOPE_STORE + ]; + + $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT; + $scopeCode = null; + + foreach ($scopeTypes as $scope) { + $param = $this->request->getParam($scope); + if ($param) { + $scopeType = $scope; + $scopeCode = $param; + } + } + + $devSettingsValue = $this->_config->getValue( + self::KEY_VALUE_RELATION[$this->getData('path')], + $scopeType, + $scopeCode + ); + $this->setData('value', $devSettingsValue); + } +} diff --git a/Model/Controller/ResultPlugin.php b/Model/Controller/ResultPlugin.php index d58a5c8..dc2f862 100644 --- a/Model/Controller/ResultPlugin.php +++ b/Model/Controller/ResultPlugin.php @@ -81,7 +81,7 @@ public function aroundRenderResult( } $ignoredStrings = $this->scopeConfig->getValue( - 'mfrocketjavascript/general/ignore_deferred_javascript_with', + 'mfrocketjavascript/deferred_javascript/ignore_deferred_javascript_with', \Magento\Store\Model\ScopeInterface::SCOPE_STORE) ?: ''; $ignoredStrings = explode("\n", str_replace("\r", "\n", $ignoredStrings)); foreach ($ignoredStrings as $key => $ignoredString) { @@ -150,11 +150,11 @@ private function isEnabled() 'mfrocketjavascript/general/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) && $this->scopeConfig->getValue( - 'mfrocketjavascript/general/enable_deferred_javascript', + 'mfrocketjavascript/deferred_javascript/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); - + if ($enabled) { /* check if Amasty AMP enabled */ @@ -194,7 +194,7 @@ private function isAllowedOnPage() $this->allowedOnPage = false; $spPages = $this->scopeConfig->getValue( - 'mfrocketjavascript/general/disallowed_pages_for_deferred_js', + 'mfrocketjavascript/deferred_javascript/disallowed_pages_for_deferred_js', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); $spPages = explode("\n", str_replace("\r", "\n", $spPages)); diff --git a/Observer/AdminSystemConfigChangedSection.php b/Observer/AdminSystemConfigChangedSection.php new file mode 100644 index 0000000..4827d42 --- /dev/null +++ b/Observer/AdminSystemConfigChangedSection.php @@ -0,0 +1,461 @@ + 'enable_js_bundling' + ]; + + /** + * @var ScopeConfigInterface + */ + private $scopeConfig; + + /** + * @var ConfigLoader + */ + private $configLoader; + + /** + * @var TransactionFactory + */ + private $transactionFactory; + + /** + * @var ConfigStructure + */ + private $configStructure; + + /** + * @var SettingChecker + */ + private $settingChecker; + + /** + * @var ConfigValueFactory + */ + private $configValueFactory; + + /** + * @var StoreManagerInterface + */ + private $storeManager; + + /** + * @var MagentoConfig + */ + private $magentoConfig; + + /** + * @var ReinitableConfigInterface + */ + private $appConfig; + + /** + * @var null + */ + private $scope = null; + + /** + * @var null + */ + private $store = null; + + /** + * @var null + */ + private $website = null; + + /** + * @var null + */ + private $scopeId = null; + + /** + * @var null + */ + private $scopeCode = null; + + /** + * AdminSystemConfigChangedSection constructor. + * @param ScopeConfigInterface $scopeConfig + * @param ConfigLoader $configLoader + * @param TransactionFactory $transactionFactory + * @param ConfigStructure $configStructure + * @param ConfigValueFactory $configValueFactory + * @param StoreManagerInterface $storeManager + * @param MagentoConfig $magentoConfig + * @param ReinitableConfigInterface $appConfig + * @param SettingChecker|null $settingChecker + */ + public function __construct( + ScopeConfigInterface $scopeConfig, + ConfigLoader $configLoader, + TransactionFactory $transactionFactory, + ConfigStructure $configStructure, + ConfigValueFactory $configValueFactory, + StoreManagerInterface $storeManager, + MagentoConfig $magentoConfig, + ReinitableConfigInterface $appConfig, + SettingChecker $settingChecker = null + ) { + $this->scopeConfig = $scopeConfig; + $this->configLoader = $configLoader; + $this->transactionFactory = $transactionFactory; + $this->configStructure = $configStructure; + $this->configValueFactory = $configValueFactory; + $this->storeManager = $storeManager; + $this->magentoConfig = $magentoConfig; + $this->appConfig = $appConfig; + $this->settingChecker = $settingChecker ?: ObjectManager::getInstance()->get(SettingChecker::class); + } + + /** + * @param Observer $observer + * @return void + * @throws \Exception + */ + public function execute(Observer $observer) + { + $this->store = $observer->getData('store') ?? null; + $this->website = $observer->getData('website') ?? null; + + $this->initScope(); + + $groups = $this->scopeConfig->getValue('mfrocketjavascript', $this->scope, $this->scopeCode); + + if (!isset($groups['general']) && !isset($groups['javascript_bundling'])) { + return; + } + + $developerJsFields = []; + + foreach ($groups['general'] as $key => $value) { + if (in_array($key, self::JS_MERGE_ARRAY)) { + $developerJsFields['fields'][$key] = ['value' => $value]; + } + } + + foreach ($groups['javascript_bundling'] as $key => $value) { + if (key_exists($key, self::JS_BUNDLE_ARRAY)) { + $developerJsFields['fields'][self::JS_BUNDLE_ARRAY[$key]] = ['value' => $value]; + } + } + + $developerGroup['js'] = $developerJsFields; + $this->proceedTransaction($developerGroup, 'dev'); + } + + /** + * @param $groups + * @param $sectionId + * @throws \Exception + */ + private function proceedTransaction($groups, $sectionId) + { + $oldConfig = $this->configLoader->getConfigByPath( + 'mfrocketjavascript', + $this->scope, + $this->scopeId, + true + ); + + /** @var Transaction $deleteTransaction */ + $deleteTransaction = $this->transactionFactory->create(); + /** @var Transaction $saveTransaction */ + $saveTransaction = $this->transactionFactory->create(); + + $extraOldGroups = []; + + foreach ($groups as $groupId => $groupData) { + $this->processGroup( + $groupId, + $groupData, + $groups, + $sectionId, + $extraOldGroups, + $oldConfig, + $saveTransaction, + $deleteTransaction + ); + } + + try { + $deleteTransaction->delete(); + $saveTransaction->save(); + $this->appConfig->reinit(); + } catch (\Exception $e) { + $this->appConfig->reinit(); + throw $e; + } + } + + /** + * Process group data + * + * @param string $groupId + * @param array $groupData + * @param array $groups + * @param string $sectionPath + * @param array $extraOldGroups + * @param array $oldConfig + * @param Transaction $saveTransaction + * @param Transaction $deleteTransaction + * @return void + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) + */ + private function processGroup( + $groupId, + array $groupData, + array $groups, + $sectionPath, + array &$extraOldGroups, + array &$oldConfig, + Transaction $saveTransaction, + Transaction $deleteTransaction + ) { + $groupPath = $sectionPath . '/' . $groupId; + + if (isset($groupData['fields'])) { + /** @var Group $group */ + $group = $this->configStructure->getElement($groupPath); + + // set value for group field entry by fieldname + // use extra memory + $fieldsetData = []; + foreach ($groupData['fields'] as $fieldId => $fieldData) { + $fieldsetData[$fieldId] = $fieldData['value'] ?? null; + } + + foreach ($groupData['fields'] as $fieldId => $fieldData) { + $isReadOnly = $this->settingChecker->isReadOnly( + $groupPath . '/' . $fieldId, + $this->scope, + $this->scopeCode + ); + + if ($isReadOnly) { + continue; + } + + $field = $this->getField($sectionPath, $groupId, $fieldId); + /** @var ValueInterface $backendModel */ + $backendModel = $field->hasBackendModel() + ? $field->getBackendModel() + : $this->configValueFactory->create(); + + if (!isset($fieldData['value'])) { + $fieldData['value'] = null; + } + + if ($field->getType() == 'multiline' && is_array($fieldData['value'])) { + $fieldData['value'] = trim(implode(PHP_EOL, $fieldData['value'])); + } + + $data = [ + 'field' => $fieldId, + 'groups' => $groups, + 'group_id' => $group->getId(), + 'scope' => $this->scope, + 'scope_id' => $this->scopeId, + 'scope_code' => $this->scopeCode, + 'field_config' => $field->getData(), + 'fieldset_data' => $fieldsetData + ]; + $backendModel->addData($data); + $this->checkSingleStoreMode($field, $backendModel); + + $path = $this->getFieldPath($field, $fieldId, $oldConfig, $extraOldGroups); + $backendModel->setPath($path)->setValue($fieldData['value']); + + $inherit = !empty($fieldData['inherit']); + if (isset($oldConfig[$path])) { + $backendModel->setConfigId($oldConfig[$path]['config_id']); + + /** + * Delete config data if inherit + */ + if (!$inherit) { + $saveTransaction->addObject($backendModel); + } else { + $deleteTransaction->addObject($backendModel); + } + } elseif (!$inherit) { + $backendModel->unsConfigId(); + $saveTransaction->addObject($backendModel); + } + } + } + } + + /** + * Get field object + * + * @param string $sectionId + * @param string $groupId + * @param string $fieldId + * @return Field + */ + private function getField(string $sectionId, string $groupId, string $fieldId): Field + { + /** @var Group $group */ + $group = $this->configStructure->getElement($sectionId . '/' . $groupId); + $fieldPath = $group->getPath() . '/' . $this->getOriginalFieldId($group, $fieldId); + $field = $this->configStructure->getElement($fieldPath); + + return $field; + } + + /** + * Map field name if they were cloned + * + * @param Group $group + * @param string $fieldId + * @return string + */ + private function getOriginalFieldId(Group $group, string $fieldId): string + { + if ($group->shouldCloneFields()) { + $cloneModel = $group->getCloneModel(); + + /** @var Field $field */ + foreach ($group->getChildren() as $field) { + foreach ($cloneModel->getPrefixes() as $prefix) { + if ($prefix['field'] . $field->getId() === $fieldId) { + $fieldId = $field->getId(); + break(2); + } + } + } + } + + return $fieldId; + } + + /** + * Set correct scope if isSingleStoreMode = true + * + * @param Field $fieldConfig + * @param ValueInterface $dataObject + * @return void + */ + protected function checkSingleStoreMode(Field $fieldConfig, $dataObject) + { + $isSingleStoreMode = $this->storeManager->isSingleStoreMode(); + if (!$isSingleStoreMode) { + return; + } + if (!$fieldConfig->showInDefault()) { + $websites = $this->storeManager->getWebsites(); + $singleStoreWebsite = array_shift($websites); + $dataObject->setScope('websites'); + $dataObject->setWebsiteCode($singleStoreWebsite->getCode()); + $dataObject->setScopeCode($singleStoreWebsite->getCode()); + $dataObject->setScopeId($singleStoreWebsite->getId()); + } + } + + /** + * Get field path + * + * @param Field $field + * @param string $fieldId Need for support of clone_field feature + * @param array $oldConfig Need for compatibility with _processGroup() + * @param array $extraOldGroups Need for compatibility with _processGroup() + * @return string + */ + private function getFieldPath(Field $field, string $fieldId, array &$oldConfig, array &$extraOldGroups): string + { + $path = $field->getGroupPath() . '/' . $fieldId; + + /** + * Look for custom defined field path + */ + $configPath = $field->getConfigPath(); + if ($configPath && strrpos($configPath, '/') > 0) { + // Extend old data with specified section group + $configGroupPath = substr($configPath, 0, strrpos($configPath, '/')); + if (!isset($extraOldGroups[$configGroupPath])) { + $oldConfig = $this->magentoConfig->extendConfig($configGroupPath, true, $oldConfig); + $extraOldGroups[$configGroupPath] = true; + } + $path = $configPath; + } + + return $path; + } + + /** + * Get scope name and scopeId + * + * @todo refactor to scope resolver + * @return void + */ + private function initScope() + { + if ($this->website === null) { + $this->website = ''; + } + if ($this->store === null) { + $this->store = ''; + } + + if ($this->store) { + $scope = 'stores'; + $store = $this->storeManager->getStore($this->store); + $scopeId = (int)$store->getId(); + $scopeCode = $store->getCode(); + } elseif ($this->website) { + $scope = 'websites'; + $website = $this->storeManager->getWebsite($this->website); + $scopeId = (int)$website->getId(); + $scopeCode = $website->getCode(); + } else { + $scope = 'default'; + $scopeId = 0; + $scopeCode = ''; + } + $this->scope = $scope; + $this->scopeId = $scopeId; + $this->scopeCode = $scopeCode; + } +} diff --git a/Plugin/Deploy/Package/Bundle/RequireJsPlugin.php b/Plugin/Deploy/Package/Bundle/RequireJsPlugin.php index 042cf5d..edae6c0 100644 --- a/Plugin/Deploy/Package/Bundle/RequireJsPlugin.php +++ b/Plugin/Deploy/Package/Bundle/RequireJsPlugin.php @@ -18,12 +18,12 @@ class RequireJsPlugin /** * @var string */ - const BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/general/enable_js_bundling_optimization'; + const BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/javascript_bundling/enabled'; /** * @var string */ - const INCLUDE_IN_BUNDLING = 'mfrocketjavascript/general/included_in_bundling'; + const INCLUDE_IN_BUNDLING = 'mfrocketjavascript/javascript_bundling/included_in_bundling'; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface diff --git a/Plugin/Frontend/Magento/Theme/Controller/Result/JsFooterPlugin.php b/Plugin/Frontend/Magento/Theme/Controller/Result/JsFooterPlugin.php new file mode 100644 index 0000000..df5cc04 --- /dev/null +++ b/Plugin/Frontend/Magento/Theme/Controller/Result/JsFooterPlugin.php @@ -0,0 +1,59 @@ +scopeConfig = $scopeConfig; + } + + /** + * @param \Magento\Theme\Controller\Result\JsFooterPlugin $subject + * @param \Closure $proceed + * @param Layout $argumentSubject + * @param Layout $result + * @param ResponseInterface $httpResponse + * @return Layout|mixed + */ + public function aroundAfterRenderResult( + \Magento\Theme\Controller\Result\JsFooterPlugin $subject, + \Closure $proceed, + Layout $argumentSubject, + Layout $result, + ResponseInterface $httpResponse + ) { + $jsRjOptimization = $this->scopeConfig->isSetFlag(self::XML_PATH_RJ_DEFERRED_ENABLED, ScopeInterface::SCOPE_STORE) + && $this->scopeConfig->isSetFlag('mfrocketjavascript/general/enabled', ScopeInterface::SCOPE_STORE); + if ($jsRjOptimization) { + return $result; + } + + return $proceed($argumentSubject, $result, $httpResponse); + } +} diff --git a/Setup/Patch/Schema/ChangePath.php b/Setup/Patch/Schema/ChangePath.php new file mode 100644 index 0000000..47b4d2a --- /dev/null +++ b/Setup/Patch/Schema/ChangePath.php @@ -0,0 +1,80 @@ +schemaSetup = $schemaSetup; + } + + /** + * {@inheritdoc} + */ + public function apply() + { + $this->schemaSetup->startSetup(); + $connection = $this->schemaSetup->getConnection(); + + $table = $this->schemaSetup->getTable('core_config_data'); + + $changedConfigurationFields = [ + 'mfrocketjavascript/general/enable_deferred_javascript' => 'mfrocketjavascript/deferred_javascript/enabled', + 'mfrocketjavascript/general/disallowed_pages_for_deferred_js' => 'mfrocketjavascript/deferred_javascript/disallowed_pages_for_deferred_js', + 'mfrocketjavascript/general/ignore_deferred_javascript_with' => 'mfrocketjavascript/deferred_javascript/ignore_deferred_javascript_with', + + 'mfrocketjavascript/general/enable_js_bundling_optimization' => 'mfrocketjavascript/javascript_bundling/enabled', + 'mfrocketjavascript/general/included_in_bundling' => 'mfrocketjavascript/javascript_bundling/included_in_bundling', + ]; + + foreach ($changedConfigurationFields as $oldPath => $newPath) { + $connection->update( + $table, + ['path' => $newPath], + ['path = ?' => $oldPath] + ); + } + + $this->schemaSetup->endSetup(); + } + + /** + * @return array + */ + public function getAliases() + { + return []; + } + + /** + * @return array + */ + public static function getDependencies() + { + return []; + } +} diff --git a/etc/adminhtml/events.xml b/etc/adminhtml/events.xml new file mode 100644 index 0000000..cbf7441 --- /dev/null +++ b/etc/adminhtml/events.xml @@ -0,0 +1,14 @@ + + + + + + + diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index a2d2b26..527492b 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -27,34 +27,59 @@ Magefan\Community\Block\Adminhtml\System\Config\Form\ProductKeyField - - + + + Magento\Config\Model\Config\Source\Yesno + Magefan\RocketJavaScript\Model\Config\Backend\DevSettings + + + + Magento\Config\Model\Config\Source\Yesno + Magefan\RocketJavaScript\Model\Config\Backend\DevSettings + Minification is not applied in developer mode. + + + + + + If enabled all JavaScript on storefront will be moved to the end of the page. Magento\Config\Model\Config\Source\Yesno - - + + - 1 + 1 Enter page patches each in a new line. "*" means any path, you can use it at the beginning or end. - + - 1 + 1 data-rocketjavascript="false" will automatically be ignored. Example <script data-rocketjavascript="false">/* some script *</script>]]> - - + + + + + + Magento\Config\Model\Config\Source\Yesno + Magefan\RocketJavaScript\Model\Config\Backend\DevSettings + + + + + 1 + Please note that this option only works with enabled JavaScript Bundling (Configuration > Advanced > Developer > JavaScript Settings > Enable JavaScript Bundling). Magento\Config\Model\Config\Source\Yesno - + - 1 + 1 List of files that included in JS bundle. diff --git a/etc/config.xml b/etc/config.xml index 56f71fd..6ccc965 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -13,11 +13,15 @@ RocketJavaScript 1 - 1 + + + 1 checkout/* onestepcheckout/* www.googletagmanager.com + + 1 jquery.min.js mage/common.min.js @@ -135,7 +139,7 @@ Magento_Customer/template/authentication-popup.html Magento_Ui/templates/collection.html Magento_Ui/template/messages.html Magento_Captcha/template/checkout/captcha.html - + diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index 43a5cfa..b71473d 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -11,4 +11,7 @@ + + + From 0503db9db69d113425dbb8a91c5a847e5d48ca68 Mon Sep 17 00:00:00 2001 From: Yuriy Date: Wed, 24 Jul 2024 16:39:36 +0300 Subject: [PATCH 2/4] 11052-add-config --- Model/Config.php | 181 ++++++++++++++++++ Model/Controller/ResultPlugin.php | 37 ++-- .../Deploy/Package/Bundle/RequireJsPlugin.php | 35 ++-- 3 files changed, 203 insertions(+), 50 deletions(-) create mode 100644 Model/Config.php diff --git a/Model/Config.php b/Model/Config.php new file mode 100644 index 0000000..9ee8920 --- /dev/null +++ b/Model/Config.php @@ -0,0 +1,181 @@ +scopeConfig = $scopeConfig; + } + + /** + * Retrieve true if module is enabled + * + * @param string|null $storeId + * @return bool + */ + public function isEnabled(string $storeId = null): bool + { + return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId); + } + + /** + * Retrieve true if js files merged + * + * @param string|null $storeId + * @return bool + */ + public function isMergeFiles(string $storeId = null): bool + { + return (bool)$this->getConfig(self::XML_PATH_MERGE_FILES, $storeId); + } + + /** + * Retrieve true if js files minified + * + * @param string|null $storeId + * @return bool + */ + public function isMinifyFiles(string $storeId = null): bool + { + return (bool)$this->getConfig(self::XML_PATH_MINIFY_FILES, $storeId); + } + + /** + * Retrieve true if deferred is enabled + * + * @param string|null $storeId + * @return bool + */ + public function isDeferredEnabled(string $storeId = null): bool + { + return (bool)$this->getConfig(self::XML_PATH_DEFERRED_ENABLED, $storeId); + } + + /** + * Retrieve Disallowed Pages + * + * @param string|null $storeId + * @return string + */ + public function getDisallowedPages(string $storeId = null): string + { + return (string)$this->getConfig(self::XML_PATH_DEFERRED_DISALLOWED_PAGES, $storeId); + } + + /** + * Retrieve Ignore JS + * + * @param string|null $storeId + * @return string + */ + public function getIgnoreJavaScript(string $storeId = null): string + { + return (string)$this->getConfig(self::XML_PATH_DEFERRED_IGNORE_JAVASCRIPT, $storeId); + } + + /** + * Retrieve true if JS bundling is enabled + * + * @param string|null $storeId + * @return bool + */ + public function isBundlingEnabled(string $storeId = null): bool + { + return (bool)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_ENABLED, $storeId); + } + + /** + * Retrieve true if bundling optimization is enabled + * + * @param string|null $storeId + * @return bool + */ + public function isBundlingOptimizationEnabled(string $storeId = null): bool + { + return (bool)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_OPTIMIZATION_ENABLED, $storeId); + } + + /** + * Retrieve included in bundling JS + * + * @param string|null $storeId + * @return string + */ + public function getIncludedInBundling(string $storeId = null): string + { + return (string)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_INCLUDED_IN_BUNDLING, $storeId); + } + + /** + * Retrieve true if amp enabled + * + * @param string|null $storeId + * @return bool + */ + public function isAmpRequest(string $storeId = null): bool + { + return (bool)$this->getConfig(self::XML_PATH_PLUMROCKET_AMP_ENABLED, $storeId); + } + + /** + * Retrieve store config value + * + * @param string $path + * @param string|null $storeId + * @return mixed + */ + public function getConfig(string $path, string $storeId = null) + { + return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId); + } +} diff --git a/Model/Controller/ResultPlugin.php b/Model/Controller/ResultPlugin.php index dc2f862..efc2b9f 100644 --- a/Model/Controller/ResultPlugin.php +++ b/Model/Controller/ResultPlugin.php @@ -24,11 +24,9 @@ class ResultPlugin protected $request; /** - * Core store config - * - * @var \Magento\Framework\App\Config\ScopeConfigInterface + * @var \Magefan\RocketJavaScript\Model\Config */ - protected $scopeConfig; + protected $config; /** * @var bool @@ -41,17 +39,18 @@ class ResultPlugin protected $storeManager; /** - * @param \Magento\Framework\App\RequestInterface $request - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + * ResultPlugin constructor. + * @param \Magento\Framework\App\RequestInterface $request + * @param \Magefan\RocketJavaScript\Model\Config $config * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager */ public function __construct( \Magento\Framework\App\RequestInterface $request, - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, + \Magefan\RocketJavaScript\Model\Config $config, \Magento\Store\Model\StoreManagerInterface $storeManager = null ) { $this->request = $request; - $this->scopeConfig = $scopeConfig; + $this->config = $config; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->storeManager = $storeManager ?: $objectManager->get( @@ -80,9 +79,7 @@ public function aroundRenderResult( return $result; } - $ignoredStrings = $this->scopeConfig->getValue( - 'mfrocketjavascript/deferred_javascript/ignore_deferred_javascript_with', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE) ?: ''; + $ignoredStrings = $this->config->getIgnoreJavaScript() ?: ''; $ignoredStrings = explode("\n", str_replace("\r", "\n", $ignoredStrings)); foreach ($ignoredStrings as $key => $ignoredString) { $ignoredString = trim($ignoredString); @@ -146,13 +143,7 @@ public function aroundRenderResult( private function isEnabled() { - $enabled = $this->scopeConfig->getValue( - 'mfrocketjavascript/general/enabled', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE - ) && $this->scopeConfig->getValue( - 'mfrocketjavascript/deferred_javascript/enabled', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE - ); + $enabled = $this->config->isEnabled() && $this->config->isDeferredEnabled(); if ($enabled) { @@ -163,10 +154,7 @@ private function isEnabled() } /* check if Plumrocket AMP enabled */ - $isAmpRequest = $this->scopeConfig->getValue( - 'pramp/general/enabled', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE - ); + $isAmpRequest = $this->config->isAmpRequest(); if ($isAmpRequest) { /* We know that using objectManager is not a not a good practice, @@ -193,10 +181,7 @@ private function isAllowedOnPage() } $this->allowedOnPage = false; - $spPages = $this->scopeConfig->getValue( - 'mfrocketjavascript/deferred_javascript/disallowed_pages_for_deferred_js', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE - ); + $spPages = $this->config->getDisallowedPages(); $spPages = explode("\n", str_replace("\r", "\n", $spPages)); foreach ($spPages as $key => $path) { diff --git a/Plugin/Deploy/Package/Bundle/RequireJsPlugin.php b/Plugin/Deploy/Package/Bundle/RequireJsPlugin.php index edae6c0..e27032c 100644 --- a/Plugin/Deploy/Package/Bundle/RequireJsPlugin.php +++ b/Plugin/Deploy/Package/Bundle/RequireJsPlugin.php @@ -9,26 +9,14 @@ namespace Magefan\RocketJavaScript\Plugin\Deploy\Package\Bundle; use Magento\Deploy\Package\Bundle\RequireJs; -use Magento\Framework\App\Config\ScopeConfigInterface; -use Magento\Store\Model\ScopeInterface; +use Magefan\RocketJavaScript\Model\Config; class RequireJsPlugin { - - /** - * @var string - */ - const BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/javascript_bundling/enabled'; - /** - * @var string + * @var Config */ - const INCLUDE_IN_BUNDLING = 'mfrocketjavascript/javascript_bundling/included_in_bundling'; - - /** - * @var \Magento\Framework\App\Config\ScopeConfigInterface - */ - protected $scopeConfig; + protected $config; /** * mixed @@ -36,13 +24,13 @@ class RequireJsPlugin protected $allowedFiles; /** - * Construct - * - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + * RequireJsPlugin constructor. + * @param Config $config */ - public function __construct(ScopeConfigInterface $scopeConfig) - { - $this->scopeConfig = $scopeConfig; + public function __construct( + Config $config + ) { + $this->config = $config; } /** @@ -51,8 +39,7 @@ public function __construct(ScopeConfigInterface $scopeConfig) public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath, $sourcePath, $contentType) { - $jsOptimization = $this->scopeConfig->getValue(self::BUNDLING_OPTIMIZATION_ENABLED, ScopeInterface::SCOPE_STORE) - && $this->scopeConfig->getValue('mfrocketjavascript/general/enabled', ScopeInterface::SCOPE_STORE); + $jsOptimization = $this->config->isBundlingEnabled() && $this->config->isEnabled(); if ($jsOptimization) { $allowedFiles = $this->getAllowedFiles(); @@ -74,7 +61,7 @@ public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath, public function getAllowedFiles() { if (null === $this->allowedFiles) { - $includeInBundling = $this->scopeConfig->getValue(self::INCLUDE_IN_BUNDLING, ScopeInterface::SCOPE_STORE); + $includeInBundling = $this->config->getIncludedInBundling(); $allowedFiles = str_replace("\r", "\n", $includeInBundling); $allowedFiles = explode("\n", $allowedFiles); From b1a91cb3e5008a87341f1a7dba38c9e9241be565 Mon Sep 17 00:00:00 2001 From: b__b Date: Thu, 14 Nov 2024 13:17:55 +0100 Subject: [PATCH 3/4] refactoring --- .../Theme/Controller/Result/JsFooterPlugin.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Plugin/Frontend/Magento/Theme/Controller/Result/JsFooterPlugin.php b/Plugin/Frontend/Magento/Theme/Controller/Result/JsFooterPlugin.php index df5cc04..d4752fa 100644 --- a/Plugin/Frontend/Magento/Theme/Controller/Result/JsFooterPlugin.php +++ b/Plugin/Frontend/Magento/Theme/Controller/Result/JsFooterPlugin.php @@ -14,12 +14,10 @@ use Magento\Framework\App\ResponseInterface; use Magento\Framework\View\Result\Layout; use Magento\Store\Model\ScopeInterface; +use Magefan\RocketJavaScript\Model\Config; class JsFooterPlugin { - - private const XML_PATH_RJ_DEFERRED_ENABLED = 'mfrocketjavascript/deferred_javascript/enabled'; - /** * @var ScopeConfigInterface */ @@ -48,12 +46,12 @@ public function aroundAfterRenderResult( Layout $result, ResponseInterface $httpResponse ) { - $jsRjOptimization = $this->scopeConfig->isSetFlag(self::XML_PATH_RJ_DEFERRED_ENABLED, ScopeInterface::SCOPE_STORE) - && $this->scopeConfig->isSetFlag('mfrocketjavascript/general/enabled', ScopeInterface::SCOPE_STORE); - if ($jsRjOptimization) { - return $result; - } + $jsRjOptimization = + $this->scopeConfig->isSetFlag(Config::XML_PATH_DEFERRED_ENABLED, ScopeInterface::SCOPE_STORE) && + $this->scopeConfig->isSetFlag(Config::XML_PATH_EXTENSION_ENABLED, ScopeInterface::SCOPE_STORE); - return $proceed($argumentSubject, $result, $httpResponse); + return $jsRjOptimization + ? $result + : $proceed($argumentSubject, $result, $httpResponse); } } From 095ccff4d982c8caff4e53100f193620fa6e43e5 Mon Sep 17 00:00:00 2001 From: Bohdan Berezhniy Date: Mon, 18 Nov 2024 13:57:28 +0200 Subject: [PATCH 4/4] refactoring --- Model/Config.php | 25 ++----------------------- etc/adminhtml/system.xml | 5 +++-- 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/Model/Config.php b/Model/Config.php index 9ee8920..43b403d 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -12,6 +12,7 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\ScopeInterface; +use Magento\Framework\View\Asset\Config as MagentoConfig; class Config { @@ -68,28 +69,6 @@ public function isEnabled(string $storeId = null): bool return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId); } - /** - * Retrieve true if js files merged - * - * @param string|null $storeId - * @return bool - */ - public function isMergeFiles(string $storeId = null): bool - { - return (bool)$this->getConfig(self::XML_PATH_MERGE_FILES, $storeId); - } - - /** - * Retrieve true if js files minified - * - * @param string|null $storeId - * @return bool - */ - public function isMinifyFiles(string $storeId = null): bool - { - return (bool)$this->getConfig(self::XML_PATH_MINIFY_FILES, $storeId); - } - /** * Retrieve true if deferred is enabled * @@ -131,7 +110,7 @@ public function getIgnoreJavaScript(string $storeId = null): string */ public function isBundlingEnabled(string $storeId = null): bool { - return (bool)$this->getConfig(self::XML_PATH_JAVASCRIPT_BUNDLING_ENABLED, $storeId); + return (bool)$this->getConfig(MagentoConfig::XML_PATH_JS_BUNDLING, $storeId); } /** diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 527492b..51d1fc8 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -36,7 +36,7 @@ Magento\Config\Model\Config\Source\Yesno Magefan\RocketJavaScript\Model\Config\Backend\DevSettings - Minification is not applied in developer mode. + Minification is not applied in developer mode. Static content deployment is a requirement after option change @@ -67,13 +67,14 @@ Magento\Config\Model\Config\Source\Yesno Magefan\RocketJavaScript\Model\Config\Backend\DevSettings + Static content deployment is a requirement after option change 1 - Please note that this option only works with enabled JavaScript Bundling (Configuration > Advanced > Developer > JavaScript Settings > Enable JavaScript Bundling). + Static content deployment is a requirement after option change Magento\Config\Model\Config\Source\Yesno