From 7775e927e12e2ba6a81d7046df0e228df2b1b608 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 22 Apr 2013 06:50:22 -0400 Subject: [PATCH] script command WIP --- framework/console/controllers/ScriptController.php | 156 ++++++++++++++++++++- framework/web/AssetBundle.php | 15 +- 2 files changed, 165 insertions(+), 6 deletions(-) diff --git a/framework/console/controllers/ScriptController.php b/framework/console/controllers/ScriptController.php index 7ca498b..44a5818 100644 --- a/framework/console/controllers/ScriptController.php +++ b/framework/console/controllers/ScriptController.php @@ -17,10 +17,160 @@ use yii\console\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, << $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 diff --git a/framework/web/AssetBundle.php b/framework/web/AssetBundle.php index 4108b07..9fc2bbf 100644 --- a/framework/web/AssetBundle.php +++ b/framework/web/AssetBundle.php @@ -132,9 +132,7 @@ class AssetBundle extends Object $view->registerAssetBundle($name); } - if ($this->sourcePath !== null) { - list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); - } + $this->publish($am); $converter = $am->getConverter(); @@ -161,4 +159,15 @@ class AssetBundle extends Object $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