yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

Change AssetController to use relative path for conversion with yuicompressor #16088

Open Bhoft opened 6 years ago

Bhoft commented 6 years ago

The yuicompressor has the problem that "absolute paths" aren't working on Windows with the "current" version 2.4.8 for the output file (which is already 3 years old and it doesn't look like a newer release would happen anytime soon)

See this issue about this problem: https://github.com/yui/yuicompressor/issues/78

The Version 2.4.7 works without a problem but has of course issues which were fixed in the 2.4.8 release. So it would be good that the 2.4.8 version of the yuicompressor would also work on Windows.

A solution "could" be that the -o parameter of the shell exec uses a relative path instead of an absolute.

I tried to set the webroot alias "relative", so instead of Yii::setAlias('@webroot', __DIR__ . '/web'); to Yii::setAlias('@webroot', './web'); in the "assets.php" file for assetbundle generation call yii asset assets.php config/assets-dev.php.

The compressed css is then generated but all 'url' inside the css are wrong, so images and fonts aren't correctly included..

If the -o parameter for the yuicompressor would use a relative path instead of an absolute the generated would work also Windows machines.

So instead of '{to}' => escapeshellarg($outputFile), use this line '{to}' => escapeshellarg(substr($outputFile, strlen(Yii::getAlias('@app/')))),

on this line: https://github.com/yiisoft/yii2/blob/master/framework/console/controllers/AssetController.php#L549

So that the to output path would be -o "web/assets/css/all-temp.css" instead of -o "D:\wamp\www\yii2-app\web/assets/css/all-temp.css"

What steps will reproduce the problem?

create a asset bundle on console with yuicompressor version 2.4.8 on Windows.

What is the expected result?

a compressed css instead of an java exception.

What do you get instead?

java.io.FileNotFoundException: wampwwwyii2-app\web\assets\css\all-temp.css:\wamp\www\yii2-app\web\assets\css\all-temp.css.tmp (The filename, directory name, or volume label syntax is incorrect) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(Unknown Source)

Additional info

Q A
Yii version 2.0.15.1
PHP version 7.1.4
Operating system Win 7
SilverFire commented 6 years ago

Thank you for the reported issue. Would you like to help up to fix this issue by submitting a pull request? We have contributors guide that is a good starting point. If you need any tip – let us know.

yus-ham commented 6 years ago

java.io.FileNotFoundException: wampwwwyii2-app\web\assets\css\all-temp... It seem there are missing the DIRECTORY_SEPARATOR.