From 863513be80ae6d3b79e418e7d48d83d42f42b99a Mon Sep 17 00:00:00 2001 From: Semenov Date: Fri, 30 Oct 2020 12:23:25 +0300 Subject: [PATCH] New option cssFileCompileByGroups --- CHANGELOG.md | 5 ++ README.md | 4 +- src/AssetsAutoCompressComponent.php | 117 ++++++++++++++++++++++++---- 3 files changed, 112 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38e6214..c2bc77d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ============== +1.4.3 +----------------- + * New option cssFileCompileByGroups — Enables the compilation of files in groups rather than in a single file. Works only when the $cssFileCompile option is enabled + * New option jsFileCompileByGroups — Enables the compilation of files in groups rather than in a single file. Works only when the $jsFileCompile option is enabled + 1.4.2 ----------------- * Fixed: https://github.com/skeeks-semenov/yii2-assets-auto-compress/issues/51 diff --git a/README.md b/README.md index 80ce126..25bbcb0 100644 --- a/README.md +++ b/README.md @@ -66,14 +66,16 @@ How to use 'jsCompressFlaggedComments' => true, //Cut comments during processing js 'cssCompress' => true, //Enable minification css in html code - + 'cssFileCompile' => true, //Turning association css files + 'cssFileCompileByGroups' => false //Enables the compilation of files in groups rather than in a single file. Works only when the $cssFileCompile option is enabled 'cssFileRemouteCompile' => false, //Trying to get css files to which the specified path as the remote file, skchat him to her. 'cssFileCompress' => true, //Enable compression and processing before being stored in the css file 'cssFileBottom' => false, //Moving down the page css files 'cssFileBottomLoadOnJs' => false, //Transfer css file down the page and uploading them using js 'jsFileCompile' => true, //Turning association js files + 'jsFileCompileByGroups' => false //Enables the compilation of files in groups rather than in a single file. Works only when the $jsFileCompile option is enabled 'jsFileRemouteCompile' => false, //Trying to get a js files to which the specified path as the remote file, skchat him to her. 'jsFileCompress' => true, //Enable compression and processing js before saving a file 'jsFileCompressFlaggedComments' => true, //Cut comments during processing js diff --git a/src/AssetsAutoCompressComponent.php b/src/AssetsAutoCompressComponent.php index d35c211..e25dbde 100644 --- a/src/AssetsAutoCompressComponent.php +++ b/src/AssetsAutoCompressComponent.php @@ -8,7 +8,6 @@ namespace skeeks\yii2\assetsAuto; -use skeeks\yii2\assetsAuto\vendor\HtmlCompressor; use yii\base\BootstrapInterface; use yii\base\Component; use yii\base\Event; @@ -73,6 +72,12 @@ class AssetsAutoCompressComponent extends Component implements BootstrapInterfac */ public $cssFileCompile = true; + /** + * Enables the compilation of files in groups rather than in a single file. Works only when the $cssFileCompile option is enabled + * @var bool + */ + public $cssFileCompileByGroups = false; + /** * Trying to get css files to which the specified path as the remote file, skchat him to her. * @var bool @@ -104,6 +109,12 @@ class AssetsAutoCompressComponent extends Component implements BootstrapInterfac */ public $jsFileCompile = true; + /** + * Enables the compilation of files in groups rather than in a single file. Works only when the $jsFileCompile option is enabled + * @var bool + */ + public $jsFileCompileByGroups = false; + /** * @var array */ @@ -218,7 +229,7 @@ public function bootstrap($app) $app->response->on(\yii\web\Response::EVENT_BEFORE_SEND, function (\yii\base\Event $event) use ($app) { $response = $event->sender; - if ($this->enabled && ($this->htmlFormatter instanceof IFormatter) && $response->format == \yii\web\Response::FORMAT_HTML && !$app->request->isAjax && !$app->request->isPjax) { + if ($this->enabled && ($this->htmlFormatter instanceof IFormatter) && $response->format == \yii\web\Response::FORMAT_HTML && !$app->request->isAjax && !$app->request->isPjax) { if (!empty($response->data)) { $response->data = $this->_processingHtml($response->data); } @@ -236,15 +247,21 @@ public function bootstrap($app) protected function _processing(View $view) { //Компиляция файлов js в один. + //echo "
" . print_r($view->jsFiles, true);die;
         if ($view->jsFiles && $this->jsFileCompile) {
             \Yii::beginProfile('Compress js files');
             foreach ($view->jsFiles as $pos => $files) {
                 if ($files) {
-                    $view->jsFiles[$pos] = $this->_processingJsFiles($files);
+                    if ($this->jsFileCompileByGroups) {
+                        $view->jsFiles[$pos] = $this->_processAndGroupJsFiles($files);
+                    } else {
+                        $view->jsFiles[$pos] = $this->_processingJsFiles($files);
+                    }
                 }
             }
             \Yii::endProfile('Compress js files');
         }
+        //echo "
" . print_r($view->jsFiles, true);die;
 
         //Compiling js code that is found in the html code of the page.
         if ($view->js && $this->jsCompress) {
@@ -261,8 +278,11 @@ protected function _processing(View $view)
         //Compiling css files
         if ($view->cssFiles && $this->cssFileCompile) {
             \Yii::beginProfile('Compress css files');
-
-            $view->cssFiles = $this->_processingCssFiles($view->cssFiles);
+            if ($this->cssFileCompileByGroups) {
+                $view->cssFiles = $this->_processAndGroupCssFiles($view->cssFiles);
+            } else {
+                $view->cssFiles = $this->_processingCssFiles($view->cssFiles);
+            }
             \Yii::endProfile('Compress css files');
         }
 
@@ -313,6 +333,55 @@ protected function _processing(View $view)
             \Yii::endProfile('Moving css files bottom');
         }
     }
+
+    /**
+     * @param array $files
+     */
+    protected function _processAndGroupJsFiles($files = [])
+    {
+        if (!$files) {
+            return [];
+        }
+
+        $result = [];
+        $groupedFiles = $this->_getGroupedFiles($files);
+        foreach ($groupedFiles as $files)
+        {
+            $resultGroup = $this->_processingJsFiles($files);
+            $result = ArrayHelper::merge($result, $resultGroup);
+        }
+        
+        return $result;
+        echo "
" . print_r($result, true); die;
+
+    }
+
+    public function _getGroupedFiles($files)
+    {
+        $result = [];
+
+        $lastKey = null;
+        $tmpData = [];
+        $counter = 0;
+        foreach ($files as $fileCode => $fileTag) {
+            list($one, $two, $key) = explode("/", $fileCode);
+
+            $counter ++;
+
+            if ($key != $lastKey && $counter > 1) {
+                $result[] = $tmpData;
+                $tmpData = [];
+                $tmpData[$fileCode] = $fileTag;
+            } else {
+                $tmpData[$fileCode] = $fileTag;
+            }
+
+            $lastKey = $key;
+        }
+
+        return $result;
+    }
+
     /**
      * @param array $files
      * @return array
@@ -435,7 +504,7 @@ public function readLocalFile($filePath)
             throw new \Exception("Unable to open file: '{$filePath}'");
         }
         $filesSize = filesize($filePath);
-        if($filesSize){
+        if ($filesSize) {
             return fread($file, $filesSize);
         }
         fclose($file);
@@ -481,6 +550,28 @@ protected function _processingJs($parts)
 
         return $result;
     }
+    
+    /**
+     * @param array $files
+     */
+    protected function _processAndGroupCssFiles($files = [])
+    {
+        if (!$files) {
+            return [];
+        }
+
+        $result = [];
+        $groupedFiles = $this->_getGroupedFiles($files);
+        foreach ($groupedFiles as $files)
+        {
+            $resultGroup = $this->_processingCssFiles($files);
+            $result = ArrayHelper::merge($result, $resultGroup);
+        }
+        
+        return $result;
+
+    }
+    
     /**
      * @param array $files
      * @return array
@@ -616,9 +707,9 @@ protected function _processingHtml($html)
     {
         if ($this->htmlFormatter instanceof IFormatter) {
             $r = new \ReflectionClass($this->htmlFormatter);
-            \Yii::beginProfile('Format html: ' . $r->getName());
-                $result = $this->htmlFormatter->format($html);
-            \Yii::endProfile('Format html: ' . $r->getName());
+            \Yii::beginProfile('Format html: '.$r->getName());
+            $result = $this->htmlFormatter->format($html);
+            \Yii::endProfile('Format html: '.$r->getName());
             return $result;
         }
 
@@ -629,9 +720,9 @@ protected function _processingHtml($html)
 
 
     /**
-     * @deprecated >= 1.4
      * @param $value
      * @return $this
+     * @deprecated >= 1.4
      */
     public function setHtmlCompress($value)
     {
@@ -639,20 +730,20 @@ public function setHtmlCompress($value)
     }
 
     /**
-     * @deprecated >= 1.4
      * @param $value
      * @return $this
+     * @deprecated >= 1.4
      */
     public function getHtmlCompress()
     {
         return $this;
     }
     /**
-     * @deprecated >= 1.4
      * @param $value array options for compressing output result
      *   * extra - use more compact algorithm
      *   * no-comments - cut all the html comments
      * @return $this
+     * @deprecated >= 1.4
      */
     public function setHtmlCompressOptions($value)
     {
@@ -660,9 +751,9 @@ public function setHtmlCompressOptions($value)
     }
 
     /**
-     * @deprecated >= 1.4
      * @param $value
      * @return $this
+     * @deprecated >= 1.4
      */
     public function getHtmlCompressOptions()
     {