From e597d3f3d2a228101143dfb59a13a91a5d5be917 Mon Sep 17 00:00:00 2001 From: Ivan A Date: Wed, 4 Oct 2017 10:16:06 +0400 Subject: [PATCH] Changed for project environment with changed to better preg replaced --- .gitignore | 0 .scrutinizer.yml | 0 .travis.yml | 0 CHANGELOG.md | 0 Exception.php | 0 HtmlCompressor.php | 4 +- LICENSE | 0 README.md | 0 View.php | 48 +----------- components/CSS.php | 97 +++---------------------- components/JS.php | 57 ++------------- components/MinifyComponent.php | 18 ++--- composer.json | 0 composer.lock | 0 phpunit.xml.dist | 0 tests/unit/.gitignore | 0 tests/unit/TestCase.php | 0 tests/unit/bootstrap.php | 0 tests/unit/config/.gitignore | 0 tests/unit/config/main.php | 0 tests/unit/data/DependAssetBundle.php | 0 tests/unit/data/EmptyAssetBundle.php | 0 tests/unit/data/ExcludedAssetBundle.php | 0 tests/unit/data/JQueryAssetBundle.php | 0 tests/unit/data/TestAssetBundle.php | 0 tests/unit/data/source/depend.css | 0 tests/unit/data/source/depend.js | 0 tests/unit/data/source/excluded.css | 0 tests/unit/data/source/excluded.js | 0 tests/unit/data/source/for_import.css | 0 tests/unit/data/source/image.png | 0 tests/unit/data/source/test.css | 0 tests/unit/data/source/test.js | 0 tests/unit/runtime/.gitignore | 0 tests/unit/view/HtmlCompressorTest.php | 0 tests/unit/view/ViewTest.php | 0 36 files changed, 25 insertions(+), 199 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .scrutinizer.yml mode change 100644 => 100755 .travis.yml mode change 100644 => 100755 CHANGELOG.md mode change 100644 => 100755 Exception.php mode change 100644 => 100755 HtmlCompressor.php mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md mode change 100644 => 100755 View.php mode change 100644 => 100755 components/CSS.php mode change 100644 => 100755 components/JS.php mode change 100644 => 100755 components/MinifyComponent.php mode change 100644 => 100755 composer.json mode change 100644 => 100755 composer.lock mode change 100644 => 100755 phpunit.xml.dist mode change 100644 => 100755 tests/unit/.gitignore mode change 100644 => 100755 tests/unit/TestCase.php mode change 100644 => 100755 tests/unit/bootstrap.php mode change 100644 => 100755 tests/unit/config/.gitignore mode change 100644 => 100755 tests/unit/config/main.php mode change 100644 => 100755 tests/unit/data/DependAssetBundle.php mode change 100644 => 100755 tests/unit/data/EmptyAssetBundle.php mode change 100644 => 100755 tests/unit/data/ExcludedAssetBundle.php mode change 100644 => 100755 tests/unit/data/JQueryAssetBundle.php mode change 100644 => 100755 tests/unit/data/TestAssetBundle.php mode change 100644 => 100755 tests/unit/data/source/depend.css mode change 100644 => 100755 tests/unit/data/source/depend.js mode change 100644 => 100755 tests/unit/data/source/excluded.css mode change 100644 => 100755 tests/unit/data/source/excluded.js mode change 100644 => 100755 tests/unit/data/source/for_import.css mode change 100644 => 100755 tests/unit/data/source/image.png mode change 100644 => 100755 tests/unit/data/source/test.css mode change 100644 => 100755 tests/unit/data/source/test.js mode change 100644 => 100755 tests/unit/runtime/.gitignore mode change 100644 => 100755 tests/unit/view/HtmlCompressorTest.php mode change 100644 => 100755 tests/unit/view/ViewTest.php diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.scrutinizer.yml b/.scrutinizer.yml old mode 100644 new mode 100755 diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 diff --git a/CHANGELOG.md b/CHANGELOG.md old mode 100644 new mode 100755 diff --git a/Exception.php b/Exception.php old mode 100644 new mode 100755 diff --git a/HtmlCompressor.php b/HtmlCompressor.php old mode 100644 new mode 100755 index 3e3f213..d8ccc25 --- a/HtmlCompressor.php +++ b/HtmlCompressor.php @@ -126,7 +126,9 @@ private function htmlCompress($data, $options = null) // Remove HTML comments... if (array_key_exists('c', $options) || array_key_exists('no-comments', $options)) { - $out = preg_replace('/()/ms', '', $out); +// $out = preg_replace('/()/ms', '', $out); +// adding browser's condition's exceptions + $out = preg_replace('#\\s*\\s*#s', '', $out); $out = str_replace('', '', $out); } diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/View.php b/View.php old mode 100644 new mode 100755 index ee40472..63d76fc --- a/View.php +++ b/View.php @@ -190,20 +190,14 @@ class View extends \yii\web\View */ public $excludeFiles = []; - /* - * Updated params by Jaimin MosLake to give more functionality - */ - /** * @var boolean - * whether you want to use S3Bucket or not - * By default it will be false */ public $S3Upload = false ; + /** * @var boolean - * Name of awsBucket */ public $awsBucket = null ; @@ -211,64 +205,26 @@ class View extends \yii\web\View * @var boolean * It is for linking Resource folder to asset files * if Resources like images above one folder it should be "../" if two folders above "../../" - * You want to load all the images from s3 now you have images folder in root and you have dev , qa , prod folder - * and css and js inside those folder now you have to link images in those css file. - * You have to use this option to do that. */ public $assetsFolderPathPatch = null ; + /* * boolean * backend checke will help take asset from root/minify folder for backedn instead of root/backend/minifiy - * If backend and frontend has same assets and you want to use same location to store asset - * you can make thi true. By this it will use root/minify other then root/backedn/minify - * to copy files to S3 */ public $backendCheck = false ; - /* * Folder name where minified files will be kept - * Here i have devided it will be used when $backendCheck is true - * as if we are doing from web from backend / frontend to have same file hashes - * as I am using same assets folder in root for backend / frontend as to have files from same assets folder */ public $folderName = 'minify' ; - /* * will be used at _getSummaryFilesHash will fix path to have same hash value as frontend or backend when files generated from console. - * At console level this will be used as when generating from console path will be different so some adjustment path should be decalared t make path same as - * of running in web browser as console has path from console folder script - * - * so when you generate from console make modifyPath true - * and modifyPathData regarding your assets folder to console folder */ public $modifyPath = false ; public $modifyPathData = "" ; - /* - * It helps if you want to add prefix to any file as it will mostly create file name - * as {prefix}-all-in-one-{HASH}.{js/css} - * so if you want to give a prefix for certain layout - * then you can do it by this option. - * Pass layout name as array key and pass prefix name as array value - * ex : for main layout if you want newmain prefix - * you have to pass array like ["main"=>"newmain"] - * if you do not wont prefix do not do anything just live it a blank array - */ - public $layoutPrefixArray = [] ; - - /* - * Use layoutPrefixArray option for css true/false - */ - public $layoutPrefixCss = false ; - - /* - * Use layoutPrefixArray option for Js true/false - */ - public $layoutPrefixJS = false ; - - /** * @throws \processfast\yii\minify\Exception */ diff --git a/components/CSS.php b/components/CSS.php old mode 100644 new mode 100755 index 9736290..b7a7cb0 --- a/components/CSS.php +++ b/components/CSS.php @@ -2,28 +2,22 @@ /** * CSS.php * @author Revin Roman - * @link https://processfast.com + * @link https://rmrevin.com */ - namespace processfast\yii\minify\components; - +use tubalmartin\CssMin\Minifier as CSSmin; use yii\helpers\Html; - /** * Class CSS - * @package processfast\yii\minify\components + * @package rmrevin\yii\minify\components */ class CSS extends MinifyComponent { - public function export() { $cssFiles = $this->view->cssFiles; - $this->view->cssFiles = []; - $toMinify = []; - if (!empty($cssFiles)) { foreach ($cssFiles as $file => $html) { if ($this->thisFileNeedMinify($file, $html)) { @@ -35,54 +29,29 @@ public function export() } else { if (!empty($toMinify)) { $this->process($toMinify); - $toMinify = []; } - $this->view->cssFiles[$file] = $html; } } } - if (!empty($toMinify)) { $this->process($toMinify); } - unset($toMinify); } - /** * @param array $files */ protected function process(array $files) { - $hash = $this->_getSummaryFilesHash($files) ; - $resultFile = $this->view->minifyPath . DIRECTORY_SEPARATOR . $hash . '.css'; - - if( $this->view->S3Upload && $this->doesObjectExist( $resultFile , "CSS" , $hash ) ) - { - // It exist on s3 just do not do any processing - $resultFile = $this->getS3Path( $resultFile , "CSS" , $hash ); - } - else if (!file_exists($resultFile)) - { + $resultFile = $this->view->minifyPath . DIRECTORY_SEPARATOR . $this->_getSummaryFilesHash($files) . '.css'; + if (!file_exists($resultFile)) { $css = ''; - foreach ($files as $file => $html) { $path = dirname($file); - - if( $this->view->S3Upload ) - { - $assetsFolderPathPatch = $this->view->assetsFolderPathPatch ; - $pathArray = explode('assets', $path, 2); - $newPath = $pathArray[1] ; - $path = $assetsFolderPathPatch."assets".$newPath ; - } - $file = $this->getAbsoluteFilePath($file); - $content = ''; - if (!file_exists($file)) { \Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__); } elseif (!is_readable($file)) { @@ -90,72 +59,43 @@ protected function process(array $files) } else { $content = file_get_contents($file); } - $result = []; - preg_match_all('|url\(([^)]+)\)|is', $content, $m); if (!empty($m[0])) { foreach ($m[0] as $k => $v) { if (in_array(strpos($m[1][$k], 'data:'), [0, 1], true)) { continue; } - $url = str_replace(['\'', '"'], '', $m[1][$k]); - if ($this->isUrl($url)) { $result[$m[1][$k]] = $url; } else { $result[$m[1][$k]] = $path . '/' . $url; } } - $content = strtr($content, $result); } - $css .= $content; } - $this->expandImports($css); - $this->removeCssComments($css); - if ($this->view->minifyCss) { - $css = (new \CSSmin()) + $css = (new CSSmin()) ->run($css, $this->view->cssLinebreakPos); } - $charsets = false !== $this->view->forceCharset ? ('@charset "' . (string)$this->view->forceCharset . '";' . "\n") : $this->collectCharsets($css); - $imports = $this->collectImports($css); $fonts = $this->collectFonts($css); - - $content = gzencode( $charsets . $imports . $fonts . $css , 9); - file_put_contents($resultFile, $content ); - + file_put_contents($resultFile, $charsets . $imports . $fonts . $css); if (false !== $this->view->fileMode) { @chmod($resultFile, $this->view->fileMode); } - - if( $this->view->S3Upload ) - { - $resultFile = $this->uploadToS3( $resultFile , "CSS" , $hash ); - } - } - else - { - if( $this->view->S3Upload ) - { - $resultFile = $this->uploadToS3( $resultFile , "CSS" , $hash ); - } } - $file = $this->prepareResultFile($resultFile); - $this->view->cssFiles[$file] = Html::cssFile($file); } - /** * @param string $code */ @@ -165,7 +105,6 @@ protected function removeCssComments(&$code) $code = preg_replace('#/\*(?:[^*]*(?:\*(?!/))*)*\*/#', '', $code); } } - /** * @param string $code */ @@ -173,14 +112,11 @@ protected function expandImports(&$code) { if (true === $this->view->expandImports) { preg_match_all('|\@import\s([^;]+);|is', str_replace('&', '&', $code), $m); - if (!empty($m[0])) { foreach ($m[0] as $k => $v) { $import_url = $m[1][$k]; - if (!empty($import_url)) { $import_content = $this->_getImportContent($import_url); - if (!empty($import_content)) { $code = str_replace($m[0][$k], $import_content, $code); } @@ -189,7 +125,6 @@ protected function expandImports(&$code) } } } - /** * @param string $code * @return string @@ -200,7 +135,6 @@ protected function collectCharsets(&$code) return $string . ';'; }); } - /** * @param string $code * @return string @@ -211,7 +145,6 @@ protected function collectImports(&$code) return $string . ';'; }); } - /** * @param string $code * @return string @@ -222,7 +155,6 @@ protected function collectFonts(&$code) return $string; }); } - /** * @param string $code * @param string $pattern @@ -232,19 +164,14 @@ protected function collectFonts(&$code) protected function _collect(&$code, $pattern, $handler) { $result = ''; - preg_match_all($pattern, $code, $m); - foreach ($m[0] as $string) { $string = $handler($string); $code = str_replace($string, '', $code); - $result .= $string . PHP_EOL; } - return $result; } - /** * @param string $url * @return null|string @@ -252,30 +179,24 @@ protected function _collect(&$code, $pattern, $handler) protected function _getImportContent($url) { $result = null; - if ('url(' === mb_substr($url, 0, 4)) { $url = str_replace(['url(\'', 'url("', 'url(', '\')', '")', ')'], '', $url); - if (mb_substr($url, 0, 2) === '//') { $url = preg_replace('|^//|', 'http://', $url, 1); } - if (!empty($url)) { if (!in_array(mb_substr($url, 0, 4), ['http', 'ftp:'], true)) { $url = \Yii::getAlias($this->view->basePath . $url); } - $context = [ 'ssl' => [ - 'verify_peer' => false, + 'verify_peer' => false, 'verify_peer_name' => false, ], ]; - $result = file_get_contents($url, null, stream_context_create($context)); } } - return $result; } -} +} \ No newline at end of file diff --git a/components/JS.php b/components/JS.php old mode 100644 new mode 100755 index 7ec732a..ad49d72 --- a/components/JS.php +++ b/components/JS.php @@ -2,40 +2,32 @@ /** * JS.php * @author Revin Roman - * @link https://processfast.com + * @link https://rmrevin.com */ - namespace processfast\yii\minify\components; - +//use werkint\jsmin; use yii\helpers\Html; - /** * Class JS - * @package processfast\yii\minify\components + * @package rmrevin\yii\minify\components */ class JS extends MinifyComponent { - public function export() { $jsFiles = $this->view->jsFiles; - $jsPosition = $this->view->jsPosition; $jsOptions = $this->view->jsOptions; - if (!empty($jsFiles)) { foreach ($jsFiles as $position => $files) { if (false === in_array($position, $jsPosition, true)) { $this->view->jsFiles[$position] = []; - foreach ($files as $file => $html) { $this->view->jsFiles[$position][$file] = $html; } } else { $this->view->jsFiles[$position] = []; - $toMinify = []; - foreach ($files as $file => $html) { if ($this->thisFileNeedMinify($file, $html)) { if ($this->view->concatJs) { @@ -46,24 +38,19 @@ public function export() } else { if (!empty($toMinify)) { $this->process($position, $jsOptions, $toMinify); - $toMinify = []; } - $this->view->jsFiles[$position][$file] = $html; } } - if (!empty($toMinify)) { $this->process($position, $jsOptions, $toMinify); } - unset($toMinify); } } } } - /** * @param integer $position * @param array $options @@ -71,23 +58,12 @@ public function export() */ protected function process($position, $options, $files) { - $hash = $this->_getSummaryFilesHash($files) ; - $resultFile = sprintf('%s/%s.js', $this->view->minifyPath, $hash); - - if( $this->view->S3Upload && $this->doesObjectExist( $resultFile , "JS" , $hash ) ) - { - // It exist on s3 so just get - $resultFile = $this->getS3Path( $resultFile , "JS" , $hash ); - } - else if (!file_exists($resultFile)) - { + $resultFile = sprintf('%s/%s.js', $this->view->minifyPath, $this->_getSummaryFilesHash($files)); + if (!file_exists($resultFile)) { $js = ''; - foreach ($files as $file => $html) { $file = $this->getAbsoluteFilePath($file); - $content = ''; - if (!file_exists($file)) { \Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__); } elseif (!is_readable($file)) { @@ -95,42 +71,21 @@ protected function process($position, $options, $files) } else { $content .= file_get_contents($file) . ';' . "\n"; } - $js .= $content; } - $this->removeJsComments($js); - if ($this->view->minifyJs) { $js = (new \JSMin($js)) ->min(); } - - $js = gzencode( $js , 9 ); file_put_contents($resultFile, $js); - if (false !== $this->view->fileMode) { @chmod($resultFile, $this->view->fileMode); } - - if( $this->view->S3Upload ) - { - $resultFile = $this->uploadToS3( $resultFile , "JS" , $hash); - } - } - else - { - if( $this->view->S3Upload ) - { - $resultFile = $this->uploadToS3( $resultFile , "JS" , $hash); - } } - $file = $this->prepareResultFile($resultFile); - $this->view->jsFiles[$position][$file] = Html::jsFile($file, $options); } - /** * @todo * @param string $code @@ -141,4 +96,4 @@ protected function removeJsComments(&$code) //$code = preg_replace('', '', $code); } } -} +} \ No newline at end of file diff --git a/components/MinifyComponent.php b/components/MinifyComponent.php old mode 100644 new mode 100755 index a0bfd89..39cfb1d --- a/components/MinifyComponent.php +++ b/components/MinifyComponent.php @@ -278,30 +278,22 @@ protected function getFileInfo( $resultFile , $type , $hash , $typeIn = "FILEN $mime_type = null ; $fileName = null ; - - $prefix = "" ; - if(( $type == "CSS" && $this->view->layoutPrefixCss ) || ( $type == "JS" && $this->view->layoutPrefixJS )) + if( $type == "CSS" ) { + $prefix = "" ; $layout = \Yii::$app->controller->layout ; - $layoutPrefixArray = $this->view->layoutPrefixArray ; - if( is_array($layoutPrefixArray) ) + if( in_array( $layout , ["old_main","public_pages"] ) ) { - if( array_key_exists( $layout , $layoutPrefixArray ) ) - { - $prefix = $layoutPrefixArray[$layout] ; - } + $prefix = "public-pages-" ; } - } - if( $type == "CSS" ) - { $mime_type = "text/css" ; $fileName = "web-assets/".$env."/minify/".$prefix."all-in-one".$versionName."-".$hash.".css"; } else if( $type == "JS" ) { $mime_type = "application/javascript" ; - $fileName = "web-assets/".$env."/minify/".$prefix."all-in-one".$versionName."-".$hash.".js"; + $fileName = "web-assets/".$env."/minify/all-in-one".$versionName."-".$hash.".js"; } if( $typeIn == "FILENAME" ) diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 diff --git a/composer.lock b/composer.lock old mode 100644 new mode 100755 diff --git a/phpunit.xml.dist b/phpunit.xml.dist old mode 100644 new mode 100755 diff --git a/tests/unit/.gitignore b/tests/unit/.gitignore old mode 100644 new mode 100755 diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php old mode 100644 new mode 100755 diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php old mode 100644 new mode 100755 diff --git a/tests/unit/config/.gitignore b/tests/unit/config/.gitignore old mode 100644 new mode 100755 diff --git a/tests/unit/config/main.php b/tests/unit/config/main.php old mode 100644 new mode 100755 diff --git a/tests/unit/data/DependAssetBundle.php b/tests/unit/data/DependAssetBundle.php old mode 100644 new mode 100755 diff --git a/tests/unit/data/EmptyAssetBundle.php b/tests/unit/data/EmptyAssetBundle.php old mode 100644 new mode 100755 diff --git a/tests/unit/data/ExcludedAssetBundle.php b/tests/unit/data/ExcludedAssetBundle.php old mode 100644 new mode 100755 diff --git a/tests/unit/data/JQueryAssetBundle.php b/tests/unit/data/JQueryAssetBundle.php old mode 100644 new mode 100755 diff --git a/tests/unit/data/TestAssetBundle.php b/tests/unit/data/TestAssetBundle.php old mode 100644 new mode 100755 diff --git a/tests/unit/data/source/depend.css b/tests/unit/data/source/depend.css old mode 100644 new mode 100755 diff --git a/tests/unit/data/source/depend.js b/tests/unit/data/source/depend.js old mode 100644 new mode 100755 diff --git a/tests/unit/data/source/excluded.css b/tests/unit/data/source/excluded.css old mode 100644 new mode 100755 diff --git a/tests/unit/data/source/excluded.js b/tests/unit/data/source/excluded.js old mode 100644 new mode 100755 diff --git a/tests/unit/data/source/for_import.css b/tests/unit/data/source/for_import.css old mode 100644 new mode 100755 diff --git a/tests/unit/data/source/image.png b/tests/unit/data/source/image.png old mode 100644 new mode 100755 diff --git a/tests/unit/data/source/test.css b/tests/unit/data/source/test.css old mode 100644 new mode 100755 diff --git a/tests/unit/data/source/test.js b/tests/unit/data/source/test.js old mode 100644 new mode 100755 diff --git a/tests/unit/runtime/.gitignore b/tests/unit/runtime/.gitignore old mode 100644 new mode 100755 diff --git a/tests/unit/view/HtmlCompressorTest.php b/tests/unit/view/HtmlCompressorTest.php old mode 100644 new mode 100755 diff --git a/tests/unit/view/ViewTest.php b/tests/unit/view/ViewTest.php old mode 100644 new mode 100755