Skip to content

Commit

Permalink
Add Features
Browse files Browse the repository at this point in the history
  • Loading branch information
hryvinskyi committed Mar 23, 2019
1 parent c74e221 commit 4ec3b5d
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Config extends AbstractHelper

/**
* @param string $scopeType
* @param null $scopeCode
* @param null|string $scopeCode
*
* @return bool
*/
Expand All @@ -43,7 +43,7 @@ public function isEnabled(

/**
* @param string $scopeType
* @param null $scopeCode
* @param null|string $scopeCode
*
* @return string
*/
Expand All @@ -60,7 +60,7 @@ public function getDisableAttribute(

/**
* @param string $scopeType
* @param null $scopeCode
* @param null|string $scopeCode
*
* @return bool
*/
Expand Down
14 changes: 12 additions & 2 deletions Model/MinifyJs.php → Model/Minify/MinifyJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

declare(strict_types=1);

namespace Hryvinskyi\DeferJs\Model;
namespace Hryvinskyi\DeferJs\Model\Minify;

use JShrink\Minifier;

Expand All @@ -16,6 +16,13 @@
*/
class MinifyJs implements MinifyJsInterface
{
/**
* MinifyJs constructor.
*/
public function __construct() {

}

/**
* @param array $scripts
*
Expand All @@ -27,7 +34,10 @@ public function execute(array $scripts): array
foreach ($scripts as &$script) {
try {
$script = Minifier::minify($script);
} catch (\Exception $exception) {}
} catch (\Exception $exception) {
// Remove comments
$script = preg_replace("/[^:']\/\/.*/", '', $script);
}

$script = preg_replace('!\s+!', ' ', $script);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
declare(strict_types=1);


namespace Hryvinskyi\DeferJs\Model;
namespace Hryvinskyi\DeferJs\Model\Minify;

/**
* Class MinifyJsInterface
Expand Down
14 changes: 12 additions & 2 deletions Model/MoveJsToFooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Hryvinskyi\DeferJs\Model;

use Hryvinskyi\DeferJs\Helper\Config;
use Hryvinskyi\DeferJs\Model\Minify\MinifyJsInterface;
use Hryvinskyi\DeferJs\Model\PassesValidator\ValidateSkipper;
use Magento\Framework\App\Response\Http;

/**
Expand All @@ -27,18 +29,26 @@ class MoveJsToFooter implements MoveJsToFooterInterface
*/
private $minifyJs;

/**
* @var ValidateSkipper
*/
private $validateSkipper;

/**
* MoveJsToFooter constructor.
*
* @param Config $config
* @param MinifyJsInterface $minifyJs
* @param ValidateSkipper $validateSkipper
*/
public function __construct(
Config $config,
MinifyJsInterface $minifyJs
MinifyJsInterface $minifyJs,
ValidateSkipper $validateSkipper
) {
$this->config = $config;
$this->minifyJs = $minifyJs;
$this->validateSkipper = $validateSkipper;
}

/**
Expand Down Expand Up @@ -66,7 +76,7 @@ public function execute(Http $http)
$len = $end + strlen($scriptEnd) - $start;
$script = substr($html, $start, $len);

if (stripos($script, $this->config->getDisableAttribute()) !== false) {
if ($this->validateSkipper->execute($script, $http)) {
$start++;
continue;
}
Expand Down
53 changes: 53 additions & 0 deletions Model/PassesValidator/EntityList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Model\PassesValidator;

/**
* Class EntityList
*/
class EntityList
{
/**
* @var string[]
*/
private $entityTypes = [];


/**
* EntityList constructor.
*
* @param string[] $entityTypes
*/
public function __construct(
$entityTypes = []
) {
$this->entityTypes = $entityTypes;
}

/**
* Retrieve list of entities
*
* @return ValidatorInterface[]
*/
public function getList(): array
{
return $this->entityTypes;
}

/**
* @param string $code
*
* @return ValidatorInterface
*/
public function getEntityByCode(string $code): ValidatorInterface
{
return $this->entityTypes[$code];
}
}
52 changes: 52 additions & 0 deletions Model/PassesValidator/ValidateSkipper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Model\PassesValidator;

use Hryvinskyi\Base\Helper\VarDumper;
use Magento\Framework\App\Response\Http;

/**
* Class Validate
*/
class ValidateSkipper
{
/**
* @var EntityList
*/
private $deferJsPassesValidators;

/**
* Validate constructor.
*
* @param EntityList $deferJsPassesValidators
*/
public function __construct(
EntityList $deferJsPassesValidators
) {
$this->deferJsPassesValidators = $deferJsPassesValidators;
}

/**
* @param string $script
* @param Http $http
*
* @return bool
*/
public function execute(string $script, Http $http): bool
{
foreach ($this->deferJsPassesValidators->getList() as $deferJsPassesValidator) {
if($deferJsPassesValidator->validate($script, $http)) {
return true;
}
}

return false;
}
}
25 changes: 25 additions & 0 deletions Model/PassesValidator/ValidatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Model\PassesValidator;

use Magento\Framework\App\Response\Http;

interface ValidatorInterface
{
/**
* Validator function, handle javascript or not
*
* @param string $script
* @param Http $http
*
* @return bool
*/
public function validate(string $script, Http $http): bool;
}
33 changes: 33 additions & 0 deletions Model/PassesValidator/Validators/SkipGoogleTagManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\Model\PassesValidator\Validators;

use Hryvinskyi\DeferJs\Model\PassesValidator\ValidatorInterface;
use Magento\Framework\App\Response\Http;

/**
* Class SkipGoogleTagManager
*/
class SkipGoogleTagManager implements ValidatorInterface
{
/**
* Validator function, handle javascript or not
*
* @param string $script
* @param Http $http
*
* @return bool
*/
public function validate(string $script, Http $http): bool
{
return !!preg_match("/.*?googletagmanager\.com.*?/s", $script);
}
}
53 changes: 53 additions & 0 deletions Model/PassesValidator/Validators/SkipScriptByAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Model\PassesValidator\Validators;

use Hryvinskyi\DeferJs\Helper\Config;
use Hryvinskyi\DeferJs\Model\PassesValidator\ValidatorInterface;
use Magento\Framework\App\Response\Http;

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

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

/**
* Validator function, handle javascript or not
*
* @param string $script
* @param Http $http
*
* @return bool
*/
public function validate(string $script, Http $http): bool
{
$return = false;

if (stripos($script, $this->config->getDisableAttribute()) !== false) {
$return = true;
}

return $return;
}
}
8 changes: 4 additions & 4 deletions Plugin/MoveJsToFooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Closure;
use Hryvinskyi\DeferJs\Helper\Config;
use Hryvinskyi\DeferJs\Model\MoveJsToFooterInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http;
use Magento\Framework\Controller\ResultInterface;

Expand All @@ -31,7 +31,7 @@ class MoveJsToFooter
/**
* Request HTTP
*
* @var RequestInterface
* @var RequestHttp
*/
private $request;

Expand All @@ -46,12 +46,12 @@ class MoveJsToFooter
* MoveJsToFooter constructor.
*
* @param Config $config
* @param RequestInterface $request
* @param RequestHttp $request
* @param MoveJsToFooterInterface $moveJsToFooter
*/
public function __construct(
Config $config,
RequestInterface $request,
RequestHttp $request,
MoveJsToFooterInterface $moveJsToFooter
) {
$this->config = $config;
Expand Down
Loading

0 comments on commit 4ec3b5d

Please sign in to comment.