Browse Source

initial implementation of asset command.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
e132ed8d18
  1. 2
      framework/console/Application.php
  2. 115
      framework/console/controllers/AssetController.php

2
framework/console/Application.php

@ -129,7 +129,7 @@ class Application extends \yii\base\Application
'migrate' => 'yii\console\controllers\MigrateController', 'migrate' => 'yii\console\controllers\MigrateController',
'app' => 'yii\console\controllers\AppController', 'app' => 'yii\console\controllers\AppController',
'cache' => 'yii\console\controllers\CacheController', 'cache' => 'yii\console\controllers\CacheController',
'script' => 'yii\console\controllers\ScriptController', 'asset' => 'yii\console\controllers\AssetController',
); );
} }

115
framework/console/controllers/ScriptController.php → framework/console/controllers/AssetController.php

@ -15,7 +15,7 @@ use yii\console\Controller;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class ScriptController extends Controller class AssetController extends Controller
{ {
public $defaultAction = 'compress'; public $defaultAction = 'compress';
@ -33,13 +33,15 @@ class ScriptController extends Controller
*/ */
public $targets = array(); public $targets = array();
public $assetManager = array(); public $assetManager = array();
public $jsCompressor = 'java -jar compiler.jar --js {from} --js_output_file {to}';
public $cssCompressor = 'java -jar yuicompressor.jar {from} -o {to}';
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, $this->extensions);
$targets = $this->loadTargets($this->targets, $bundles); $targets = $this->loadTargets($this->targets, $bundles);
// $this->publishBundles($bundles, $this->publishOptions); $this->publishBundles($bundles, $this->publishOptions);
$timestamp = time(); $timestamp = time();
foreach ($targets as $target) { foreach ($targets as $target) {
if (!empty($target->js)) { if (!empty($target->js)) {
@ -96,11 +98,38 @@ class ScriptController extends Controller
protected function loadTargets($targets, $bundles) protected function loadTargets($targets, $bundles)
{ {
// build the dependency order of bundles
$registered = array(); $registered = array();
foreach ($bundles as $name => $bundle) { foreach ($bundles as $name => $bundle) {
$this->registerBundle($bundles, $name, $registered); $this->registerBundle($bundles, $name, $registered);
} }
$bundleOrders = array_combine(array_keys($registered), range(0, count($bundles) - 1)); $bundleOrders = array_combine(array_keys($registered), range(0, count($bundles) - 1));
// fill up the target which has empty 'depends'.
$referenced = array();
foreach ($targets as $name => $target) {
if (empty($target['depends'])) {
if (!isset($all)) {
$all = $name;
} else {
throw new Exception("Only one target can have empty 'depends' option. Found two now: $all, $name");
}
} else {
foreach ($target['depends'] as $bundle) {
if (!isset($referenced[$bundle])) {
$referenced[$bundle] = $name;
} else {
throw new Exception("Target '{$referenced[$bundle]}' and '$name' cannot contain the bundle '$bundle' at the same time.");
}
}
}
}
if (isset($all)) {
$targets[$all]['depends'] = array_diff(array_keys($registered), array_keys($referenced));
}
// adjust the 'depends' order for each target according to the dependency order of bundles
// create an AssetBundle object for each target
foreach ($targets as $name => $target) { foreach ($targets as $name => $target) {
if (!isset($target['basePath'])) { if (!isset($target['basePath'])) {
throw new Exception("Please specify 'basePath' for the '$name' target."); throw new Exception("Please specify 'basePath' for the '$name' target.");
@ -172,11 +201,7 @@ class ScriptController extends Controller
$map = array(); $map = array();
foreach ($targets as $name => $target) { foreach ($targets as $name => $target) {
foreach ($target->depends as $bundle) { foreach ($target->depends as $bundle) {
if (!isset($map[$bundle])) { $map[$bundle] = $name;
$map[$bundle] = $name;
} else {
throw new Exception("Bundle '$bundle' is found in both target '{$map[$bundle]}' and '$name'.");
}
} }
} }
@ -236,7 +261,8 @@ class ScriptController extends Controller
file_put_contents($bundleFile, <<<EOD file_put_contents($bundleFile, <<<EOD
<?php <?php
/** /**
* Do not modify this file manually as it is automatically generated by the "yiic script" command. * This file is generated by the "yiic script" command.
* DO NOT MODIFY THIS FILE DIRECTLY.
* @version $version * @version $version
*/ */
return $array; return $array;
@ -246,11 +272,82 @@ EOD
protected function compressJsFiles($inputFiles, $outputFile) protected function compressJsFiles($inputFiles, $outputFile)
{ {
if (is_string($this->jsCompressor)) {
$tmpFile = $outputFile . '.tmp';
$this->combineJsFiles($inputFiles, $tmpFile);
$log = shell_exec(strtr($this->jsCompressor, array(
'{from}' => $tmpFile,
'{to}' => $outputFile,
)));
@unlink($tmpFile);
} else {
$log = call_user_func($this->jsCompressor, $this, $inputFiles, $outputFile);
}
} }
protected function compressCssFiles($inputFiles, $outputFile) protected function compressCssFiles($inputFiles, $outputFile)
{ {
if (is_string($this->cssCompressor)) {
$tmpFile = $outputFile . '.tmp';
$this->combineCssFiles($inputFiles, $tmpFile);
$log = shell_exec(strtr($this->cssCompressor, array(
'{from}' => $inputFiles,
'{to}' => $outputFile,
)));
} else {
$log = call_user_func($this->cssCompressor, $this, $inputFiles, $outputFile);
}
}
public function combineJsFiles($files, $tmpFile)
{
$content = '';
foreach ($files as $file) {
$content .= "/*** BEGIN FILE: $file ***/\n"
. file_get_contents($file)
. "/*** END FILE: $file ***/\n";
}
file_put_contents($tmpFile, $content);
}
public function combineCssFiles($files, $tmpFile)
{
// todo: adjust url() references in CSS files
$content = '';
foreach ($files as $file) {
$content .= "/*** BEGIN FILE: $file ***/\n"
. file_get_contents($file)
. "/*** END FILE: $file ***/\n";
}
file_put_contents($tmpFile, $content);
}
public function actionTemplate($configFile)
{
$template = <<<EOD
<?php
return array(
//
'bundles' => require('path/to/bundles.php'),
//
'extensions' => require('path/to/namespaces.php'),
//
'targets' => array(
'all' => array(
'basePath' => __DIR__,
'baseUrl' => '/test',
'js' => 'all-{ts}.js',
'css' => 'all-{ts}.css',
),
),
'assetManager' => array(
'basePath' => __DIR__,
'baseUrl' => '/test',
),
);
EOD;
file_put_contents($configFile, $template);
} }
} }
Loading…
Cancel
Save