Commit 7775e927 by Qiang Xue

script command WIP

parent 304122eb
...@@ -17,10 +17,160 @@ use yii\console\Controller; ...@@ -17,10 +17,160 @@ use yii\console\Controller;
*/ */
class ScriptController extends Controller class ScriptController extends Controller
{ {
public $defaultAction = 'combo'; public $defaultAction = 'compress';
public function actionCombo($configFile) public $bundles = array();
public $extensions = array();
/**
* @var array
* ~~~
* 'all' => array(
* 'css' => 'all.css',
* 'js' => 'js.css',
* 'depends' => array( ... ),
* )
* ~~~
*/
public $targets = array();
public $basePath;
public $baseUrl;
public $publishOptions = array();
public function actionCompress($configFile, $bundleFile)
{
$this->loadConfiguration($configFile);
$bundles = $this->loadBundles($this->bundles, $this->extensions);
$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'])) {
$this->buildTarget($target, 'js', $bundles, $timestamp);
}
if (isset($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
);
}
protected function loadConfiguration($configFile)
{
foreach (require($configFile) as $name => $value) {
if (property_exists($this, $name)) {
$this->$name = $value;
} else {
throw new Exception("Unknown configuration: $name");
}
}
if (!isset($this->basePath)) {
throw new Exception("Please specify the 'basePath' option.");
}
if (!is_dir($this->basePath)) {
throw new Exception("The 'basePath' directory does not exist: {$this->basePath}");
}
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)
{
$result = array();
foreach ($bundles as $name => $bundle) {
$bundle['class'] = 'yii\\web\\AssetBundle';
$result[$name] = Yii::createObject($bundle);
}
foreach ($extensions as $path) {
$manifest = $path . '/assets.php';
if (!is_file($manifest)) {
continue;
}
foreach (require($manifest) as $name => $bundle) {
if (!isset($result[$name])) {
$bundle['class'] = 'yii\\web\\AssetBundle';
$result[$name] = Yii::createObject($bundle);
}
}
}
return $result;
}
/**
* @param \yii\web\AssetBundle[] $bundles
* @param array $options
*/
protected function publishBundles($bundles, $options)
{ {
if (!isset($options['class'])) {
$options['class'] = 'yii\\web\\AssetManager';
}
$am = Yii::createObject($options);
foreach ($bundles as $bundle) {
$bundle->publish($am);
}
}
/**
* @param array $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)
{
$outputFile = strtr($target[$type], array(
'{ts}' => $timestamp,
));
$inputFiles = array();
foreach ($target['depends'] as $name) {
if (isset($bundles[$name])) {
foreach ($bundles[$name]->$type as $file) {
$inputFiles[] = $bundles[$name]->basePath . '/' . $file;
}
} else {
throw new Exception("Unknown bundle: $name");
}
}
if ($type === 'js') {
$this->compressJsFiles($inputFiles, $target['basePath'] . '/' . $outputFile);
} else {
$this->compressCssFiles($inputFiles, $target['basePath'] . '/' . $outputFile);
}
$target[$type] = array($outputFile);
}
protected function adjustDependency($targets, $bundles)
{
return $targets;
}
protected function compressJsFiles($inputFiles, $outputFile)
{
}
protected function compressCssFiles($inputFiles, $outputFile)
{
} }
} }
\ No newline at end of file
...@@ -132,9 +132,7 @@ class AssetBundle extends Object ...@@ -132,9 +132,7 @@ class AssetBundle extends Object
$view->registerAssetBundle($name); $view->registerAssetBundle($name);
} }
if ($this->sourcePath !== null) { $this->publish($am);
list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
}
$converter = $am->getConverter(); $converter = $am->getConverter();
...@@ -161,4 +159,15 @@ class AssetBundle extends Object ...@@ -161,4 +159,15 @@ class AssetBundle extends Object
$view->registerCssFile($css, is_array($options) ? $options : array()); $view->registerCssFile($css, is_array($options) ? $options : array());
} }
} }
/**
* 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
*/
public function publish($am)
{
if ($this->sourcePath !== null) {
list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
}
}
} }
\ No newline at end of file
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