Skip to content

Commit

Permalink
New option cssFileCompileByGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
skeeks-semenov committed Oct 30, 2020
1 parent c01b7a7 commit 863513b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
117 changes: 104 additions & 13 deletions src/AssetsAutoCompressComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
Expand All @@ -236,15 +247,21 @@ public function bootstrap($app)
protected function _processing(View $view)
{
//Компиляция файлов js в один.
//echo "<pre><code>" . 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 "<pre><code>" . print_r($view->jsFiles, true);die;

//Compiling js code that is found in the html code of the page.
if ($view->js && $this->jsCompress) {
Expand All @@ -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');
}

Expand Down Expand Up @@ -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 "<pre><code>" . 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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

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


/**
* @deprecated >= 1.4
* @param $value
* @return $this
* @deprecated >= 1.4
*/
public function setHtmlCompress($value)
{
return $this;
}

/**
* @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)
{
return $this;
}

/**
* @deprecated >= 1.4
* @param $value
* @return $this
* @deprecated >= 1.4
*/
public function getHtmlCompressOptions()
{
Expand Down

0 comments on commit 863513b

Please sign in to comment.