Commit 94f702fe by Klimov Paul

`yii\console\controllers\AssetController` now handles bundle files from external resources properly

parent 95c7a6fe
......@@ -12,6 +12,7 @@ Yii Framework 2 Change Log
- Bug #6691: Fixed console help description parsing with UTF8 characters (cebe)
- Bug #6717: Fixed issue with UrlManager not matching a route on url creation when it was prefixed with `/` and pattern was empty (cebe)
- Enh #4502: Added alias support to URL route when calling `Url::toRoute()` and `Url::to()` (qiangxue, lynicidn)
- Enh #5194: `yii\console\controllers\AssetController` now handles bundle files from external resources properly (klimov-paul)
- Enh #6247: Logger and error handler are now using slightly less memory (stepanselyuk, samdark)
- Enh #6398: Added support for specifying dependent component in terms of a configuration array for classes such as `DbCache` (qiangxue)
- Enh #6434: Added `yii\behaviors\SluggableBehavior::immutable` to support keeping the generated slug unchanged (trntv)
......
......@@ -12,6 +12,7 @@ use yii\console\Exception;
use yii\console\Controller;
use yii\helpers\Console;
use yii\helpers\VarDumper;
use yii\web\AssetBundle;
/**
* Allows you to combine and compress your JavaScript and CSS files.
......@@ -298,8 +299,10 @@ class AssetController extends Controller
foreach ($target->depends as $name) {
if (isset($bundles[$name])) {
foreach ($bundles[$name]->$type as $file) {
$inputFiles[] = $bundles[$name]->basePath . '/' . $file;
if (!$this->isBundleExternal($bundles[$name])) {
foreach ($bundles[$name]->$type as $file) {
$inputFiles[] = $bundles[$name]->basePath . '/' . $file;
}
}
} else {
throw new Exception("Unknown bundle: '{$name}'");
......@@ -352,9 +355,14 @@ class AssetController extends Controller
}
foreach ($map as $bundle => $target) {
$sourceBundle = $bundles[$bundle];
$depends = $sourceBundle->depends;
if (!$this->isBundleExternal($sourceBundle)) {
$depends[] = $target;
}
$targets[$bundle] = Yii::createObject([
'class' => strpos($bundle, '\\') !== false ? $bundle : 'yii\\web\\AssetBundle',
'depends' => [$target],
'depends' => $depends,
]);
}
......@@ -402,12 +410,16 @@ class AssetController extends Controller
'css' => $target->css,
];
} else {
$array[$name] = [
'sourcePath' => null,
'js' => [],
'css' => [],
'depends' => $target->depends,
];
if ($this->isBundleExternal($target)) {
$array[$name] = $this->composeBundleConfig($target);
} else {
$array[$name] = [
'sourcePath' => null,
'js' => [],
'css' => [],
'depends' => $target->depends,
];
}
}
}
$array = VarDumper::export($array);
......@@ -683,4 +695,24 @@ EOD;
}
return implode(DIRECTORY_SEPARATOR, $realPathParts);
}
/**
* @param AssetBundle $bundle
* @return boolean whether asset bundle external or not.
*/
private function isBundleExternal($bundle)
{
return (empty($bundle->sourcePath) && empty($bundle->basePath));
}
/**
* @param AssetBundle $bundle asset bundle instance.
* @return array bundle configuration.
*/
private function composeBundleConfig($bundle)
{
$config = Yii::getObjectVars($bundle);
$config['class'] = get_class($bundle);
return $config;
}
}
......@@ -95,7 +95,9 @@ class AssetControllerTest extends TestCase
*/
protected function createCompressConfig(array $bundles)
{
$className = $this->declareAssetBundleClass(['class' => 'AssetBundleAll']);
static $classNumber = 0;
$classNumber++;
$className = $this->declareAssetBundleClass(['class' => 'AssetBundleAll' . $classNumber]);
$baseUrl = '/test';
$config = [
'bundles' => $bundles,
......@@ -301,6 +303,73 @@ EOL;
}
/**
* @depends testActionCompress
*
* @see https://github.com/yiisoft/yii2/issues/5194
*/
public function testCompressExternalAsset()
{
// Given :
$externalAssetConfig = [
'class' => 'ExternalAsset',
'sourcePath' => null,
'basePath' => null,
'js' => [
'//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js',
],
'css' => [
'//ajax.googleapis.com/css/libs/jquery/2.1.1/jquery.ui.min.css'
],
];
$externalAssetBundleClassName = $this->declareAssetBundleClass($externalAssetConfig);
$cssFiles = [
'css/test.css' => 'body {
padding-top: 20px;
padding-bottom: 60px;
}',
];
$this->createAssetSourceFiles($cssFiles);
$jsFiles = [
'js/test.js' => "function test() {
alert('Test message');
}",
];
$this->createAssetSourceFiles($jsFiles);
$regularAssetBundleClassName = $this->declareAssetBundleClass([
'class' => 'RegularAsset',
'css' => array_keys($cssFiles),
'js' => array_keys($jsFiles),
'depends' => [
$externalAssetBundleClassName
],
]);
$bundles = [
$regularAssetBundleClassName
];
$bundleFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'bundle.php';
$configFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'config.php';
$this->createCompressConfigFile($configFile, $bundles);
// When :
$this->runAssetControllerAction('compress', [$configFile, $bundleFile]);
// Then :
$this->assertTrue(file_exists($bundleFile), 'Unable to create output bundle file!');
$compressedBundleConfig = require($bundleFile);
$this->assertTrue(is_array($compressedBundleConfig), 'Output bundle file has incorrect format!');
$this->assertArrayHasKey($externalAssetBundleClassName, $compressedBundleConfig, 'External bundle is lost!');
$compressedExternalAssetConfig = $compressedBundleConfig[$externalAssetBundleClassName];
$this->assertEquals($externalAssetConfig['js'], $compressedExternalAssetConfig['js'], 'External bundle js is lost!');
$this->assertEquals($externalAssetConfig['css'], $compressedExternalAssetConfig['css'], 'External bundle css is lost!');
$compressedRegularAssetConfig = $compressedBundleConfig[$regularAssetBundleClassName];
$this->assertContains($externalAssetBundleClassName, $compressedRegularAssetConfig['depends'], 'Dependency on external bundle is lost!');
}
/**
* Data provider for [[testAdjustCssUrl()]].
* @return array test data.
*/
......
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