Browse Source

Fix for asset command.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
a128bffb47
  1. 10
      framework/yii/base/View.php
  2. 119
      framework/yii/console/controllers/AssetController.php
  3. 2
      framework/yii/web/AssetBundle.php

10
framework/yii/base/View.php

@ -575,13 +575,9 @@ class View extends Component
if (!isset($this->assetBundles[$name])) { if (!isset($this->assetBundles[$name])) {
$am = $this->getAssetManager(); $am = $this->getAssetManager();
$bundle = $am->getBundle($name); $bundle = $am->getBundle($name);
if ($bundle !== null) { $this->assetBundles[$name] = false;
$this->assetBundles[$name] = false; $bundle->registerAssets($this);
$bundle->registerAssets($this); $this->assetBundles[$name] = true;
$this->assetBundles[$name] = true;
} else {
throw new InvalidConfigException("Unknown asset bundle: $name");
}
} elseif ($this->assetBundles[$name] === false) { } elseif ($this->assetBundles[$name] === false) {
throw new InvalidConfigException("A circular dependency is detected for bundle '$name'."); throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
} }

119
framework/yii/console/controllers/AssetController.php

@ -41,36 +41,26 @@ class AssetController extends Controller
public $defaultAction = 'compress'; public $defaultAction = 'compress';
/** /**
* @var array list of asset bundles to be compressed. * @var array list of asset bundles to be compressed.
* The keys are the bundle names, and the values are the configuration
* arrays for creating the [[yii\web\AssetBundle]] objects.
*/ */
public $bundles = array(); public $bundles = array();
/** /**
* @var array list of paths to the extensions, which assets should be also compressed.
* Each path should contain asset manifest file named "assets.php".
*/
public $extensions = array();
/**
* @var array list of asset bundles, which represents output compressed files. * @var array list of asset bundles, which represents output compressed files.
* You can specify the name of the output compressed file using 'css' and 'js' keys: * You can specify the name of the output compressed file using 'css' and 'js' keys:
* For example: * For example:
*
* ~~~ * ~~~
* 'all' => array( * 'app\config\AllAsset' => array(
* 'css' => 'all.css', * 'js' => 'js/all-{ts}.js',
* 'js' => 'all.js', * 'css' => 'css/all-{ts}.css',
* 'depends' => array( ... ), * 'depends' => array( ... ),
* ) * )
* ~~~ * ~~~
*
* File names can contain placeholder "{ts}", which will be filled by current timestamp, while * File names can contain placeholder "{ts}", which will be filled by current timestamp, while
* file creation. * file creation.
*/ */
public $targets = array(); public $targets = array();
/** /**
* @var array|\yii\web\AssetManager [[yii\web\AssetManager]] instance or its array configuration, which will be used
* for assets processing.
*/
private $_assetManager = array();
/**
* @var string|callback JavaScript file compressor. * @var string|callback JavaScript 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
* placeholders {from} - source file name - and {to} - output file name. * placeholders {from} - source file name - and {to} - output file name.
@ -92,6 +82,12 @@ class AssetController extends Controller
public $cssCompressor = 'java -jar yuicompressor.jar {from} -o {to}'; public $cssCompressor = 'java -jar yuicompressor.jar {from} -o {to}';
/** /**
* @var array|\yii\web\AssetManager [[yii\web\AssetManager]] instance or its array configuration, which will be used
* for assets processing.
*/
private $_assetManager = array();
/**
* Returns the asset manager instance. * Returns the asset manager instance.
* @throws \yii\console\Exception on invalid configuration. * @throws \yii\console\Exception on invalid configuration.
* @return \yii\web\AssetManager asset manager instance. * @return \yii\web\AssetManager asset manager instance.
@ -137,7 +133,7 @@ class AssetController extends Controller
public function actionCompress($configFile, $bundleFile) public function actionCompress($configFile, $bundleFile)
{ {
$this->loadConfiguration($configFile); $this->loadConfiguration($configFile);
$bundles = $this->loadBundles($this->bundles, $this->extensions); $bundles = $this->loadBundles($this->bundles);
$targets = $this->loadTargets($this->targets, $bundles); $targets = $this->loadTargets($this->targets, $bundles);
$this->publishBundles($bundles, $this->assetManager); $this->publishBundles($bundles, $this->assetManager);
$timestamp = time(); $timestamp = time();
@ -177,37 +173,20 @@ class AssetController extends Controller
/** /**
* Creates full list of source asset bundles. * Creates full list of source asset bundles.
* @param array[] $bundles list of asset bundle configurations. * @param string[] $bundles list of asset bundle names
* @param array $extensions list of the extension paths.
* @return \yii\web\AssetBundle[] list of source asset bundles. * @return \yii\web\AssetBundle[] list of source asset bundles.
*/ */
protected function loadBundles($bundles, $extensions) protected function loadBundles($bundles)
{ {
echo "Collecting source bundles information...\n"; echo "Collecting source bundles information...\n";
$assetManager = $this->getAssetManager(); $am = $this->getAssetManager();
$result = array(); $result = array();
foreach ($bundles as $name) {
$assetManager->bundles = $bundles; $result[$name] = $am->getBundle($name);
foreach ($assetManager->bundles as $name => $bundle) {
$result[$name] = $assetManager->getBundle($name);
} }
foreach ($result as $bundle) {
foreach ($extensions as $path) { $this->loadDependency($bundle, $result);
$manifest = $path . '/assets.php';
if (!is_file($manifest)) {
continue;
}
$assetManager->bundles = require($manifest);
foreach ($assetManager->bundles as $name => $bundle) {
if (!isset($result[$name])) {
$result[$name] = $assetManager->getBundle($name);
}
}
}
foreach ($result as $name => $bundle) {
$this->loadBundleDependency($name, $bundle, $result);
} }
return $result; return $result;
@ -215,30 +194,21 @@ class AssetController extends Controller
/** /**
* Loads asset bundle dependencies recursively. * Loads asset bundle dependencies recursively.
* @param string $name bundle name
* @param \yii\web\AssetBundle $bundle bundle instance * @param \yii\web\AssetBundle $bundle bundle instance
* @param array $result already loaded bundles list. * @param array $result already loaded bundles list.
* @throws \yii\console\Exception on failure. * @throws Exception on failure.
*/ */
protected function loadBundleDependency($name, $bundle, &$result) protected function loadDependency($bundle, &$result)
{ {
if (!empty($bundle->depends)) { $am = $this->getAssetManager();
$assetManager = $this->getAssetManager(); foreach ($bundle->depends as $name) {
foreach ($bundle->depends as $dependencyName) { if (!isset($result[$name])) {
if (!array_key_exists($dependencyName, $result)) { $dependencyBundle = $am->getBundle($name);
$dependencyBundle = $assetManager->getBundle($dependencyName); $result[$name] = false;
if ($dependencyBundle === null) { $this->loadDependency($dependencyBundle, $result);
throw new Exception("Unable to load dependency bundle '{$dependencyName}' for bundle '{$name}'."); $result[$name] = $dependencyBundle;
} else { } elseif ($result[$name] === false) {
$result[$dependencyName] = false; throw new Exception("A circular dependency is detected for bundle '$name'.");
$this->loadBundleDependency($dependencyName, $dependencyBundle, $result);
$result[$dependencyName] = $dependencyBundle;
}
} else {
if ($result[$dependencyName] === false) {
throw new Exception("A circular dependency is detected for target '{$dependencyName}'.");
}
}
} }
} }
} }
@ -248,7 +218,7 @@ class AssetController extends Controller
* @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.
* @return \yii\web\AssetBundle[] list of output asset bundles. * @return \yii\web\AssetBundle[] list of output asset bundles.
* @throws \yii\console\Exception on failure. * @throws Exception on failure.
*/ */
protected function loadTargets($targets, $bundles) protected function loadTargets($targets, $bundles)
{ {
@ -298,7 +268,7 @@ class AssetController extends Controller
return $bundleOrders[$a] > $bundleOrders[$b] ? 1 : -1; return $bundleOrders[$a] > $bundleOrders[$b] ? 1 : -1;
} }
}); });
$target['class'] = 'yii\\web\\AssetBundle'; $target['class'] = $name;
$targets[$name] = Yii::createObject($target); $targets[$name] = Yii::createObject($target);
} }
return $targets; return $targets;
@ -399,7 +369,7 @@ class AssetController extends Controller
* @param \yii\web\AssetBundle[] $bundles asset bundles list. * @param \yii\web\AssetBundle[] $bundles asset bundles list.
* @param string $name bundle name. * @param string $name bundle name.
* @param array $registered stores already registered names. * @param array $registered stores already registered names.
* @throws \yii\console\Exception if circular dependency is detected. * @throws Exception if circular dependency is detected.
*/ */
protected function registerBundle($bundles, $name, &$registered) protected function registerBundle($bundles, $name, &$registered)
{ {
@ -617,27 +587,28 @@ EOD;
<?php <?php
/** /**
* Configuration file for the "yii asset" console command. * Configuration file for the "yii asset" console command.
* Note: in the console environment some path aliases like '@wwwroot' and '@www' may not exist, * Note that in the console environment, some path aliases like '@wwwroot' and '@www' may not exist.
* so corresponding paths should be specified directly. * Please define these missing path aliases.
*/ */
return array( return array(
// The list of asset bundles to compress: // The list of asset bundles to compress:
'bundles' => require('path/to/bundles.php'), 'bundles' => array(
// The list of extensions to compress: // 'yii\web\YiiAsset',
'extensions' => require('path/to/namespaces.php'), // 'yii\web\JqueryAsset',
),
// Asset bundle for compression output: // Asset bundle for compression output:
'targets' => array( 'targets' => array(
'all' => array( 'app\config\AllAsset' => array(
'basePath' => __DIR__, 'basePath' => 'path/to/www',
'baseUrl' => '/test', 'baseUrl' => '',
'js' => 'all-{ts}.js', 'js' => 'js/all-{ts}.js',
'css' => 'all-{ts}.css', 'css' => 'css/all-{ts}.css',
), ),
), ),
// Asset manager configuration: // Asset manager configuration:
'assetManager' => array( 'assetManager' => array(
'basePath' => __DIR__, 'basePath' => __DIR__,
'baseUrl' => '/test', 'baseUrl' => '',
), ),
); );
EOD; EOD;

2
framework/yii/web/AssetBundle.php

@ -46,7 +46,7 @@ class AssetBundle extends Object
* when it publishes the asset files from [[sourcePath]]. * when it publishes the asset files from [[sourcePath]].
* *
* If the bundle contains any assets that are specified in terms of relative file path, * If the bundle contains any assets that are specified in terms of relative file path,
* then this property must be set either manually or automatically (by asset manager via * then this property must be set either manually or automatically (by [[AssetManager]] via
* asset publishing). * asset publishing).
* *
* You can use either a directory or an alias of the directory. * You can use either a directory or an alias of the directory.

Loading…
Cancel
Save