From 01d908b82e2ce58fa9e9876db678830305abf5ae Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Thu, 10 Oct 2013 05:39:56 +0200 Subject: [PATCH 1/4] added optional experimental feature for copying CSS images to output-folder on package compression --- PackageCompressor.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/PackageCompressor.php b/PackageCompressor.php index ff1ed61..2dd7863 100644 --- a/PackageCompressor.php +++ b/PackageCompressor.php @@ -19,6 +19,14 @@ class PackageCompressor extends CClientScript */ public $combineOnly = false; + /** + * @var bool wheter to copy images from combined CSS files to output directory + * Only effective when $enableCompression is set to "true" + * + * Note: Workaround for https://github.com/yiisoft/yii/issues/1033 + */ + public $copyCssImages = false; + /** * If this is enabled, during compression all other requests will wait until the compressing * process has completed. If disabled, the uncompressed files will be delivered for these @@ -148,6 +156,15 @@ public function compressPackage($name) $destFile = $am->getPublishedPath($fileName,true); } + // copy images + if ($this->copyCssImages) foreach (array_keys($this->cssFiles) as $file) { + CFileHelper::copyDirectory( + dirname($basePath . $file), + dirname($destFile), + array('fileTypes' => array('jpg', 'png', 'gif')) + ); + } + $info['css'] = array( 'file' => $destFile, 'files' => $files, From b9dec3695859421d88f99b9cba9066d174779178 Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Sun, 13 Oct 2013 23:09:03 +0200 Subject: [PATCH 2/4] refactored feature to use url rewriting instead of image copying --- PackageCompressor.php | 20 ++++++++++++++++---- composer.json | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/PackageCompressor.php b/PackageCompressor.php index 2dd7863..b907ff1 100644 --- a/PackageCompressor.php +++ b/PackageCompressor.php @@ -20,9 +20,12 @@ class PackageCompressor extends CClientScript public $combineOnly = false; /** - * @var bool wheter to copy images from combined CSS files to output directory * Only effective when $enableCompression is set to "true" * + * @var bool wheter to rewrite URLs in CSS files to an absolute path before combining + */ + public $rewriteCssUris = false; + * Note: Workaround for https://github.com/yiisoft/yii/issues/1033 */ public $copyCssImages = false; @@ -92,7 +95,7 @@ public function compressPackage($name) $info = array(); $am = Yii::app()->assetManager; - $basePath = Yii::getPathOfAlias('webroot'); + $basePath = realpath(Yii::getPathOfAlias('webroot')); // /www/root/sub -> /www/root (baseUrl=/sub) if(($baseUrl = Yii::app()->request->baseUrl)!=='') @@ -134,8 +137,17 @@ public function compressPackage($name) { $files = array(); $urls = array(); - foreach(array_keys($this->cssFiles) as $file) - $files[] = $basePath.$file; + + foreach(array_keys($this->cssFiles) as $file) { + if ($this->rewriteCssUris) { + $inFile = $basePath.$file; + $outFile = $basePath.$file.'-rewrite.css'; + file_put_contents($outFile, Minify_CSS_UriRewriter::rewrite(file_get_contents($inFile), dirname($inFile), $basePath)); + } else { + $outFile = $basePath.$file; + } + $files[] = $outFile; + } $fileName = $this->compressFiles($name,'css',$files); if(isset($this->packages[$name]['baseUrl'])) diff --git a/composer.json b/composer.json index 82cbec4..64e1a6f 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ } ], "require": { - "php": ">=5.0.0" + "php": ">=5.0.0", + "mrclay/minify": "2.1.*" } } From 8c4423696265517ea1952264515a16f8e762b3f2 Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Sun, 13 Oct 2013 23:35:11 +0200 Subject: [PATCH 3/4] fixed typo --- PackageCompressor.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/PackageCompressor.php b/PackageCompressor.php index b907ff1..3ca2669 100644 --- a/PackageCompressor.php +++ b/PackageCompressor.php @@ -26,10 +26,6 @@ class PackageCompressor extends CClientScript */ public $rewriteCssUris = false; - * Note: Workaround for https://github.com/yiisoft/yii/issues/1033 - */ - public $copyCssImages = false; - /** * If this is enabled, during compression all other requests will wait until the compressing * process has completed. If disabled, the uncompressed files will be delivered for these From d617ef6e1d2d2b1c7cd2014d9b49c4522514336a Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Sun, 13 Oct 2013 23:36:28 +0200 Subject: [PATCH 4/4] removed outdated code --- PackageCompressor.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/PackageCompressor.php b/PackageCompressor.php index 3ca2669..08f7094 100644 --- a/PackageCompressor.php +++ b/PackageCompressor.php @@ -164,15 +164,6 @@ public function compressPackage($name) $destFile = $am->getPublishedPath($fileName,true); } - // copy images - if ($this->copyCssImages) foreach (array_keys($this->cssFiles) as $file) { - CFileHelper::copyDirectory( - dirname($basePath . $file), - dirname($destFile), - array('fileTypes' => array('jpg', 'png', 'gif')) - ); - } - $info['css'] = array( 'file' => $destFile, 'files' => $files,