mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
script WIP
This commit is contained in:
@ -592,7 +592,7 @@ class View extends Component
|
||||
* Registers the named asset bundle.
|
||||
* All dependent asset bundles will be registered.
|
||||
* @param string $name the name of the asset bundle.
|
||||
* @throws InvalidConfigException if the asset bundle does not exist or a cyclic dependency is detected
|
||||
* @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected
|
||||
*/
|
||||
public function registerAssetBundle($name)
|
||||
{
|
||||
@ -607,7 +607,7 @@ class View extends Component
|
||||
throw new InvalidConfigException("Unknown asset bundle: $name");
|
||||
}
|
||||
} elseif ($this->assetBundles[$name] === false) {
|
||||
throw new InvalidConfigException("A cyclic dependency is detected for bundle '$name'.");
|
||||
throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -32,41 +32,26 @@ class ScriptController extends Controller
|
||||
* ~~~
|
||||
*/
|
||||
public $targets = array();
|
||||
public $basePath;
|
||||
public $baseUrl;
|
||||
public $publishOptions = array();
|
||||
public $assetManager = array();
|
||||
|
||||
public function actionCompress($configFile, $bundleFile)
|
||||
{
|
||||
$this->loadConfiguration($configFile);
|
||||
$bundles = $this->loadBundles($this->bundles, $this->extensions);
|
||||
$this->publishBundles($bundles, $this->publishOptions);
|
||||
$targets = $this->loadTargets($this->targets, $bundles);
|
||||
// $this->publishBundles($bundles, $this->publishOptions);
|
||||
$timestamp = time();
|
||||
$targets = array();
|
||||
foreach ($this->targets as $name => $target) {
|
||||
$target['basePath'] = $this->basePath;
|
||||
$target['baseUrl'] = $this->baseUrl;
|
||||
if (isset($target['js'])) {
|
||||
foreach ($targets as $target) {
|
||||
if (!empty($target->js)) {
|
||||
$this->buildTarget($target, 'js', $bundles, $timestamp);
|
||||
}
|
||||
if (isset($target['css'])) {
|
||||
if (!empty($target->css)) {
|
||||
$this->buildTarget($target, 'css', $bundles, $timestamp);
|
||||
}
|
||||
$targets[$name] = $target;
|
||||
}
|
||||
|
||||
$targets = $this->adjustDependency($targets, $bundles);
|
||||
$array = var_export($targets, true);
|
||||
$version = date('Y-m-d H:i:s', time());
|
||||
file_put_contents($bundleFile, <<<EOD
|
||||
<?php
|
||||
/**
|
||||
* Do not modify this file manually as it is automatically generated by the "yiic script" command.
|
||||
* @version $version
|
||||
*/
|
||||
return $array;
|
||||
EOD
|
||||
);
|
||||
$this->saveTargets($targets, $bundleFile);
|
||||
}
|
||||
|
||||
protected function loadConfiguration($configFile)
|
||||
@ -75,21 +60,16 @@ EOD
|
||||
if (property_exists($this, $name)) {
|
||||
$this->$name = $value;
|
||||
} else {
|
||||
throw new Exception("Unknown configuration: $name");
|
||||
throw new Exception("Unknown configuration option: $name");
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($this->basePath)) {
|
||||
throw new Exception("Please specify the 'basePath' option.");
|
||||
if (!isset($this->assetManager['basePath'])) {
|
||||
throw new Exception("Please specify 'basePath' for the 'assetManager' option.");
|
||||
}
|
||||
if (!is_dir($this->basePath)) {
|
||||
throw new Exception("The 'basePath' directory does not exist: {$this->basePath}");
|
||||
if (!isset($this->assetManager['baseUrl'])) {
|
||||
throw new Exception("Please specify 'baseUrl' for the 'assetManager' option.");
|
||||
}
|
||||
if (!isset($this->baseUrl)) {
|
||||
throw new Exception("Please specify the 'baseUrl' option.");
|
||||
}
|
||||
$this->publishOptions['basePath'] = $this->basePath;
|
||||
$this->publishOptions['baseUrl'] = $this->baseUrl;
|
||||
}
|
||||
|
||||
protected function loadBundles($bundles, $extensions)
|
||||
@ -114,6 +94,33 @@ EOD
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function loadTargets($targets, $bundles)
|
||||
{
|
||||
$registered = array();
|
||||
foreach ($bundles as $name => $bundle) {
|
||||
$this->registerBundle($bundles, $name, $registered);
|
||||
}
|
||||
$bundleOrders = array_combine(array_keys($registered), range(0, count($bundles) - 1));
|
||||
foreach ($targets as $name => $target) {
|
||||
if (!isset($target['basePath'])) {
|
||||
throw new Exception("Please specify 'basePath' for the '$name' target.");
|
||||
}
|
||||
if (!isset($target['baseUrl'])) {
|
||||
throw new Exception("Please specify 'baseUrl' for the '$name' target.");
|
||||
}
|
||||
usort($target['depends'], function ($a, $b) use ($bundleOrders) {
|
||||
if ($bundleOrders[$a] == $bundleOrders[$b]) {
|
||||
return 0;
|
||||
} else {
|
||||
return $bundleOrders[$a] > $bundleOrders[$b] ? 1 : -1;
|
||||
}
|
||||
});
|
||||
$target['class'] = 'yii\\web\\AssetBundle';
|
||||
$targets[$name] = Yii::createObject($target);
|
||||
}
|
||||
return $targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \yii\web\AssetBundle[] $bundles
|
||||
* @param array $options
|
||||
@ -130,19 +137,20 @@ EOD
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $target
|
||||
* @param \yii\web\AssetBundle $target
|
||||
* @param string $type either "js" or "css"
|
||||
* @param \yii\web\AssetBundle[] $bundles
|
||||
* @param integer $timestamp
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function buildTarget(&$target, $type, $bundles, $timestamp)
|
||||
protected function buildTarget($target, $type, $bundles, $timestamp)
|
||||
{
|
||||
$outputFile = strtr($target[$type], array(
|
||||
$outputFile = strtr($target->$type, array(
|
||||
'{ts}' => $timestamp,
|
||||
));
|
||||
$inputFiles = array();
|
||||
foreach ($target['depends'] as $name) {
|
||||
|
||||
foreach ($target->depends as $name) {
|
||||
if (isset($bundles[$name])) {
|
||||
foreach ($bundles[$name]->$type as $file) {
|
||||
$inputFiles[] = $bundles[$name]->basePath . '/' . $file;
|
||||
@ -152,18 +160,90 @@ EOD
|
||||
}
|
||||
}
|
||||
if ($type === 'js') {
|
||||
$this->compressJsFiles($inputFiles, $target['basePath'] . '/' . $outputFile);
|
||||
$this->compressJsFiles($inputFiles, $target->basePath . '/' . $outputFile);
|
||||
} else {
|
||||
$this->compressCssFiles($inputFiles, $target['basePath'] . '/' . $outputFile);
|
||||
$this->compressCssFiles($inputFiles, $target->basePath . '/' . $outputFile);
|
||||
}
|
||||
$target[$type] = array($outputFile);
|
||||
$target->$type = array($outputFile);
|
||||
}
|
||||
|
||||
protected function adjustDependency($targets, $bundles)
|
||||
{
|
||||
$map = array();
|
||||
foreach ($targets as $name => $target) {
|
||||
foreach ($target->depends as $bundle) {
|
||||
if (!isset($map[$bundle])) {
|
||||
$map[$bundle] = $name;
|
||||
} else {
|
||||
throw new Exception("Bundle '$bundle' is found in both target '{$map[$bundle]}' and '$name'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($targets as $name => $target) {
|
||||
$depends = array();
|
||||
foreach ($target->depends as $bn) {
|
||||
foreach ($bundles[$bn]->depends as $bundle) {
|
||||
$depends[$map[$bundle]] = true;
|
||||
}
|
||||
}
|
||||
unset($depends[$name]);
|
||||
$target->depends = array_keys($depends);
|
||||
}
|
||||
|
||||
// detect possible circular dependencies
|
||||
foreach ($targets as $name => $target) {
|
||||
$registered = array();
|
||||
$this->registerBundle($targets, $name, $registered);
|
||||
}
|
||||
|
||||
foreach ($map as $bundle => $target) {
|
||||
$targets[$bundle] = Yii::createObject(array(
|
||||
'class' => 'yii\\web\\AssetBundle',
|
||||
'depends' => array($target),
|
||||
));
|
||||
}
|
||||
return $targets;
|
||||
}
|
||||
|
||||
protected function registerBundle($bundles, $name, &$registered)
|
||||
{
|
||||
if (!isset($registered[$name])) {
|
||||
$registered[$name] = false;
|
||||
$bundle = $bundles[$name];
|
||||
foreach ($bundle->depends as $depend) {
|
||||
$this->registerBundle($bundles, $depend, $registered);
|
||||
}
|
||||
unset($registered[$name]);
|
||||
$registered[$name] = true;
|
||||
} elseif ($registered[$name] === false) {
|
||||
throw new Exception("A circular dependency is detected for target '$name'.");
|
||||
}
|
||||
}
|
||||
|
||||
protected function saveTargets($targets, $bundleFile)
|
||||
{
|
||||
$array = array();
|
||||
foreach ($targets as $name => $target) {
|
||||
foreach (array('js', 'css', 'depends', 'basePath', 'baseUrl') as $prop) {
|
||||
if (!empty($target->$prop)) {
|
||||
$array[$name][$prop] = $target->$prop;
|
||||
}
|
||||
}
|
||||
}
|
||||
$array = var_export($array, true);
|
||||
$version = date('Y-m-d H:i:s', time());
|
||||
file_put_contents($bundleFile, <<<EOD
|
||||
<?php
|
||||
/**
|
||||
* Do not modify this file manually as it is automatically generated by the "yiic script" command.
|
||||
* @version $version
|
||||
*/
|
||||
return $array;
|
||||
EOD
|
||||
);
|
||||
}
|
||||
|
||||
protected function compressJsFiles($inputFiles, $outputFile)
|
||||
{
|
||||
|
||||
|
||||
@ -65,16 +65,16 @@ class AssetBundle extends Object
|
||||
* You can use either a URL or an alias of the URL.
|
||||
*/
|
||||
public $baseUrl;
|
||||
/**
|
||||
* @var array list of the bundle names that this bundle depends on
|
||||
*/
|
||||
public $depends = array();
|
||||
/**
|
||||
* @var array list of JavaScript files that this bundle contains. Each JavaScript file can
|
||||
* be either a file path (without leading slash) relative to [[basePath]] or a URL representing
|
||||
* an external JavaScript file.
|
||||
*
|
||||
* Note that only forward slash "/" can be used as directory separator.
|
||||
*
|
||||
* Each JavaScript file may be associated with options. In this case, the array key
|
||||
* should be the JavaScript file path, while the corresponding array value should
|
||||
* be the option array. The options will be passed to [[View::registerJsFile()]].
|
||||
*/
|
||||
public $js = array();
|
||||
/**
|
||||
@ -83,16 +83,18 @@ class AssetBundle extends Object
|
||||
* an external CSS file.
|
||||
*
|
||||
* Note that only forward slash "/" can be used as directory separator.
|
||||
*
|
||||
* Each CSS file may be associated with options. In this case, the array key
|
||||
* should be the CSS file path, while the corresponding array value should
|
||||
* be the option array. The options will be passed to [[View::registerCssFile()]].
|
||||
*/
|
||||
public $css = array();
|
||||
/**
|
||||
* @var array list of the bundle names that this bundle depends on
|
||||
* @var array the options that will be passed to [[\yii\base\View::registerJsFile()]]
|
||||
* when registering the JS files in this bundle.
|
||||
*/
|
||||
public $depends = array();
|
||||
public $jsOptions = array();
|
||||
/**
|
||||
* @var array the options that will be passed to [[\yii\base\View::registerCssFile()]]
|
||||
* when registering the CSS files in this bundle.
|
||||
*/
|
||||
public $cssOptions = array();
|
||||
/**
|
||||
* @var array the options to be passed to [[AssetManager::publish()]] when the asset bundle
|
||||
* is being published.
|
||||
@ -126,48 +128,49 @@ class AssetBundle extends Object
|
||||
*/
|
||||
public function registerAssets($view)
|
||||
{
|
||||
$am = $view->getAssetManager();
|
||||
|
||||
foreach ($this->depends as $name) {
|
||||
$view->registerAssetBundle($name);
|
||||
}
|
||||
|
||||
$this->publish($am);
|
||||
$this->publish($view->getAssetManager());
|
||||
|
||||
$converter = $am->getConverter();
|
||||
|
||||
foreach ($this->js as $js => $options) {
|
||||
$js = is_string($options) ? $options : $js;
|
||||
if (strpos($js, '/') !== 0 && strpos($js, '://') === false) {
|
||||
if (isset($this->basePath, $this->baseUrl)) {
|
||||
$js = $converter->convert($js, $this->basePath, $this->baseUrl);
|
||||
} else {
|
||||
throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.');
|
||||
foreach ($this->js as $js) {
|
||||
$view->registerJsFile($js, $this->jsOptions);
|
||||
}
|
||||
}
|
||||
$view->registerJsFile($js, is_array($options) ? $options : array());
|
||||
}
|
||||
foreach ($this->css as $css => $options) {
|
||||
$css = is_string($options) ? $options : $css;
|
||||
if (strpos($css, '/') !== 0 && strpos($css, '://') === false) {
|
||||
if (isset($this->basePath, $this->baseUrl)) {
|
||||
$css = $converter->convert($css, $this->basePath, $this->baseUrl);
|
||||
} else {
|
||||
throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.');
|
||||
}
|
||||
}
|
||||
$view->registerCssFile($css, is_array($options) ? $options : array());
|
||||
foreach ($this->css as $css) {
|
||||
$view->registerCssFile($css, $this->cssOptions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes the asset bundle if its source code is not under Web-accessible directory.
|
||||
* @param AssetManager $am the asset manager to perform the asset publishing
|
||||
* @throws InvalidConfigException if [[baseUrl]] or [[basePath]] is not set when the bundle
|
||||
* contains internal CSS or JS files.
|
||||
*/
|
||||
public function publish($am)
|
||||
{
|
||||
if ($this->sourcePath !== null) {
|
||||
list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
|
||||
}
|
||||
$converter = $am->getConverter();
|
||||
foreach ($this->js as $i => $js) {
|
||||
if (strpos($js, '/') !== 0 && strpos($js, '://') === false) {
|
||||
if (isset($this->basePath, $this->baseUrl)) {
|
||||
$this->js[$i] = $converter->convert($js, $this->basePath, $this->baseUrl);
|
||||
} else {
|
||||
throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.');
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($this->css as $i => $css) {
|
||||
if (strpos($css, '/') !== 0 && strpos($css, '://') === false) {
|
||||
if (isset($this->basePath, $this->baseUrl)) {
|
||||
$this->css[$i] = $converter->convert($css, $this->basePath, $this->baseUrl);
|
||||
} else {
|
||||
throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user