Skip to content

Commit

Permalink
Code style changes + improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Apr 16, 2018
1 parent 61338d0 commit fa9517b
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 73 deletions.
39 changes: 11 additions & 28 deletions Block/Adminhtml/System/Config/Form/Info.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
Expand All @@ -11,42 +11,25 @@
use Magento\Store\Model\ScopeInterface;

/**
* Admin blog configurations information block
* Admin configurations information block
*/
class Info extends \Magento\Config\Block\System\Config\Form\Field
class Info extends \Magefan\Community\Block\Adminhtml\System\Config\Form\Info
{
/**
* @var \Magento\Framework\Module\ModuleListInterface
*/
protected $moduleList;

/**
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
* @param \Magento\Backend\Block\Template\Context $context
* @param array $data
* Return extension url
* @return string
*/
public function __construct(
\Magento\Framework\Module\ModuleListInterface $moduleList,
\Magento\Backend\Block\Template\Context $context,
array $data = []
) {
parent::__construct($context, $data);
$this->moduleList = $moduleList;
protected function getModuleUrl()
{
return 'https://magefan.com/rocket-javascript-deferred-javascript?utm_source=m2admin_rocket_javascript_config&utm_medium=link&utm_campaign=regular';
}

/**
* Return info block html
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
* Return extension title
* @return string
*/
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
protected function getModuleTitle()
{
$m = $this->moduleList->getOne($this->getModuleName());
$html = '<div style="padding:10px;background-color:#f8f8f8;border:1px solid #ddd;margin-bottom:7px;">
Rocket JavaScript Extension v' . $m['setup_version'] . ' was developed by <a href="http://magefan.com/" target="_blank">Magefan</a>.
</div>';

return $html;
return 'Rocket JavaScript Extension';
}

}
28 changes: 14 additions & 14 deletions Model/Controller/ResultPlugin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
Expand All @@ -17,7 +17,7 @@ class ResultPlugin
{
const EXCLUDE_FLAG_PATTERN = 'data-rocketjavascript="false"';

/**
/**
* Request
* @var \Magento\Framework\App\RequestInterface
*/
Expand Down Expand Up @@ -69,7 +69,7 @@ public function aroundRenderResult(
$jsHtml = [];

foreach ($patterns as $pattern) {
$matches = array();
$matches = [];
$success = preg_match_all($pattern, $html, $matches);
if ($success) {
foreach ($matches[0] as $i => $js) {
Expand Down Expand Up @@ -105,21 +105,21 @@ private function isEnabled()

/* check if Plumrocket AMP enabled */
if ($enabled) {
$isAmpRequest = $this->scopeConfig->getValue(
'pramp/general/enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$isAmpRequest = $this->scopeConfig->getValue(
'pramp/general/enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

if ($isAmpRequest) {
/* We know that using objectManager is not a not a good practice,
if ($isAmpRequest) {
/* We know that using objectManager is not a not a good practice,
but if Plumrocket_AMP is not installed on your magento instance
you'll get error during di:compile */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$isAmpRequest = $objectManager->get('\Plumrocket\Amp\Helper\Data')
->isAmpRequest();
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$isAmpRequest = $objectManager->get('\Plumrocket\Amp\Helper\Data')
->isAmpRequest();
}

$enabled = !$isAmpRequest;
$enabled = !$isAmpRequest;
}

return $enabled;
Expand Down
89 changes: 70 additions & 19 deletions Plugin/Deploy/Package/Bundle/RequireJsPlugin.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
*/

namespace Magefan\RocketJavaScript\Plugin\Deploy\Package\Bundle;

use Magento\Deploy\Package\Bundle\RequireJs;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class RequireJsPlugin
{

/**
* @var string
*/
const BUNDLING_OPTIMIZATION_ENABLED = 'mfrocketjavascript/general/enable_js_bundling_optimization';

/**
* @var string
*/
const INCLUDE_IN_BUNDLING = 'mfrocketjavascript/general/included_in_bundling';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;

public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
/**
* mixed
*/
protected $allowedFiles;

/**
* Construct
*
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath, $sourcePath, $contentType) {
/**
* Improve bundling
*/
public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath, $sourcePath, $contentType)
{

$jsOptimization = $this->scopeConfig->getValue('mfrocketjavascript/general/enable_javaScript_bundling_optimization', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$jsOptimization = $this->scopeConfig->getValue(self::BUNDLING_OPTIMIZATION_ENABLED, ScopeInterface::SCOPE_STORE);
if ($jsOptimization) {
$includeInBundling = $this->scopeConfig->getValue('mfrocketjavascript/general/included_in_bundling', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$allowedFiles = str_replace("\r","\n", $includeInBundling);
$allowedFiles = explode("\n", $allowedFiles);

foreach ($allowedFiles as $key => $allowedFile) {
$allowedFiles[$key] = trim($allowedFile);
if (empty($allowedFiles[$key])) {
unset($allowedFiles[$key]);
}
}

foreach ($allowedFiles as $allowed) {
$allowedFiles[] = str_replace('.min.js', '.js', $allowed);
}
$allowedFiles = $this->getAllowedFiles();

$include = false;
foreach ($allowedFiles as $allowedFile) {
Expand All @@ -42,8 +66,35 @@ public function aroundAddFile(RequireJs $subject, callable $proceed, $filePath,
if (!$include) {
return true;
}

}
return $proceed($filePath, $sourcePath, $contentType);
}
}

public function getAllowedFiles()
{
if (null === $this->allowedFiles) {
$includeInBundling = $this->scopeConfig->getValue(self::INCLUDE_IN_BUNDLING, ScopeInterface::SCOPE_STORE);
$allowedFiles = str_replace("\r", "\n", $includeInBundling);
$allowedFiles = explode("\n", $allowedFiles);

foreach ($allowedFiles as $key => $allowedFile) {
$allowedFiles[$key] = trim($allowedFile);
if (empty($allowedFiles[$key])) {
unset($allowedFiles[$key]);
}
}

foreach ($allowedFiles as $allowed) {
if (false !== strpos($allowed, '.min.js')) {
$allowedFiles[] = str_replace('.min.js', '.js', $allowed);
} else {
$allowedFiles[] = str_replace('.js', '.min.js', $allowed);
}
}

$this->allowedFiles = $allowedFiles;
}

return $this->allowedFiles;
}
}
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "magefan/module-rocketjavascript",
"description": "Move all javascripts to the footer on Magento 2 store.",
"require": {
"php": "~5.6.5 || >=7.0",
"magefan/module-community" : ">=2.0.1"
},
"type": "magento2-module",
"version": "2.0.0",
"version": "2.1.0",
"license": "OSL-3.0",
"authors": [
{
Expand Down
2 changes: 1 addition & 1 deletion etc/acl.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" ?>
<!--
/**
* Copyright © 2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
Expand Down
11 changes: 5 additions & 6 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?xml version="1.0" ?>
<!--
/**
* Copyright © 2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="magefan" sortOrder="110" translate="label">
<label>Magefan Extensions</label>
</tab>
<section id="mfrocketjavascript" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
<label>Rocket JavasSript</label>
<tab>magefan</tab>
Expand All @@ -27,15 +24,17 @@
<comment>If enabled all JavaScript on storefront will be moved to the end of the page.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="enable_javaScript_bundling_optimization" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="select">
<field id="enable_js_bundling_optimization" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="select">
<label>Enable JavaScript Bundling Optimization</label>
<comment>Please note that this option only works with enabled JavaScript Bundling (Configuration > Advanced > Developer > JavaScript Settings > Enable JavaScript Bundling).</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="included_in_bundling" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="40" translate="label" type="textarea">
<label>Included In Bundling</label>
<depends>
<field id="enable_javaScript_bundling_optimization">1</field>
<field id="enable_js_bundling_optimization">1</field>
</depends>
<comment>List of files that included in JS bundle.</comment>
</field>
</group>
</section>
Expand Down
8 changes: 8 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Deploy\Package\Bundle\RequireJs">
<plugin name="magefan_rocketjavascript_plugin_deploy_package_bundle_requirejs"
Expand Down
2 changes: 1 addition & 1 deletion etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
Expand Down
8 changes: 6 additions & 2 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?xml version="1.0" ?>
<!--
/**
* Copyright © 2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magefan_RocketJavaScript" setup_version="2.0.0"/>
<module name="Magefan_RocketJavaScript" setup_version="2.1.0">
<sequence>
<module name="Magefan_Community"/>
</sequence>
</module>
</config>
2 changes: 1 addition & 1 deletion registration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
Expand Down

0 comments on commit fa9517b

Please sign in to comment.