Browse Source

"yii\console\controllers\AssetController" has been updated:

- assetManager has been converted into virtual property
 - bundle dependency load has been fixed
tags/2.0.0-beta
Klimov Paul 12 years ago
parent
commit
9f66abc984
  1. 6
      tests/unit/framework/console/controllers/AssetControllerTest.php
  2. 115
      yii/console/controllers/AssetController.php

6
tests/unit/framework/console/controllers/AssetControllerTest.php

@ -92,7 +92,8 @@ class AssetControllerTest extends TestCase
*/ */
protected function createCompressConfig(array $bundles) protected function createCompressConfig(array $bundles)
{ {
$baseUrl = '/test'; //$baseUrl = '/test';
$baseUrl = '';
$config = array( $config = array(
'bundles' => $this->createBundleConfig($bundles), 'bundles' => $this->createBundleConfig($bundles),
'targets' => array( 'targets' => array(
@ -207,6 +208,9 @@ class AssetControllerTest extends TestCase
'app' => array( 'app' => array(
'css' => array_keys($cssFiles), 'css' => array_keys($cssFiles),
'js' => array_keys($jsFiles), 'js' => array_keys($jsFiles),
'depends' => array(
'yii',
),
), ),
);; );;
$bundleFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'bundle.php'; $bundleFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'bundle.php';

115
yii/console/controllers/AssetController.php

@ -14,6 +14,8 @@ use yii\console\Controller;
/** /**
* This command allows you to combine and compress your JavaScript and CSS files. * This command allows you to combine and compress your JavaScript and CSS files.
* *
* @property array|\yii\web\AssetManager $assetManager asset manager, which will be used for assets processing.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
@ -50,10 +52,10 @@ class AssetController extends Controller
*/ */
public $targets = array(); public $targets = array();
/** /**
* @var array configuration for [[yii\web\AssetManager]] instance, which will be used * @var array|\yii\web\AssetManager [[yii\web\AssetManager]] instance or its array configuration, which will be used
* for assets publishing. * for assets processing.
*/ */
public $assetManager = array(); private $_assetManager = array();
/** /**
* @var string|callback Java Script file compressor. * @var string|callback Java Script file compressor.
* If a string, it is treated as shell command template, which should contain * If a string, it is treated as shell command template, which should contain
@ -76,6 +78,33 @@ class AssetController extends Controller
public $cssCompressor = 'java -jar yuicompressor.jar {from} -o {to}'; public $cssCompressor = 'java -jar yuicompressor.jar {from} -o {to}';
/** /**
* @return \yii\web\AssetManager asset manager instance.
*/
public function getAssetManager()
{
if (!is_object($this->_assetManager)) {
$options = $this->_assetManager;
if (!isset($options['class'])) {
$options['class'] = 'yii\\web\\AssetManager';
}
$this->_assetManager = Yii::createObject($options);
}
return $this->_assetManager;
}
/**
* @param \yii\web\AssetManager|array $assetManager asset manager instance or its array configuration.
* @throws \yii\console\Exception on invalid argument type.
*/
public function setAssetManager($assetManager)
{
if (is_scalar($assetManager)) {
throw new Exception('"' . get_class($this) . '::assetManager" should be either object or array - "' . gettype($assetManager) . '" given.');
}
$this->_assetManager = $assetManager;
}
/**
* Combines and compresses the asset files according to the given configuration. * Combines and compresses the asset files according to the given configuration.
* During the process new asset bundle configuration file will be created. * During the process new asset bundle configuration file will be created.
* You should replace your original asset bundle configuration with this file in order to use compressed files. * You should replace your original asset bundle configuration with this file in order to use compressed files.
@ -114,19 +143,14 @@ class AssetController extends Controller
echo "Loading configuration from '{$configFile}'...\n"; echo "Loading configuration from '{$configFile}'...\n";
foreach (require($configFile) as $name => $value) { foreach (require($configFile) as $name => $value) {
if (property_exists($this, $name)) { if (property_exists($this, $name) || $this->canSetProperty($name)) {
$this->$name = $value; $this->$name = $value;
} else { } else {
throw new Exception("Unknown configuration option: $name"); throw new Exception("Unknown configuration option: $name");
} }
} }
if (!isset($this->assetManager['basePath'])) { $this->getAssetManager(); // check asset manager configuration
throw new Exception("Please specify 'basePath' for the 'assetManager' option.");
}
if (!isset($this->assetManager['baseUrl'])) {
throw new Exception("Please specify 'baseUrl' for the 'assetManager' option.");
}
} }
/** /**
@ -138,27 +162,60 @@ class AssetController extends Controller
protected function loadBundles($bundles, $extensions) protected function loadBundles($bundles, $extensions)
{ {
echo "Collecting source bundles information...\n"; echo "Collecting source bundles information...\n";
$assetManager = $this->getAssetManager();
$result = array(); $result = array();
foreach ($bundles as $name => $bundle) {
$bundle['class'] = 'yii\\web\\AssetBundle'; $assetManager->bundles = $bundles;
$result[$name] = Yii::createObject($bundle); foreach ($assetManager->bundles as $name => $bundle) {
$result[$name] = $assetManager->getBundle($name);
} }
foreach ($extensions as $path) { foreach ($extensions as $path) {
$manifest = $path . '/assets.php'; $manifest = $path . '/assets.php';
if (!is_file($manifest)) { if (!is_file($manifest)) {
continue; continue;
} }
foreach (require($manifest) as $name => $bundle) { $assetManager->bundles = require($manifest);
foreach ($assetManager->bundles as $name => $bundle) {
if (!isset($result[$name])) { if (!isset($result[$name])) {
$bundle['class'] = 'yii\\web\\AssetBundle'; $result[$name] = $assetManager->getBundle($name);
$result[$name] = Yii::createObject($bundle);
} }
} }
} }
foreach ($result as $name => $bundle) {
$this->loadBundleDependency($name, $bundle, $result);
}
return $result; return $result;
} }
/** /**
* Loads asset bundle dependencies recursively.
* @param string $name bundle name
* @param \yii\web\AssetBundle $bundle bundle instance
* @param array $result already loaded bundles list.
* @throws \yii\console\Exception on failure.
*/
protected function loadBundleDependency($name, $bundle, &$result) {
if (!empty($bundle->depends)) {
$assetManager = $this->getAssetManager();
foreach ($bundle->depends as $dependencyName) {
if (!array_key_exists($dependencyName, $result)) {
$dependencyBundle = $assetManager->getBundle($dependencyName);
if ($dependencyBundle === null) {
throw new Exception("Unable to load dependency bundle '{$dependencyName}' for bundle '{$name}'.");
} else {
$result[$dependencyName] = $dependencyBundle;
$this->loadBundleDependency($dependencyName, $dependencyBundle, $result);
}
}
}
}
}
/**
* Creates full list of output asset bundles. * Creates full list of output asset bundles.
* @param array $targets output asset bundles configuration. * @param array $targets output asset bundles configuration.
* @param \yii\web\AssetBundle[] $bundles list of source asset bundles. * @param \yii\web\AssetBundle[] $bundles list of source asset bundles.
@ -222,17 +279,13 @@ class AssetController extends Controller
/** /**
* Publishes given asset bundles. * Publishes given asset bundles.
* @param \yii\web\AssetBundle[] $bundles asset bundles to be published. * @param \yii\web\AssetBundle[] $bundles asset bundles to be published.
* @param array $options assert manager instance configuration.
*/ */
protected function publishBundles($bundles, $options) protected function publishBundles($bundles)
{ {
echo "\nPublishing bundles:\n"; echo "\nPublishing bundles:\n";
if (!isset($options['class'])) { $assetManager = $this->getAssetManager();
$options['class'] = 'yii\\web\\AssetManager';
}
$am = Yii::createObject($options);
foreach ($bundles as $name => $bundle) { foreach ($bundles as $name => $bundle) {
$bundle->publish($am); $bundle->publish($assetManager);
echo " '".$name."' published.\n"; echo " '".$name."' published.\n";
} }
echo "\n"; echo "\n";
@ -252,14 +305,20 @@ class AssetController extends Controller
'{ts}' => $timestamp, '{ts}' => $timestamp,
)); ));
$inputFiles = array(); $inputFiles = array();
foreach ($target->depends as $name) { foreach ($target->depends as $name) {
if (isset($bundles[$name])) { if (isset($bundles[$name])) {
foreach ($bundles[$name]->$type as $file) { $bundle = $bundles[$name];
$inputFiles[] = $bundles[$name]->basePath . $file; foreach ($bundle->$type as $file) {
if ($bundle->sourcePath === null) {
// native :
$inputFiles[] = $bundle->basePath . $file;
} else {
// published :
$inputFiles[] = $this->getAssetManager()->basePath . $file;
}
} }
} else { } else {
throw new Exception("Unknown bundle: $name"); throw new Exception("Unknown bundle: '{$name}'");
} }
} }
if ($type === 'js') { if ($type === 'js') {
@ -339,6 +398,8 @@ class AssetController extends Controller
* Saves new asset bundles configuration. * Saves new asset bundles configuration.
* @param \yii\web\AssetBundle[] $targets list of asset bundles to be saved. * @param \yii\web\AssetBundle[] $targets list of asset bundles to be saved.
* @param string $bundleFile output file name. * @param string $bundleFile output file name.
* @throws \yii\console\Exception on failure.
* @return void
*/ */
protected function saveTargets($targets, $bundleFile) protected function saveTargets($targets, $bundleFile)
{ {

Loading…
Cancel
Save