diff --git a/framework/base/View.php b/framework/base/View.php index 247dfd2..a794e08 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -241,7 +241,7 @@ class View extends Component { if (!empty($this->cacheStack)) { $n = count($this->dynamicPlaceholders); - $placeholder = ""; + $placeholder = ""; $this->addDynamicPlaceholder($placeholder, $statements); return $placeholder; } else { diff --git a/framework/base/ViewContent.php b/framework/base/ViewContent.php index 9bdeaef..797dba2 100644 --- a/framework/base/ViewContent.php +++ b/framework/base/ViewContent.php @@ -25,30 +25,9 @@ class ViewContent extends Component const TOKEN_BODY_END = ''; /** - * @var array - * - * 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', - * ), - * ) - * ~~~ + * @var \yii\web\AssetManager */ + public $assetManager; public $assetBundles; public $title; public $metaTags; @@ -62,6 +41,14 @@ class ViewContent extends Component public $jsInBody; public $jsFilesInBody; + public function init() + { + parent::init(); + if ($this->assetManager === null) { + $this->assetManager = Yii::$app->getAssetManager(); + } + } + public function reset() { $this->title = null; @@ -104,21 +91,22 @@ class ViewContent extends Component echo self::TOKEN_HEAD; } - public function requireAssetBundle($name) + public function registerAssetBundle($name) { if (!isset($this->assetBundles[$name])) { - $bundle = Yii::$app->assets->getBundle($name); + $bundle = $this->assetManager->getBundle($name); if ($bundle !== null) { - $this->assetBundles[$name] = $bundle; + $this->assetBundles[$name] = false; + $bundle->registerWith($this); + $this->assetBundles[$name] = true; } else { throw new InvalidConfigException("Unknown asset bundle: $name"); } - foreach ($bundle->depends as $d) { - $this->requireAssetBundle($d); - } + } elseif ($this->assetBundles[$name] === false) { + throw new InvalidConfigException("A cyclic dependency is detected for bundle '$name'."); } } - + public function registerMetaTag($options, $key = null) { if ($key === null) { @@ -149,8 +137,10 @@ class ViewContent extends Component $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; $html = Html::script($js, $options); if ($position == self::POS_END) { @@ -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; $html = Html::jsFile($url, $options); if ($position == self::POS_END) { @@ -178,11 +170,9 @@ class ViewContent extends Component throw new InvalidParamException("Unknown position: $position"); } } - protected function populate($content) { - $this->expandAssetBundles(); return strtr($content, array( self::TOKEN_HEAD => $this->getHeadHtml(), self::TOKEN_BODY_BEGIN => $this->getBodyBeginHtml(), @@ -190,11 +180,6 @@ class ViewContent extends Component )); } - protected function expandAssetBundles() - { - - } - protected function getHeadHtml() { $lines = array(); diff --git a/framework/web/AssetBundle.php b/framework/web/AssetBundle.php new file mode 100644 index 0000000..3e4541e --- /dev/null +++ b/framework/web/AssetBundle.php @@ -0,0 +1,68 @@ + '...', + * '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 + * @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 diff --git a/framework/web/AssetManager.php b/framework/web/AssetManager.php index 60f4c07..3cb1e83 100644 --- a/framework/web/AssetManager.php +++ b/framework/web/AssetManager.php @@ -1,140 +1,121 @@ * @link http://www.yiiframework.com/ - * @copyright Copyright © 2008-2011 Yii Software LLC + * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ +namespace yii\web; + +use Yii; +use yii\base\Component; +use yii\base\InvalidConfigException; +use yii\base\InvalidParamException; /** - * CAssetManager is a Web application component that manages private files (called assets) and makes them accessible by Web clients. - * - * It achieves this goal by copying assets to a Web-accessible directory - * and returns the corresponding URL for accessing them. - * - * To publish an asset, simply call {@link publish()}. - * - * The Web-accessible directory holding the published files is specified - * by {@link setBasePath basePath}, which defaults to the "assets" directory - * under the directory containing the application entry script file. - * The property {@link setBaseUrl baseUrl} refers to the URL for accessing - * the {@link setBasePath basePath}. - * - * @property string $basePath The root directory storing the published asset files. Defaults to 'WebRoot/assets'. - * @property string $baseUrl The base url that the published asset files can be accessed. - * Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'. * * @author Qiang Xue - * @version $Id$ - * @package system.web - * @since 1.0 + * @since 2.0 */ -class CAssetManager extends CApplicationComponent +class AssetManager extends Component { /** - * Default web accessible base path for storing private files + * @var array list of asset bundles. The keys are the bundle names, and the values are the configuration + * arrays for creating [[AssetBundle]] objects. Besides the bundles listed here, the asset manager + * may look for bundles declared in extensions. For more details, please refer to [[getBundle()]]. + */ + public $bundles; + /** + * @var + */ + public $bundleMap; + /** + * @return string the root directory storing the published asset files. + */ + public $basePath = '@wwwroot/assets'; + /** + * @return string the base URL through which the published asset files can be accessed. */ - const DEFAULT_BASEPATH='assets'; + public $baseUrl = '@www/assets'; /** * @var boolean whether to use symbolic link to publish asset files. Defaults to false, meaning - * asset files are copied to public folders. Using symbolic links has the benefit that the published - * assets will always be consistent with the source assets. This is especially useful during development. + * asset files are copied to [[basePath]]. Using symbolic links has the benefit that the published + * assets will always be consistent with the source assets and there is no copy operation required. + * This is especially useful during development. * * However, there are special requirements for hosting environments in order to use symbolic links. * In particular, symbolic links are supported only on Linux/Unix, and Windows Vista/2008 or greater. - * The latter requires PHP 5.3 or greater. * * Moreover, some Web servers need to be properly configured so that the linked assets are accessible * to Web users. For example, for Apache Web server, the following configuration directive should be added * for the Web folder: - *
-	 * Options FollowSymLinks
-	 * 
* - * @since 1.1.5 + * ~~~ + * Options FollowSymLinks + * ~~~ */ - public $linkAssets=false; + public $linkAssets = false; /** * @var array list of directories and files which should be excluded from the publishing process. * Defaults to exclude '.svn' and '.gitignore' files only. This option has no effect if {@link linkAssets} is enabled. * @since 1.1.6 **/ - public $excludeFiles=array('.svn','.gitignore'); + public $excludeFiles = array('.svn', '.gitignore'); /** * @var integer the permission to be set for newly generated asset files. * This value will be used by PHP chmod function. * Defaults to 0666, meaning the file is read-writable by all users. * @since 1.1.8 */ - public $newFileMode=0666; + public $newFileMode = 0666; /** * @var integer the permission to be set for newly generated asset directories. * This value will be used by PHP chmod function. * Defaults to 0777, meaning the directory can be read, written and executed by all users. * @since 1.1.8 */ - public $newDirMode=0777; - /** - * @var string base web accessible path for storing private files - */ - private $_basePath; - /** - * @var string base URL for accessing the publishing directory. - */ - private $_baseUrl; + public $newDirMode = 0777; /** * @var array published assets */ - private $_published=array(); + private $_published = array(); - /** - * @return string the root directory storing the published asset files. Defaults to 'WebRoot/assets'. - */ - public function getBasePath() + public function init() { - if($this->_basePath===null) - { - $request=\Yii::$app->getRequest(); - $this->setBasePath(dirname($request->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH); + parent::init(); + $this->basePath = Yii::getAlias($this->basePath); + if (!is_dir($this->basePath)) { + throw new InvalidConfigException("The directory does not exist: {$this->basePath}"); + } elseif (!is_writable($this->basePath)) { + throw new InvalidConfigException("The directory is not writable by the Web process: {$this->basePath}"); + } else { + $this->base = realpath($this->basePath); } - return $this->_basePath; - } - - /** - * Sets the root directory storing published asset files. - * @param string $value the root directory storing published asset files - * @throws CException if the base path is invalid - */ - public function setBasePath($value) - { - if(($basePath=realpath($value))!==false && is_dir($basePath) && is_writable($basePath)) - $this->_basePath=$basePath; - else - throw new CException(Yii::t('yii|CAssetManager.basePath "{path}" is invalid. Please make sure the directory exists and is writable by the Web server process.', - array('{path}'=>$value))); + $this->baseUrl = rtrim(Yii::getAlias($this->getBaseUrl), '/'); } /** - * @return string the base url that the published asset files can be accessed. - * Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'. + * @param string $name + * @return AssetBundle + * @throws InvalidParamException */ - public function getBaseUrl() + public function getBundle($name) { - if($this->_baseUrl===null) - { - $request=\Yii::$app->getRequest(); - $this->setBaseUrl($request->getBaseUrl().'/'.self::DEFAULT_BASEPATH); + if (!isset($this->bundles[$name])) { + $manifest = Yii::getAlias("@{$name}/assets.php", false); + if ($manifest === false) { + throw new InvalidParamException("Unable to find the asset bundle: $name"); + } + $this->bundles[$name] = require($manifest); } - return $this->_baseUrl; - } - - /** - * @param string $value the base url that the published asset files can be accessed - */ - public function setBaseUrl($value) - { - $this->_baseUrl=rtrim($value,'/'); + if (is_array($this->bundles[$name])) { + $config = $this->bundles[$name]; + if (!isset($config['class'])) { + $config['class'] = 'yii\\web\\AssetBundle'; + $this->bundles[$name] = Yii::createObject($config); + } + } + return $this->bundles[$name]; } /** @@ -173,69 +154,65 @@ class CAssetManager extends CApplicationComponent * @return string an absolute URL to the published asset * @throws CException if the asset to be published does not exist. */ - public function publish($path,$hashByName=false,$level=-1,$forceCopy=false) + public function publish($path, $hashByName = false, $level = -1, $forceCopy = false) { - if(isset($this->_published[$path])) + if (isset($this->_published[$path])) { return $this->_published[$path]; - else if(($src=realpath($path))!==false) - { - if(is_file($src)) - { - $dir=$this->hash($hashByName ? basename($src) : dirname($src).filemtime($src)); - $fileName=basename($src); - $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir; - $dstFile=$dstDir.DIRECTORY_SEPARATOR.$fileName; + } else { + if (($src = realpath($path)) !== false) { + if (is_file($src)) { + $dir = $this->hash($hashByName ? basename($src) : dirname($src) . filemtime($src)); + $fileName = basename($src); + $dstDir = $this->getBasePath() . DIRECTORY_SEPARATOR . $dir; + $dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName; - if($this->linkAssets) - { - if(!is_file($dstFile)) - { - if(!is_dir($dstDir)) - { - mkdir($dstDir); - @chmod($dstDir, $this->newDirMode); + if ($this->linkAssets) { + if (!is_file($dstFile)) { + if (!is_dir($dstDir)) { + mkdir($dstDir); + @chmod($dstDir, $this->newDirMode); + } + symlink($src, $dstFile); + } + } else { + if (@filemtime($dstFile) < @filemtime($src)) { + if (!is_dir($dstDir)) { + mkdir($dstDir); + @chmod($dstDir, $this->newDirMode); + } + copy($src, $dstFile); + @chmod($dstFile, $this->newFileMode); } - symlink($src,$dstFile); - } - } - else if(@filemtime($dstFile)<@filemtime($src)) - { - if(!is_dir($dstDir)) - { - mkdir($dstDir); - @chmod($dstDir, $this->newDirMode); } - copy($src,$dstFile); - @chmod($dstFile, $this->newFileMode); - } - return $this->_published[$path]=$this->getBaseUrl()."/$dir/$fileName"; - } - else if(is_dir($src)) - { - $dir=$this->hash($hashByName ? basename($src) : $src.filemtime($src)); - $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir; + return $this->_published[$path] = $this->getBaseUrl() . "/$dir/$fileName"; + } else { + if (is_dir($src)) { + $dir = $this->hash($hashByName ? basename($src) : $src . filemtime($src)); + $dstDir = $this->getBasePath() . DIRECTORY_SEPARATOR . $dir; - if($this->linkAssets) - { - if(!is_dir($dstDir)) - symlink($src,$dstDir); - } - else if(!is_dir($dstDir) || $forceCopy) - { - CFileHelper::copyDirectory($src,$dstDir,array( - 'exclude'=>$this->excludeFiles, - 'level'=>$level, - 'newDirMode'=>$this->newDirMode, - 'newFileMode'=>$this->newFileMode, - )); - } + if ($this->linkAssets) { + if (!is_dir($dstDir)) { + symlink($src, $dstDir); + } + } else { + if (!is_dir($dstDir) || $forceCopy) { + CFileHelper::copyDirectory($src, $dstDir, array( + 'exclude' => $this->excludeFiles, + 'level' => $level, + 'newDirMode' => $this->newDirMode, + 'newFileMode' => $this->newFileMode, + )); + } + } - return $this->_published[$path]=$this->getBaseUrl().'/'.$dir; + return $this->_published[$path] = $this->getBaseUrl() . '/' . $dir; + } + } } } throw new CException(Yii::t('yii|The asset "{asset}" to be published does not exist.', - array('{asset}'=>$path))); + array('{asset}' => $path))); } /** @@ -249,18 +226,18 @@ class CAssetManager extends CApplicationComponent * different extensions. * @return string the published file path. False if the file or directory does not exist */ - public function getPublishedPath($path,$hashByName=false) + public function getPublishedPath($path, $hashByName = false) { - if(($path=realpath($path))!==false) - { - $base=$this->getBasePath().DIRECTORY_SEPARATOR; - if(is_file($path)) - return $base . $this->hash($hashByName ? basename($path) : dirname($path).filemtime($path)) . DIRECTORY_SEPARATOR . basename($path); - else - return $base . $this->hash($hashByName ? basename($path) : $path.filemtime($path)); - } - else + if (($path = realpath($path)) !== false) { + $base = $this->getBasePath() . DIRECTORY_SEPARATOR; + if (is_file($path)) { + return $base . $this->hash($hashByName ? basename($path) : dirname($path) . filemtime($path)) . DIRECTORY_SEPARATOR . basename($path); + } else { + return $base . $this->hash($hashByName ? basename($path) : $path . filemtime($path)); + } + } else { return false; + } } /** @@ -274,19 +251,20 @@ class CAssetManager extends CApplicationComponent * different extensions. * @return string the published URL for the file or directory. False if the file or directory does not exist. */ - public function getPublishedUrl($path,$hashByName=false) + public function getPublishedUrl($path, $hashByName = false) { - if(isset($this->_published[$path])) + if (isset($this->_published[$path])) { return $this->_published[$path]; - if(($path=realpath($path))!==false) - { - if(is_file($path)) - return $this->getBaseUrl().'/'.$this->hash($hashByName ? basename($path) : dirname($path).filemtime($path)).'/'.basename($path); - else - return $this->getBaseUrl().'/'.$this->hash($hashByName ? basename($path) : $path.filemtime($path)); } - else + if (($path = realpath($path)) !== false) { + if (is_file($path)) { + return $this->getBaseUrl() . '/' . $this->hash($hashByName ? basename($path) : dirname($path) . filemtime($path)) . '/' . basename($path); + } else { + return $this->getBaseUrl() . '/' . $this->hash($hashByName ? basename($path) : $path . filemtime($path)); + } + } else { return false; + } } /** @@ -297,6 +275,6 @@ class CAssetManager extends CApplicationComponent */ protected function hash($path) { - return sprintf('%x',crc32($path.Yii::getVersion())); + return sprintf('%x', crc32($path . Yii::getVersion())); } }