Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

176 lines
4.3 KiB

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\console\controllers;
use Yii;
use yii\console\Exception;
use yii\console\Controller;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ScriptController extends Controller
{
public $defaultAction = 'compress';
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)
{
}
}