Browse Source

Added `yii\console\controllers\AssetController::deleteSource`

tags/2.0.10
Klimov Paul 8 years ago
parent
commit
56cc97ae42
  1. 2
      framework/CHANGELOG.md
  2. 46
      framework/console/controllers/AssetController.php
  3. 119
      tests/framework/console/controllers/AssetControllerTest.php

2
framework/CHANGELOG.md

@ -29,6 +29,7 @@ Yii Framework 2 Change Log
- Bug #11715: Fixed JS validation when the same model's attribute file input is listed more than once on the same page (uaoleg)
- Bug #11541: Fixed default MySQL integer display width for unsigned primary key (h311ion, rob006, cebe)
- Bug #12143: Fixed `yii\db\BaseActiveRecord::updateAttributes()` change `isNewRecord` state for the new model (klimov-paul)
- Enh #9708: Added `yii\console\controllers\AssetController::deleteSource` option allowing deletion of the source asset files after compression (pana1990, klimov-paul)
- Enh #10583: Do not silence session errors in debug mode (samdark)
- Enh #11658: Added argument to `yii\grid\ActionColumn::urlCreator` callback, which holds reference to the column instance (klimov-paul)
- Enh #11804: Added `yii\behaviors\AttributeTypecastBehavior` for maintaining of strict ActiveRecord attribute types (klimov-paul)
@ -47,6 +48,7 @@ Yii Framework 2 Change Log
- Bug #12331: Fixed bug with incorrect currency formatter output, when `$thousandSeparator` was explicitly set (cebe)
- Bug #11347: Fixed `yii\widgets\Pjax::registerClientScript()` to pass custom `container` to the PJAX JS plugin (silverfire)
- Bug #12293: Fixed MSSQL `Schema::resolveTableNames()` when using linked database tables (hAppywAy)
- Enh: Method `yii\console\controllers\AssetController::getAssetManager()` automatically enables `yii\web\AssetManager::forceCopy` in case it is not explicitly specified (pana1990, klimov-paul)
2.0.9 July 11, 2016

46
framework/console/controllers/AssetController.php

@ -41,6 +41,7 @@ use yii\web\AssetBundle;
* differs in getter and setter. See [[getAssetManager()]] and [[setAssetManager()]] for details.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class AssetController extends Controller
@ -120,6 +121,12 @@ class AssetController extends Controller
* @see https://github.com/yui/yuicompressor/
*/
public $cssCompressor = 'java -jar yuicompressor.jar --type css {from} -o {to}';
/**
* @var boolean whether to delete asset source files after compression.
* This option affects only those bundles, which have [[\yii\web\AssetBundle::sourcePath]] is set.
* @since 2.0.10
*/
public $deleteSource = false;
/**
* @var array|\yii\web\AssetManager [[\yii\web\AssetManager]] instance or its array configuration, which will be used
@ -146,6 +153,11 @@ class AssetController extends Controller
if (!isset($options['baseUrl'])) {
throw new Exception("Please specify 'baseUrl' for the 'assetManager' option.");
}
if (!isset($options['forceCopy'])) {
$options['forceCopy'] = true;
}
$this->_assetManager = Yii::createObject($options);
}
@ -190,6 +202,10 @@ class AssetController extends Controller
$targets = $this->adjustDependency($targets, $bundles);
$this->saveTargets($targets, $bundleFile);
if ($this->deleteSource) {
$this->deletePublishedAssets($bundles);
}
}
/**
@ -683,6 +699,8 @@ return [
'jsCompressor' => {$jsCompressor},
// Adjust command/callback for CSS files compressing:
'cssCompressor' => {$cssCompressor},
// Whether to delete asset source after compression:
'deleteSource' => false,
// The list of asset bundles to compress:
'bundles' => [
// 'app\assets\AppAsset',
@ -782,4 +800,32 @@ EOD;
$dependencyTrace[] = $circularDependencyName;
return implode(' -> ', $dependencyTrace);
}
/**
* Deletes bundle asset files, which have been published from `sourcePath`.
* @param \yii\web\AssetBundle[] $bundles asset bundles to be processed.
* @since 2.0.10
*/
private function deletePublishedAssets($bundles)
{
$this->stdout("Deleting source files...\n");
if ($this->getAssetManager()->linkAssets) {
$this->stdout("`AssetManager::linkAssets` option is enabled. Deleting of source files canceled.\n", Console::FG_YELLOW);
return;
}
foreach ($bundles as $bundle) {
if ($bundle->sourcePath !== null) {
foreach ($bundle->js as $jsFile) {
@unlink($bundle->basePath . DIRECTORY_SEPARATOR . $jsFile);
}
foreach ($bundle->css as $cssFile) {
@unlink($bundle->basePath . DIRECTORY_SEPARATOR . $cssFile);
}
}
}
$this->stdout("Source files deleted.\n", Console::FG_GREEN);
}
}

119
tests/framework/console/controllers/AssetControllerTest.php

@ -2,8 +2,10 @@
namespace yiiunit\framework\console\controllers;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;
use yii\helpers\StringHelper;
use yii\helpers\VarDumper;
use yiiunit\TestCase;
use yii\console\controllers\AssetController;
use Yii;
@ -89,16 +91,17 @@ class AssetControllerTest extends TestCase
/**
* Creates test compress config.
* @param array[] $bundles asset bundles config.
* @return array config array.
* @param array[] $bundles asset bundles config.
* @param array $config additional config.
* @return array config array.
*/
protected function createCompressConfig(array $bundles)
protected function createCompressConfig(array $bundles, array $config = [])
{
static $classNumber = 0;
$classNumber++;
$className = $this->declareAssetBundleClass(['class' => 'AssetBundleAll' . $classNumber]);
$baseUrl = '/test';
$config = [
$config = ArrayHelper::merge($config, [
'bundles' => $bundles,
'targets' => [
$className => [
@ -112,20 +115,21 @@ class AssetControllerTest extends TestCase
'basePath' => $this->testAssetsBasePath,
'baseUrl' => '',
],
];
]);
return $config;
}
/**
* Creates test compress config file.
* @param string $fileName output file name.
* @param array[] $bundles asset bundles config.
* @param string $fileName output file name.
* @param array[] $bundles asset bundles config.
* @param array $config additional config parameters.
* @throws \Exception on failure.
*/
protected function createCompressConfigFile($fileName, array $bundles)
protected function createCompressConfigFile($fileName, array $bundles, array $config = [])
{
$content = '<?php return ' . var_export($this->createCompressConfig($bundles), true) . ';';
$content = '<?php return ' . var_export($this->createCompressConfig($bundles, $config), true) . ';';
if (file_put_contents($fileName, $content) <= 0) {
throw new \Exception("Unable to create file '{$fileName}'!");
}
@ -133,13 +137,17 @@ class AssetControllerTest extends TestCase
/**
* Creates test asset file.
* @param string $fileRelativeName file name relative to [[testFilePath]]
* @param string $content file content
* @param string $fileRelativeName file name relative to [[testFilePath]]
* @param string $content file content
* @param string $fileBasePath base path for the created files, if not set [[testFilePath]] is used.
* @throws \Exception on failure.
*/
protected function createAssetSourceFile($fileRelativeName, $content)
protected function createAssetSourceFile($fileRelativeName, $content, $fileBasePath = null)
{
$fileFullName = $this->testFilePath . DIRECTORY_SEPARATOR . $fileRelativeName;
if ($fileBasePath === null) {
$fileBasePath = $this->testFilePath;
}
$fileFullName = $fileBasePath . DIRECTORY_SEPARATOR . $fileRelativeName;
$this->createDir(dirname($fileFullName));
if (file_put_contents($fileFullName, $content) <= 0) {
throw new \Exception("Unable to create file '{$fileFullName}'!");
@ -149,11 +157,12 @@ class AssetControllerTest extends TestCase
/**
* Creates a list of asset source files.
* @param array $files assert source files in format: file/relative/name => fileContent
* @param string $fileBasePath base path for the created files, if not set [[testFilePath]]
*/
protected function createAssetSourceFiles(array $files)
protected function createAssetSourceFiles(array $files, $fileBasePath = null)
{
foreach ($files as $name => $content) {
$this->createAssetSourceFile($name, $content);
$this->createAssetSourceFile($name, $content, $fileBasePath);
}
}
@ -186,6 +195,7 @@ class AssetControllerTest extends TestCase
[
'namespace' => StringHelper::dirname(get_class($this)),
'class' => 'AppAsset',
'sourcePath' => null,
'basePath' => $this->testFilePath,
'baseUrl' => '',
'css' => [],
@ -195,8 +205,8 @@ class AssetControllerTest extends TestCase
$config
);
foreach ($config as $name => $value) {
if (is_array($value)) {
$config[$name] = var_export($value, true);
if (!in_array($name, ['namespace', 'class'])) {
$config[$name] = VarDumper::export($value);
}
}
@ -207,8 +217,9 @@ use yii\web\AssetBundle;
class {$config['class']} extends AssetBundle
{
public \$basePath = '{$config['basePath']}';
public \$baseUrl = '{$config['baseUrl']}';
public \$sourcePath = {$config['sourcePath']};
public \$basePath = {$config['basePath']};
public \$baseUrl = {$config['baseUrl']};
public \$css = {$config['css']};
public \$js = {$config['js']};
public \$depends = {$config['depends']};
@ -590,6 +601,76 @@ EOL;
$realPath = $this->invokeAssetControllerMethod('findRealPath', [$sourcePath]);
$this->assertEquals($expectedRealPath, $realPath);
}
/**
* @depends testActionCompress
*
* @see https://github.com/yiisoft/yii2/issues/9708
*/
public function testActionCompressDeleteSource()
{
// Given :
$cssFiles = [
'css/test_body.css' => 'body {
padding-top: 20px;
padding-bottom: 60px;
}',
];
$this->createAssetSourceFiles($cssFiles);
$jsFiles = [
'js/test_alert.js' => "function test() {
alert('Test message');
}",
];
$sourcePath = $this->testFilePath . DIRECTORY_SEPARATOR . 'source';
$this->createAssetSourceFiles($cssFiles, $sourcePath);
$this->createAssetSourceFiles($jsFiles, $sourcePath);
$assetBundleClassName = $this->declareAssetBundleClass([
'class' => 'AssetDelete',
'css' => array_keys($cssFiles),
'js' => array_keys($jsFiles),
'basePath' => null,
'sourcePath' => $sourcePath,
]);
$bundles = [
$assetBundleClassName
];
$bundleFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'bundle.php';
// Keep source :
$configFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'config2.php';
$this->createCompressConfigFile($configFile, $bundles, [
'deleteSource' => false
]);
$this->runAssetControllerAction('compress', [$configFile, $bundleFile]);
$files = FileHelper::findFiles($this->testAssetsBasePath, [
'only' => [
'test_body.css',
'test_alert.js'
],
]);
$this->assertNotEmpty($files);
// Delete source :
$configFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'config2.php';
$this->createCompressConfigFile($configFile, $bundles, [
'deleteSource' => true
]);
$this->runAssetControllerAction('compress', [$configFile, $bundleFile]);
$files = FileHelper::findFiles($this->testAssetsBasePath, [
'only' => [
'test_body.css',
'test_alert.js'
],
]);
$this->assertEmpty($files);
}
}
/**

Loading…
Cancel
Save