Skip to content

11052 speed optimization extension v2 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/

declare(strict_types=1);

namespace Magefan\RocketJavaScript\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\View\Asset\Config as MagentoConfig;

class Config
{
/**
* General config
*/
public const XML_PATH_EXTENSION_ENABLED = 'mfrocketjavascript/general/enabled';
public const XML_PATH_MERGE_FILES = 'mfrocketjavascript/general/merge_files';
public const XML_PATH_MINIFY_FILES = 'mfrocketjavascript/general/minify_files';

/**
* Deferred JavaScript config
*/
public const XML_PATH_DEFERRED_ENABLED = 'mfrocketjavascript/deferred_javascript/enabled';
public const XML_PATH_DEFERRED_DISALLOWED_PAGES = 'mfrocketjavascript/deferred_javascript/disallowed_pages_for_deferred_js';
public const XML_PATH_DEFERRED_IGNORE_JAVASCRIPT = 'mfrocketjavascript/deferred_javascript/ignore_deferred_javascript_with';

/**
* JavaScript Bundling config
*/
public const XML_PATH_JAVASCRIPT_BUNDLING_ENABLED = 'mfrocketjavascript/javascript_bundling/enabled';
public const XML_PATH_JAVASCRIPT_BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/javascript_bundling/enable_js_bundling_optimization';
public const XML_PATH_JAVASCRIPT_BUNDLING_INCLUDED_IN_BUNDLING = 'mfrocketjavascript/javascript_bundling/included_in_bundling';

/**
* Plumrocket AMP config
*/
public const XML_PATH_PLUMROCKET_AMP_ENABLED = 'pramp/general/enabled';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Config constructor.
*
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->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 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(MagentoConfig::XML_PATH_JS_BUNDLING, $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);
}
}
91 changes: 91 additions & 0 deletions Model/Config/Backend/DevSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/

declare(strict_types=1);

namespace Magefan\RocketJavaScript\Model\Config\Backend;

use Magento\Framework\App\Config\Value;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Model\Context;
use Magento\Framework\Registry;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Data\Collection\AbstractDb;

class DevSettings extends Value
{
const KEY_VALUE_RELATION = [
'mfrocketjavascript/general/merge_files' => '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);
}
}
39 changes: 12 additions & 27 deletions Model/Controller/ResultPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -80,9 +79,7 @@ public function aroundRenderResult(
return $result;
}

$ignoredStrings = $this->scopeConfig->getValue(
'mfrocketjavascript/general/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);
Expand Down Expand Up @@ -146,15 +143,9 @@ public function aroundRenderResult(

private function isEnabled()
{
$enabled = $this->scopeConfig->getValue(
'mfrocketjavascript/general/enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) && $this->scopeConfig->getValue(
'mfrocketjavascript/general/enable_deferred_javascript',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$enabled = $this->config->isEnabled() && $this->config->isDeferredEnabled();



if ($enabled) {

/* check if Amasty AMP enabled */
Expand All @@ -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,
Expand All @@ -193,10 +181,7 @@ private function isAllowedOnPage()
}
$this->allowedOnPage = false;

$spPages = $this->scopeConfig->getValue(
'mfrocketjavascript/general/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) {
Expand Down
Loading