Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
40.00% |
2 / 5 |
CRAP | |
87.18% |
34 / 39 |
Minifier | |
0.00% |
0 / 1 |
|
40.00% |
2 / 5 |
16.54 | |
87.18% |
34 / 39 |
open | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
close | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
minify | |
0.00% |
0 / 1 |
7.99 | |
72.73% |
8 / 11 |
|||
shouldMinify | |
0.00% |
0 / 1 |
5.07 | |
85.71% |
6 / 7 |
|||
compileMinify | |
0.00% |
0 / 1 |
2.00 | |
92.86% |
13 / 14 |
<?php | |
/** | |
* Part of the Caffeinated PHP packages. | |
* | |
* MIT License and copyright information bundled with this package in the LICENSE file | |
*/ | |
namespace Radic\BladeExtensions\Helpers; | |
/** | |
* This is the Minifier. | |
* | |
* @package Radic\BladeExtensions | |
* @author Caffeinated Dev Team | |
* @copyright Copyright (c) 2015, Caffeinated | |
* @license https://tldrlegal.com/license/mit-license MIT License | |
*/ | |
class Minifier | |
{ | |
/** | |
* @var | |
*/ | |
protected $type; | |
/** | |
* open | |
* | |
* @param $type | |
*/ | |
public function open($type) | |
{ | |
$this->type = trim(strtolower($type)); | |
ob_start(); | |
} | |
/** | |
* close | |
*/ | |
public function close() | |
{ | |
$code = ob_get_clean(); | |
echo $this->minify($this->type, $code); | |
$this->type = null; | |
} | |
/** | |
* minify | |
* | |
* @param $type | |
* @param $code | |
* @return string | |
*/ | |
protected function minify($type, $code) | |
{ | |
$types = ['html', 'css', 'js']; | |
if (!in_array($type, $types, true)) { | |
$typeStr = implode(', ', $types); | |
throw new \InvalidArgumentException("BladeExtensions Minifier could not minify your code, you haven't specified a valid type. Given: [{$type}]. Allowed: [{$typeStr}]"); | |
} | |
if ($type === 'html') { | |
return $this->compileMinify($code); | |
} elseif ($type === 'css' && class_exists('MatthiasMullie\Minify\CSS')) { | |
return with(new \MatthiasMullie\Minify\CSS($code))->execute(); | |
} elseif ($type === 'js' && class_exists('MatthiasMullie\Minify\CSS')) { | |
return with(new \MatthiasMullie\Minify\JS($code))->execute(); | |
} else { | |
return $code; | |
} | |
} | |
/** | |
* | |
* @author https://github.com/yocmen/html-minify | |
* @param string $value the contents of the view file | |
* | |
* @return bool | |
*/ | |
protected function shouldMinify($value) | |
{ | |
if (preg_match('/skipmin/', $value) | |
|| preg_match('/<(pre|textarea)/', $value) | |
|| preg_match('/<script[^\??>]*>[^<\/script>]/', $value) | |
|| preg_match('/value=("|\')(.*)([ ]{2,})(.*)("|\')/', $value) | |
) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
/** | |
* Compress the HTML output before saving it | |
* | |
* @author https://github.com/yocmen/html-minify | |
* @param string $value the contents of the view file | |
* | |
* @return string | |
*/ | |
protected function compileMinify($value) | |
{ | |
if ($this->shouldMinify($value)) { | |
$replace = [ | |
'/<!--[^\[](.*?)[^\]]-->/s' => '', | |
'/<\?php/' => '<?php ', | |
'/\n([\S])/' => ' $1', | |
'/\r/' => '', | |
'/\n/' => '', | |
'/\t/' => ' ', | |
'/ +/' => ' ', | |
'/>\s</' => '><' | |
]; | |
return preg_replace( | |
array_keys($replace), | |
array_values($replace), | |
$value | |
); | |
} else { | |
return $value; | |
} | |
} | |
} |