Commit c49222f7 by Qiang Xue

script WIP

parent 34c0794f
...@@ -241,7 +241,7 @@ class View extends Component ...@@ -241,7 +241,7 @@ class View extends Component
{ {
if (!empty($this->cacheStack)) { if (!empty($this->cacheStack)) {
$n = count($this->dynamicPlaceholders); $n = count($this->dynamicPlaceholders);
$placeholder = "<![CDATA[YDP-$n]]>"; $placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";
$this->addDynamicPlaceholder($placeholder, $statements); $this->addDynamicPlaceholder($placeholder, $statements);
return $placeholder; return $placeholder;
} else { } else {
......
...@@ -25,30 +25,9 @@ class ViewContent extends Component ...@@ -25,30 +25,9 @@ class ViewContent extends Component
const TOKEN_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>'; const TOKEN_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>';
/** /**
* @var array * @var \yii\web\AssetManager
*
* Each asset bundle should be declared with the following structure:
*
* ~~~
* array(
* 'basePath' => '...',
* 'baseUrl' => '...', // if missing, the bundle will be published to the "www/assets" folder
* 'js' => array(
* 'js/main.js',
* 'js/menu.js',
* 'js/base.js' => self::POS_HEAD,
* 'css' => array(
* 'css/main.css',
* 'css/menu.css',
* ),
* 'depends' => array(
* 'jquery',
* 'yii',
* 'yii/treeview',
* ),
* )
* ~~~
*/ */
public $assetManager;
public $assetBundles; public $assetBundles;
public $title; public $title;
public $metaTags; public $metaTags;
...@@ -62,6 +41,14 @@ class ViewContent extends Component ...@@ -62,6 +41,14 @@ class ViewContent extends Component
public $jsInBody; public $jsInBody;
public $jsFilesInBody; public $jsFilesInBody;
public function init()
{
parent::init();
if ($this->assetManager === null) {
$this->assetManager = Yii::$app->getAssetManager();
}
}
public function reset() public function reset()
{ {
$this->title = null; $this->title = null;
...@@ -104,21 +91,22 @@ class ViewContent extends Component ...@@ -104,21 +91,22 @@ class ViewContent extends Component
echo self::TOKEN_HEAD; echo self::TOKEN_HEAD;
} }
public function requireAssetBundle($name) public function registerAssetBundle($name)
{ {
if (!isset($this->assetBundles[$name])) { if (!isset($this->assetBundles[$name])) {
$bundle = Yii::$app->assets->getBundle($name); $bundle = $this->assetManager->getBundle($name);
if ($bundle !== null) { if ($bundle !== null) {
$this->assetBundles[$name] = $bundle; $this->assetBundles[$name] = false;
$bundle->registerWith($this);
$this->assetBundles[$name] = true;
} else { } else {
throw new InvalidConfigException("Unknown asset bundle: $name"); throw new InvalidConfigException("Unknown asset bundle: $name");
} }
foreach ($bundle->depends as $d) { } elseif ($this->assetBundles[$name] === false) {
$this->requireAssetBundle($d); throw new InvalidConfigException("A cyclic dependency is detected for bundle '$name'.");
}
} }
} }
public function registerMetaTag($options, $key = null) public function registerMetaTag($options, $key = null)
{ {
if ($key === null) { if ($key === null) {
...@@ -149,8 +137,10 @@ class ViewContent extends Component ...@@ -149,8 +137,10 @@ class ViewContent extends Component
$this->cssFiles[$key] = Html::cssFile($url, $options); $this->cssFiles[$key] = Html::cssFile($url, $options);
} }
public function registerJs($js, $position = self::POS_END, $options = array(), $key = null) public function registerJs($js, $options = array(), $key = null)
{ {
$position = isset($options['position']) ? $options['position'] : self::POS_END;
unset($options['position']);
$key = $key ?: $js; $key = $key ?: $js;
$html = Html::script($js, $options); $html = Html::script($js, $options);
if ($position == self::POS_END) { if ($position == self::POS_END) {
...@@ -164,8 +154,10 @@ class ViewContent extends Component ...@@ -164,8 +154,10 @@ class ViewContent extends Component
} }
} }
public function registerJsFile($url, $position = self::POS_END, $options = array(), $key = null) public function registerJsFile($url, $options = array(), $key = null)
{ {
$position = isset($options['position']) ? $options['position'] : self::POS_END;
unset($options['position']);
$key = $key ?: $url; $key = $key ?: $url;
$html = Html::jsFile($url, $options); $html = Html::jsFile($url, $options);
if ($position == self::POS_END) { if ($position == self::POS_END) {
...@@ -178,11 +170,9 @@ class ViewContent extends Component ...@@ -178,11 +170,9 @@ class ViewContent extends Component
throw new InvalidParamException("Unknown position: $position"); throw new InvalidParamException("Unknown position: $position");
} }
} }
protected function populate($content) protected function populate($content)
{ {
$this->expandAssetBundles();
return strtr($content, array( return strtr($content, array(
self::TOKEN_HEAD => $this->getHeadHtml(), self::TOKEN_HEAD => $this->getHeadHtml(),
self::TOKEN_BODY_BEGIN => $this->getBodyBeginHtml(), self::TOKEN_BODY_BEGIN => $this->getBodyBeginHtml(),
...@@ -190,11 +180,6 @@ class ViewContent extends Component ...@@ -190,11 +180,6 @@ class ViewContent extends Component
)); ));
} }
protected function expandAssetBundles()
{
}
protected function getHeadHtml() protected function getHeadHtml()
{ {
$lines = array(); $lines = array();
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use yii\base\Object;
/**
* Each asset bundle should be declared with the following structure:
*
* ~~~
* array(
* 'basePath' => '...',
* 'baseUrl' => '...', // if missing, the bundle will be published to the "www/assets" folder
* 'js' => array(
* 'js/main.js',
* 'js/menu.js',
* 'js/base.js' => self::POS_HEAD,
* 'css' => array(
* 'css/main.css',
* 'css/menu.css',
* ),
* 'depends' => array(
* 'jquery',
* 'yii',
* 'yii/treeview',
* ),
* )
* ~~~
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AssetBundle extends Object
{
public $basePath;
public $baseUrl; // if missing, the bundle will be published to the "www/assets" folder
public $js = array();
public $css = array();
public $depends = array();
/**
* @param \yii\base\ViewContent $content
*/
public function registerWith($content)
{
foreach ($this->depends as $name) {
$content->registerAssetBundle($name);
}
foreach ($this->js as $js => $options) {
if (is_array($options)) {
$content->registerJsFile($js, $options);
} else {
$content->registerJsFile($options);
}
}
foreach ($this->css as $css => $options) {
if (is_array($options)) {
$content->registerCssFile($css, $options);
} else {
$content->registerCssFile($options);
}
}
}
}
\ 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