Commit 70ca76d4 by Paul Klimov

Added `yii\web\AssetConverter::$forceConvert`

parent d8e43b7c
......@@ -47,6 +47,7 @@ Yii Framework 2 Change Log
- Bug: Gii console command help information does not contain global options (qiangxue)
- Bug: `yii\web\UrlRule` was unable to create URLs for rules containing unicode characters (samdark)
- Bug: `yii\web\AssetManager` should not publish disabled asset bundles (qiangxue)
- Enh #608: Added `yii\web\AssetConverter::$forceConvert` (klimov-paul)
- Enh #4146: Added `yii\bootstrap\ButtonDropdown::$containerOptions` (samdark)
- Enh #4181: Added `yii\bootstrap\Modal::$headerOptions` and `yii\bootstrap\Modal::$footerOptions` (tuxoff, samdark)
- Enh #4263: Added migration and SQL schema files for `yii\log\DbTarget` (samdark)
......
......@@ -42,6 +42,13 @@ class AssetConverter extends Component implements AssetConverterInterface
'coffee' => ['js', 'coffee -p {from} > {to}'],
'ts' => ['js', 'tsc --out {to} {from}'],
];
/**
* @var boolean whether the source asset file should be converted even if its result already exists.
* You may want to set this to be `true` during the development stage to make sure the converted
* assets are always up-to-date. Do not set this to true on production servers as it will
* significantly degrade the performance.
*/
public $forceConvert = false;
/**
......@@ -58,7 +65,7 @@ class AssetConverter extends Component implements AssetConverterInterface
if (isset($this->commands[$ext])) {
list ($ext, $command) = $this->commands[$ext];
$result = substr($asset, 0, $pos + 1) . $ext;
if (@filemtime("$basePath/$result") < filemtime("$basePath/$asset")) {
if ($this->forceConvert || @filemtime("$basePath/$result") < filemtime("$basePath/$asset")) {
$this->runCommand($command, $basePath, $asset, $result);
}
......
......@@ -5,6 +5,7 @@
namespace yiiunit\framework\web;
use yii\helpers\FileHelper;
use yii\web\AssetConverter;
/**
......@@ -12,18 +13,34 @@ use yii\web\AssetConverter;
*/
class AssetConverterTest extends \yiiunit\TestCase
{
/**
* @var string temporary files path
*/
protected $tmpPath;
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$this->tmpPath = \Yii::$app->runtimePath . '/assetConverterTest_' . getmypid();
if (!is_dir($this->tmpPath)) {
mkdir($this->tmpPath, 0777, true);
}
}
public function testConvert()
protected function tearDown()
{
$tmpPath = \Yii::$app->runtimePath . '/assetConverterTest';
if (!is_dir($tmpPath)) {
mkdir($tmpPath, 0777, true);
if (is_dir($this->tmpPath)) {
FileHelper::removeDirectory($this->tmpPath);
}
parent::tearDown();
}
// Tests :
public function testConvert()
{
$tmpPath = $this->tmpPath;
file_put_contents($tmpPath . '/test.php', <<<EOF
<?php
......@@ -39,4 +56,32 @@ EOF
$this->assertTrue(file_exists($tmpPath . '/test.txt'), 'Failed asserting that asset output file exists.');
$this->assertEquals("Hello World!\nHello Yii!", file_get_contents($tmpPath . '/test.txt'));
}
/**
* @depends testConvert
*/
public function testForceConvert()
{
$tmpPath = $this->tmpPath;
file_put_contents($tmpPath . '/test.php', <<<EOF
<?php
echo microtime();
EOF
);
$converter = new AssetConverter();
$converter->commands['php'] = ['txt', 'php {from} > {to}'];
$converter->convert('test.php', $tmpPath);
$initialConvertTime = file_get_contents($tmpPath . '/test.txt');
usleep(1);
$converter->convert('test.php', $tmpPath);
$this->assertEquals($initialConvertTime, file_get_contents($tmpPath . '/test.txt'));
$converter->forceConvert = true;
$converter->convert('test.php', $tmpPath);
$this->assertNotEquals($initialConvertTime, file_get_contents($tmpPath . '/test.txt'));
}
}
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