Skip to content

Commit 863513b

Browse files
New option cssFileCompileByGroups
1 parent c01b7a7 commit 863513b

File tree

3 files changed

+112
-14
lines changed

3 files changed

+112
-14
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
==============
33

4+
1.4.3
5+
-----------------
6+
* New option cssFileCompileByGroups — Enables the compilation of files in groups rather than in a single file. Works only when the $cssFileCompile option is enabled
7+
* New option jsFileCompileByGroups — Enables the compilation of files in groups rather than in a single file. Works only when the $jsFileCompile option is enabled
8+
49
1.4.2
510
-----------------
611
* Fixed: https://github.com/skeeks-semenov/yii2-assets-auto-compress/issues/51

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ How to use
6666
'jsCompressFlaggedComments' => true, //Cut comments during processing js
6767

6868
'cssCompress' => true, //Enable minification css in html code
69-
69+
7070
'cssFileCompile' => true, //Turning association css files
71+
'cssFileCompileByGroups' => false //Enables the compilation of files in groups rather than in a single file. Works only when the $cssFileCompile option is enabled
7172
'cssFileRemouteCompile' => false, //Trying to get css files to which the specified path as the remote file, skchat him to her.
7273
'cssFileCompress' => true, //Enable compression and processing before being stored in the css file
7374
'cssFileBottom' => false, //Moving down the page css files
7475
'cssFileBottomLoadOnJs' => false, //Transfer css file down the page and uploading them using js
7576

7677
'jsFileCompile' => true, //Turning association js files
78+
'jsFileCompileByGroups' => false //Enables the compilation of files in groups rather than in a single file. Works only when the $jsFileCompile option is enabled
7779
'jsFileRemouteCompile' => false, //Trying to get a js files to which the specified path as the remote file, skchat him to her.
7880
'jsFileCompress' => true, //Enable compression and processing js before saving a file
7981
'jsFileCompressFlaggedComments' => true, //Cut comments during processing js

src/AssetsAutoCompressComponent.php

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace skeeks\yii2\assetsAuto;
1010

11-
use skeeks\yii2\assetsAuto\vendor\HtmlCompressor;
1211
use yii\base\BootstrapInterface;
1312
use yii\base\Component;
1413
use yii\base\Event;
@@ -73,6 +72,12 @@ class AssetsAutoCompressComponent extends Component implements BootstrapInterfac
7372
*/
7473
public $cssFileCompile = true;
7574

75+
/**
76+
* Enables the compilation of files in groups rather than in a single file. Works only when the $cssFileCompile option is enabled
77+
* @var bool
78+
*/
79+
public $cssFileCompileByGroups = false;
80+
7681
/**
7782
* Trying to get css files to which the specified path as the remote file, skchat him to her.
7883
* @var bool
@@ -104,6 +109,12 @@ class AssetsAutoCompressComponent extends Component implements BootstrapInterfac
104109
*/
105110
public $jsFileCompile = true;
106111

112+
/**
113+
* Enables the compilation of files in groups rather than in a single file. Works only when the $jsFileCompile option is enabled
114+
* @var bool
115+
*/
116+
public $jsFileCompileByGroups = false;
117+
107118
/**
108119
* @var array
109120
*/
@@ -218,7 +229,7 @@ public function bootstrap($app)
218229
$app->response->on(\yii\web\Response::EVENT_BEFORE_SEND, function (\yii\base\Event $event) use ($app) {
219230
$response = $event->sender;
220231

221-
if ($this->enabled && ($this->htmlFormatter instanceof IFormatter) && $response->format == \yii\web\Response::FORMAT_HTML && !$app->request->isAjax && !$app->request->isPjax) {
232+
if ($this->enabled && ($this->htmlFormatter instanceof IFormatter) && $response->format == \yii\web\Response::FORMAT_HTML && !$app->request->isAjax && !$app->request->isPjax) {
222233
if (!empty($response->data)) {
223234
$response->data = $this->_processingHtml($response->data);
224235
}
@@ -236,15 +247,21 @@ public function bootstrap($app)
236247
protected function _processing(View $view)
237248
{
238249
//Компиляция файлов js в один.
250+
//echo "<pre><code>" . print_r($view->jsFiles, true);die;
239251
if ($view->jsFiles && $this->jsFileCompile) {
240252
\Yii::beginProfile('Compress js files');
241253
foreach ($view->jsFiles as $pos => $files) {
242254
if ($files) {
243-
$view->jsFiles[$pos] = $this->_processingJsFiles($files);
255+
if ($this->jsFileCompileByGroups) {
256+
$view->jsFiles[$pos] = $this->_processAndGroupJsFiles($files);
257+
} else {
258+
$view->jsFiles[$pos] = $this->_processingJsFiles($files);
259+
}
244260
}
245261
}
246262
\Yii::endProfile('Compress js files');
247263
}
264+
//echo "<pre><code>" . print_r($view->jsFiles, true);die;
248265

249266
//Compiling js code that is found in the html code of the page.
250267
if ($view->js && $this->jsCompress) {
@@ -261,8 +278,11 @@ protected function _processing(View $view)
261278
//Compiling css files
262279
if ($view->cssFiles && $this->cssFileCompile) {
263280
\Yii::beginProfile('Compress css files');
264-
265-
$view->cssFiles = $this->_processingCssFiles($view->cssFiles);
281+
if ($this->cssFileCompileByGroups) {
282+
$view->cssFiles = $this->_processAndGroupCssFiles($view->cssFiles);
283+
} else {
284+
$view->cssFiles = $this->_processingCssFiles($view->cssFiles);
285+
}
266286
\Yii::endProfile('Compress css files');
267287
}
268288

@@ -313,6 +333,55 @@ protected function _processing(View $view)
313333
\Yii::endProfile('Moving css files bottom');
314334
}
315335
}
336+
337+
/**
338+
* @param array $files
339+
*/
340+
protected function _processAndGroupJsFiles($files = [])
341+
{
342+
if (!$files) {
343+
return [];
344+
}
345+
346+
$result = [];
347+
$groupedFiles = $this->_getGroupedFiles($files);
348+
foreach ($groupedFiles as $files)
349+
{
350+
$resultGroup = $this->_processingJsFiles($files);
351+
$result = ArrayHelper::merge($result, $resultGroup);
352+
}
353+
354+
return $result;
355+
echo "<pre><code>" . print_r($result, true); die;
356+
357+
}
358+
359+
public function _getGroupedFiles($files)
360+
{
361+
$result = [];
362+
363+
$lastKey = null;
364+
$tmpData = [];
365+
$counter = 0;
366+
foreach ($files as $fileCode => $fileTag) {
367+
list($one, $two, $key) = explode("/", $fileCode);
368+
369+
$counter ++;
370+
371+
if ($key != $lastKey && $counter > 1) {
372+
$result[] = $tmpData;
373+
$tmpData = [];
374+
$tmpData[$fileCode] = $fileTag;
375+
} else {
376+
$tmpData[$fileCode] = $fileTag;
377+
}
378+
379+
$lastKey = $key;
380+
}
381+
382+
return $result;
383+
}
384+
316385
/**
317386
* @param array $files
318387
* @return array
@@ -435,7 +504,7 @@ public function readLocalFile($filePath)
435504
throw new \Exception("Unable to open file: '{$filePath}'");
436505
}
437506
$filesSize = filesize($filePath);
438-
if($filesSize){
507+
if ($filesSize) {
439508
return fread($file, $filesSize);
440509
}
441510
fclose($file);
@@ -481,6 +550,28 @@ protected function _processingJs($parts)
481550

482551
return $result;
483552
}
553+
554+
/**
555+
* @param array $files
556+
*/
557+
protected function _processAndGroupCssFiles($files = [])
558+
{
559+
if (!$files) {
560+
return [];
561+
}
562+
563+
$result = [];
564+
$groupedFiles = $this->_getGroupedFiles($files);
565+
foreach ($groupedFiles as $files)
566+
{
567+
$resultGroup = $this->_processingCssFiles($files);
568+
$result = ArrayHelper::merge($result, $resultGroup);
569+
}
570+
571+
return $result;
572+
573+
}
574+
484575
/**
485576
* @param array $files
486577
* @return array
@@ -616,9 +707,9 @@ protected function _processingHtml($html)
616707
{
617708
if ($this->htmlFormatter instanceof IFormatter) {
618709
$r = new \ReflectionClass($this->htmlFormatter);
619-
\Yii::beginProfile('Format html: ' . $r->getName());
620-
$result = $this->htmlFormatter->format($html);
621-
\Yii::endProfile('Format html: ' . $r->getName());
710+
\Yii::beginProfile('Format html: '.$r->getName());
711+
$result = $this->htmlFormatter->format($html);
712+
\Yii::endProfile('Format html: '.$r->getName());
622713
return $result;
623714
}
624715

@@ -629,40 +720,40 @@ protected function _processingHtml($html)
629720

630721

631722
/**
632-
* @deprecated >= 1.4
633723
* @param $value
634724
* @return $this
725+
* @deprecated >= 1.4
635726
*/
636727
public function setHtmlCompress($value)
637728
{
638729
return $this;
639730
}
640731

641732
/**
642-
* @deprecated >= 1.4
643733
* @param $value
644734
* @return $this
735+
* @deprecated >= 1.4
645736
*/
646737
public function getHtmlCompress()
647738
{
648739
return $this;
649740
}
650741
/**
651-
* @deprecated >= 1.4
652742
* @param $value array options for compressing output result
653743
* * extra - use more compact algorithm
654744
* * no-comments - cut all the html comments
655745
* @return $this
746+
* @deprecated >= 1.4
656747
*/
657748
public function setHtmlCompressOptions($value)
658749
{
659750
return $this;
660751
}
661752

662753
/**
663-
* @deprecated >= 1.4
664754
* @param $value
665755
* @return $this
756+
* @deprecated >= 1.4
666757
*/
667758
public function getHtmlCompressOptions()
668759
{

0 commit comments

Comments
 (0)