Skip to content

Commit

Permalink
Added execute by url pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
hryvinskyi committed May 13, 2020
1 parent 77130b9 commit 6fe372a
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 17 deletions.
33 changes: 33 additions & 0 deletions Block/Adminhtml/System/ExcludeUrlPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright (c) 2020. Volodymyr Hryvinskyi. All rights reserved.
* @author: <mailto:[email protected]>
* @github: <https://github.com/hryvinskyi>
*/

declare(strict_types=1);

namespace Hryvinskyi\DeferJs\Block\Adminhtml\System;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
* Class ExcludeUrlPattern
*/
class ExcludeUrlPattern extends AbstractFieldArray
{
/**
* @return void
*/
protected function _construct()
{
$this->addColumn('pattern', [
'label' => __('Expression')
]);

$this->_addAfter = false;
$this->_addButtonLabel = __('Add');

parent::_construct();
}
}
36 changes: 28 additions & 8 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config extends AbstractHelper
const XML_HRYVINSKYI_DEFER_JS_MINIFY_BODY_SCRIPTS = 'hryvinskyi_defer_js/general/minify_body_scripts';
const XML_HRYVINSKYI_DEFER_JS_EXCLUDE_CONTROLLERS = 'hryvinskyi_defer_js/general/exclude_controllers';
const XML_HRYVINSKYI_DEFER_JS_EXCLUDE_PATHS = 'hryvinskyi_defer_js/general/exclude_paths';
const XML_HRYVINSKYI_DEFER_JS_EXCLUDE_URL_PATTERN = 'hryvinskyi_defer_js/general/exclude_url_pattern';

/**
* @param string $scopeType
Expand All @@ -33,7 +34,7 @@ class Config extends AbstractHelper
* @return bool
*/
public function isEnabled(
string $scopeType = ScopeInterface::SCOPE_WEBSITE,
string $scopeType = ScopeInterface::SCOPE_STORE,
$scopeCode = null
): bool {
return $this->scopeConfig->isSetFlag(
Expand All @@ -50,10 +51,10 @@ public function isEnabled(
* @return string
*/
public function getDisableAttribute(
string $scopeType = ScopeInterface::SCOPE_WEBSITE,
string $scopeType = ScopeInterface::SCOPE_STORE,
$scopeCode = null
): string {
return $this->scopeConfig->getValue(
return (string)$this->scopeConfig->getValue(
self::XML_HRYVINSKYI_DEFER_JS_DISABLE_ATTRIBUTE,
$scopeType,
$scopeCode
Expand All @@ -67,7 +68,7 @@ public function getDisableAttribute(
* @return bool
*/
public function isMinifyBodyScript(
string $scopeType = ScopeInterface::SCOPE_WEBSITE,
string $scopeType = ScopeInterface::SCOPE_STORE,
$scopeCode = null
): bool {
return $this->scopeConfig->isSetFlag(
Expand All @@ -86,10 +87,10 @@ public function isMinifyBodyScript(
* @return string
*/
public function getExcludeControllers(
string $scopeType = ScopeInterface::SCOPE_WEBSITE,
string $scopeType = ScopeInterface::SCOPE_STORE,
$scopeCode = null
): string {
return $this->scopeConfig->getValue(
return (string)$this->scopeConfig->getValue(
self::XML_HRYVINSKYI_DEFER_JS_EXCLUDE_CONTROLLERS,
$scopeType,
$scopeCode
Expand All @@ -105,13 +106,32 @@ public function getExcludeControllers(
* @return string
*/
public function getExcludePaths(
string $scopeType = ScopeInterface::SCOPE_WEBSITE,
string $scopeType = ScopeInterface::SCOPE_STORE,
$scopeCode = null
): string {
return $this->scopeConfig->getValue(
return (string)$this->scopeConfig->getValue(
self::XML_HRYVINSKYI_DEFER_JS_EXCLUDE_PATHS,
$scopeType,
$scopeCode
);
}

/**
* Return Excluded URL pattern
*
* @param string $scopeType
* @param null|string $scopeCode
*
* @return string
*/
public function getExcludeUrlPattern(
string $scopeType = ScopeInterface::SCOPE_STORE,
$scopeCode = null
): string {
return (string)$this->scopeConfig->getValue(
self::XML_HRYVINSKYI_DEFER_JS_EXCLUDE_URL_PATTERN,
$scopeType,
$scopeCode
);
}
}
126 changes: 126 additions & 0 deletions Model/PassesValidator/Validators/SkipScriptsByURLPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Copyright (c) 2020. Volodymyr Hryvinskyi. All rights reserved.
* @author: <mailto:[email protected]>
* @github: <https://github.com/hryvinskyi>
*/

declare(strict_types=1);

namespace Hryvinskyi\DeferJs\Model\PassesValidator\Validators;

use Hryvinskyi\Base\Helper\ArrayHelper;
use Hryvinskyi\Base\Helper\Json;
use Hryvinskyi\DeferJs\Helper\Config;
use Hryvinskyi\DeferJs\Model\PassesValidator\ValidatorInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http;

/**
* Class SkipScriptsByURLPattern
*/
class SkipScriptsByURLPattern implements ValidatorInterface
{
/**
* @var Config
*/
private $config;

/**
* @var RequestHttp
*/
private $request;

/**
* SkipScriptsByController constructor.
*
* @param Config $config
* @param RequestHttp $request
*/
public function __construct(
Config $config,
RequestHttp $request
) {
$this->config = $config;
$this->request = $request;
}

/**
* Validator function, handle javascript or not
*
* @param string $script
* @param Http $http
*
* @return bool
*/
public function validate(string $script, Http $http): bool
{
$executeUrlPattern = Json::decode($this->config->getExcludeUrlPattern());
$executeUrlPattern = ArrayHelper::getColumn($executeUrlPattern, 'pattern', false);

$return = false;

foreach ($executeUrlPattern as $pattern) {
if ($this->checkPattern($this->request->getRequestUri(), $pattern)) {
$return = true;
break;
}
}

return $return;
}

/**
* @param $string
* @param $pattern
*
* @return bool
*/
public function checkPattern(string $string, string $pattern): bool
{
$parts = explode('*', $pattern);
$index = 0;

$shouldBeFirst = true;

foreach ($parts as $part) {
if ($part == '') {
$shouldBeFirst = false;
continue;
}

$index = strpos($string, $part, $index);

if ($index === false) {
return false;
}

if ($shouldBeFirst && $index > 0) {
return false;
}

$shouldBeFirst = false;
$index += strlen($part);
}

if (count($parts) == 1) {
return $string == $pattern;
}

$last = end($parts);

if ($last == '') {
return true;
}

if (strrpos($string, $last) === false) {
return false;
}

if (strlen($string) - strlen($last) - strrpos($string, $last) > 0) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright (c) 2019. Volodymyr Hryvinskyi. All rights reserved.
* @author: <mailto:[email protected]>
* @github: <https://github.com/hryvinskyi>
*/

declare(strict_types=1);

namespace Hryvinskyi\DeferJs\Test\Unit\Model\PassesValidator\Validators;

use Hryvinskyi\DeferJs\Helper\Config;
use Hryvinskyi\DeferJs\Model\PassesValidator\Validators\SkipScriptsByURLPattern;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;

class SkipScriptsByURLPatternTest extends TestCase
{
/**
* @var Config|PHPUnit_Framework_MockObject_MockObject
*/
private $config;

/**
* @var RequestHttp|PHPUnit_Framework_MockObject_MockObject
*/
private $request;

/**
* @var Http
*/
private $http;

/**
* @var SkipScriptsByURLPattern
*/
private $model;

/**
* Sets up the fixture
*/
protected function setUp()
{
$this->config = $this->createPartialMock(
Config::class,
['getExcludeUrlPattern']
);

$this->request = $this->createPartialMock(
RequestHttp::class,
['getRequestUri']
);

$this->config->expects($this->any())->method('getExcludeUrlPattern')
->willReturn('{"_1589359003110_110":{"pattern":"*argus-all-weather*"}}');


$this->http = (new ObjectManager($this))->getObject(Http::class);
$this->model = (new ObjectManager($this))->getObject(SkipScriptsByURLPattern::class, [
'config' => $this->config,
'request' => $this->request,
]);
}

/**
*
*/
public function testSkipScript(): void
{
$this->request->expects($this->any())->method('getRequestUri')->willReturn('/argus-all-weather-tank.html');
$this->assertEquals(true, $this->model->validate('', $this->http));
}

/**
*
*/
public function testNoSkipScript(): void
{
$this->request->expects($this->any())->method('getRequestUri')->willReturn('/someUrl.html');
$this->assertEquals(false, $this->model->validate('', $this->http));
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scriptua/magento2-base": "~2.0.0"
},
"type": "magento2-module",
"version": "1.1.3",
"version": "1.1.4",
"license": "GPL-3.0-or-later",
"autoload": {
"files": [
Expand Down
Loading

0 comments on commit 6fe372a

Please sign in to comment.