Commit d84898a7 by Alexander Makarov

Fixes #2898: `yii\console\controllers\AssetController` is now using hashes instead of timestamps

parent 8ee7f5d7
...@@ -227,8 +227,8 @@ return [ ...@@ -227,8 +227,8 @@ return [
'app\config\AllAsset' => [ 'app\config\AllAsset' => [
'basePath' => 'path/to/web', 'basePath' => 'path/to/web',
'baseUrl' => '', 'baseUrl' => '',
'js' => 'js/all-{ts}.js', 'js' => 'js/all-{hash}.js',
'css' => 'css/all-{ts}.css', 'css' => 'css/all-{hash}.css',
], ],
], ],
// Asset manager configuration: // Asset manager configuration:
...@@ -246,8 +246,8 @@ everything to `path/to/web` that can be accessed like `http://example.com/` i.e. ...@@ -246,8 +246,8 @@ everything to `path/to/web` that can be accessed like `http://example.com/` i.e.
> Note: in the console environment some path aliases like '@webroot' and '@web' may not exist, > Note: in the console environment some path aliases like '@webroot' and '@web' may not exist,
so corresponding paths inside the configuration should be specified directly. so corresponding paths inside the configuration should be specified directly.
JavaScript files are combined, compressed and written to `js/all-{ts}.js` where {ts} is replaced with current UNIX JavaScript files are combined, compressed and written to `js/all-{hash}.js` where {hash} is replaced with the hash of
timestamp. the resulting file.
`jsCompressor` and `cssCompressor` are console commands or PHP callbacks, which should perform JavaScript and CSS files `jsCompressor` and `cssCompressor` are console commands or PHP callbacks, which should perform JavaScript and CSS files
compression correspondingly. You should adjust these values according to your environment. compression correspondingly. You should adjust these values according to your environment.
......
...@@ -67,6 +67,7 @@ Yii Framework 2 Change Log ...@@ -67,6 +67,7 @@ Yii Framework 2 Change Log
- Enh: Added support for using path alias with `FileDependency::fileName` (qiangxue) - Enh: Added support for using path alias with `FileDependency::fileName` (qiangxue)
- Enh: Added param `hideOnSinglePage` to `yii\widgets\LinkPager` (arturf) - Enh: Added param `hideOnSinglePage` to `yii\widgets\LinkPager` (arturf)
- Enh: Added support for array attributes in `in` validator (creocoder) - Enh: Added support for array attributes in `in` validator (creocoder)
- Chg #2898: `yii\console\controllers\AssetController` is now using hashes instead of timestamps (samdark)
- Chg #2913: RBAC `DbManager` is now initialized via migration (samdark) - Chg #2913: RBAC `DbManager` is now initialized via migration (samdark)
- Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue) - Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue)
- Chg #3175: InvalidCallException, InvalidParamException, UnknownMethodException are now extended from SPL BadMethodCallException (samdark) - Chg #3175: InvalidCallException, InvalidParamException, UnknownMethodException are now extended from SPL BadMethodCallException (samdark)
......
...@@ -42,3 +42,5 @@ Upgrade from Yii 2.0 Beta ...@@ -42,3 +42,5 @@ Upgrade from Yii 2.0 Beta
to return cell values (via `yii\grid\DataColumn::value`), you may need to adjust the signature to return cell values (via `yii\grid\DataColumn::value`), you may need to adjust the signature
of the callable to be `function ($model, $key, $index, $widget)`. The `$key` parameter was newly added of the callable to be `function ($model, $key, $index, $widget)`. The `$key` parameter was newly added
in this release. in this release.
* `yii\console\controllers\AssetController` is now using hashes instead of timestamps. Replace all `{ts}` with `{hash}`.
\ No newline at end of file
...@@ -10,6 +10,7 @@ namespace yii\console\controllers; ...@@ -10,6 +10,7 @@ namespace yii\console\controllers;
use Yii; use Yii;
use yii\console\Exception; use yii\console\Exception;
use yii\console\Controller; use yii\console\Controller;
use yii\helpers\StringHelper;
use yii\helpers\VarDumper; use yii\helpers\VarDumper;
/** /**
...@@ -52,14 +53,13 @@ class AssetController extends Controller ...@@ -52,14 +53,13 @@ class AssetController extends Controller
* *
* ~~~ * ~~~
* 'app\config\AllAsset' => [ * 'app\config\AllAsset' => [
* 'js' => 'js/all-{ts}.js', * 'js' => 'js/all-{hash}.js',
* 'css' => 'css/all-{ts}.css', * 'css' => 'css/all-{hash}.css',
* 'depends' => [ ... ], * 'depends' => [ ... ],
* ] * ]
* ~~~ * ~~~
* *
* File names can contain placeholder "{ts}", which will be filled by current timestamp, while * File names can contain placeholder "{hash}", which will be filled by the hash of the resulting file.
* file creation.
*/ */
public $targets = []; public $targets = [];
/** /**
...@@ -138,14 +138,13 @@ class AssetController extends Controller ...@@ -138,14 +138,13 @@ class AssetController extends Controller
$this->loadConfiguration($configFile); $this->loadConfiguration($configFile);
$bundles = $this->loadBundles($this->bundles); $bundles = $this->loadBundles($this->bundles);
$targets = $this->loadTargets($this->targets, $bundles); $targets = $this->loadTargets($this->targets, $bundles);
$timestamp = time();
foreach ($targets as $name => $target) { foreach ($targets as $name => $target) {
echo "Creating output bundle '{$name}':\n"; echo "Creating output bundle '{$name}':\n";
if (!empty($target->js)) { if (!empty($target->js)) {
$this->buildTarget($target, 'js', $bundles, $timestamp); $this->buildTarget($target, 'js', $bundles);
} }
if (!empty($target->css)) { if (!empty($target->css)) {
$this->buildTarget($target, 'css', $bundles, $timestamp); $this->buildTarget($target, 'css', $bundles);
} }
echo "\n"; echo "\n";
} }
...@@ -282,14 +281,11 @@ class AssetController extends Controller ...@@ -282,14 +281,11 @@ class AssetController extends Controller
* @param \yii\web\AssetBundle $target output asset bundle * @param \yii\web\AssetBundle $target output asset bundle
* @param string $type either 'js' or 'css'. * @param string $type either 'js' or 'css'.
* @param \yii\web\AssetBundle[] $bundles source asset bundles. * @param \yii\web\AssetBundle[] $bundles source asset bundles.
* @param integer $timestamp current timestamp.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
protected function buildTarget($target, $type, $bundles, $timestamp) protected function buildTarget($target, $type, $bundles)
{ {
$outputFile = strtr($target->$type, [ $tempFile = strtr($target->$type, ['{hash}' => 'temp']);
'{ts}' => $timestamp,
]);
$inputFiles = []; $inputFiles = [];
foreach ($target->depends as $name) { foreach ($target->depends as $name) {
...@@ -302,10 +298,13 @@ class AssetController extends Controller ...@@ -302,10 +298,13 @@ class AssetController extends Controller
} }
} }
if ($type === 'js') { if ($type === 'js') {
$this->compressJsFiles($inputFiles, $target->basePath . '/' . $outputFile); $this->compressJsFiles($inputFiles, $target->basePath . '/' . $tempFile);
} else { } else {
$this->compressCssFiles($inputFiles, $target->basePath . '/' . $outputFile); $this->compressCssFiles($inputFiles, $target->basePath . '/' . $tempFile);
} }
$outputFile = strtr($target->$type, ['{hash}' => md5_file($tempFile)]);
rename($tempFile, $outputFile);
$target->$type = [$outputFile]; $target->$type = [$outputFile];
} }
...@@ -599,8 +598,8 @@ return [ ...@@ -599,8 +598,8 @@ return [
'app\assets\AllAsset' => [ 'app\assets\AllAsset' => [
'basePath' => 'path/to/web', 'basePath' => 'path/to/web',
'baseUrl' => '', 'baseUrl' => '',
'js' => 'js/all-{ts}.js', 'js' => 'js/all-{hash}.js',
'css' => 'css/all-{ts}.css', 'css' => 'css/all-{hash}.css',
], ],
], ],
// Asset manager configuration: // Asset manager configuration:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment