mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	script WIP
This commit is contained in:
		@ -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
 | 
				
			|||||||
	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
 | 
				
			|||||||
	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,18 +91,19 @@ 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'.");
 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -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
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	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) {
 | 
				
			||||||
@ -179,10 +171,8 @@ class ViewContent extends Component
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
	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
 | 
				
			|||||||
		));
 | 
							));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	protected function expandAssetBundles()
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	protected function getHeadHtml()
 | 
						protected function getHeadHtml()
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		$lines = array();
 | 
							$lines = array();
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										68
									
								
								framework/web/AssetBundle.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								framework/web/AssetBundle.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,68 @@
 | 
				
			|||||||
 | 
					<?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);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,140 +1,121 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @author Qiang Xue <qiang.xue@gmail.com>
 | 
					 | 
				
			||||||
 * @link http://www.yiiframework.com/
 | 
					 * @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/
 | 
					 * @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 <qiang.xue@gmail.com>
 | 
					 * @author Qiang Xue <qiang.xue@gmail.com>
 | 
				
			||||||
 * @version $Id$
 | 
					 * @since 2.0
 | 
				
			||||||
 * @package system.web
 | 
					 | 
				
			||||||
 * @since 1.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()]].
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	const DEFAULT_BASEPATH='assets';
 | 
						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.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public $baseUrl = '@www/assets';
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @var boolean whether to use symbolic link to publish asset files. Defaults to false, meaning
 | 
						 * @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
 | 
						 * asset files are copied to [[basePath]]. Using symbolic links has the benefit that the published
 | 
				
			||||||
	 * assets will always be consistent with the source assets. This is especially useful during development.
 | 
						 * 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.
 | 
						 * 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.
 | 
						 * 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
 | 
						 * 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
 | 
						 * to Web users. For example, for Apache Web server, the following configuration directive should be added
 | 
				
			||||||
	 * for the Web folder:
 | 
						 * for the Web folder:
 | 
				
			||||||
	 * <pre>
 | 
					 | 
				
			||||||
	 * Options FollowSymLinks
 | 
					 | 
				
			||||||
	 * </pre>
 | 
					 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @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.
 | 
						 * @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.
 | 
						 * Defaults to exclude '.svn' and '.gitignore' files only. This option has no effect if {@link linkAssets} is enabled.
 | 
				
			||||||
	 * @since 1.1.6
 | 
						 * @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.
 | 
						 * @var integer the permission to be set for newly generated asset files.
 | 
				
			||||||
	 * This value will be used by PHP chmod function.
 | 
						 * This value will be used by PHP chmod function.
 | 
				
			||||||
	 * Defaults to 0666, meaning the file is read-writable by all users.
 | 
						 * Defaults to 0666, meaning the file is read-writable by all users.
 | 
				
			||||||
	 * @since 1.1.8
 | 
						 * @since 1.1.8
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public $newFileMode=0666;
 | 
						public $newFileMode = 0666;
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @var integer the permission to be set for newly generated asset directories.
 | 
						 * @var integer the permission to be set for newly generated asset directories.
 | 
				
			||||||
	 * This value will be used by PHP chmod function.
 | 
						 * This value will be used by PHP chmod function.
 | 
				
			||||||
	 * Defaults to 0777, meaning the directory can be read, written and executed by all users.
 | 
						 * Defaults to 0777, meaning the directory can be read, written and executed by all users.
 | 
				
			||||||
	 * @since 1.1.8
 | 
						 * @since 1.1.8
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public $newDirMode=0777;
 | 
						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;
 | 
					 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @var array published assets
 | 
						 * @var array published assets
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	private $_published=array();
 | 
						private $_published = array();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						public function init()
 | 
				
			||||||
	 * @return string the root directory storing the published asset files. Defaults to 'WebRoot/assets'.
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public function getBasePath()
 | 
					 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if($this->_basePath===null)
 | 
							parent::init();
 | 
				
			||||||
		{
 | 
							$this->basePath = Yii::getAlias($this->basePath);
 | 
				
			||||||
			$request=\Yii::$app->getRequest();
 | 
							if (!is_dir($this->basePath)) {
 | 
				
			||||||
			$this->setBasePath(dirname($request->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_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;
 | 
							$this->baseUrl = rtrim(Yii::getAlias($this->getBaseUrl), '/');
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Sets the root directory storing published asset files.
 | 
						 * @param string $name
 | 
				
			||||||
	 * @param string $value the root directory storing published asset files
 | 
						 * @return AssetBundle
 | 
				
			||||||
	 * @throws CException if the base path is invalid
 | 
						 * @throws InvalidParamException
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public function setBasePath($value)
 | 
						public function getBundle($name)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if(($basePath=realpath($value))!==false && is_dir($basePath) && is_writable($basePath))
 | 
							if (!isset($this->bundles[$name])) {
 | 
				
			||||||
			$this->_basePath=$basePath;
 | 
								$manifest = Yii::getAlias("@{$name}/assets.php", false);
 | 
				
			||||||
		else
 | 
								if ($manifest === false) {
 | 
				
			||||||
			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.',
 | 
									throw new InvalidParamException("Unable to find the asset bundle: $name");
 | 
				
			||||||
				array('{path}'=>$value)));
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								$this->bundles[$name] = require($manifest);
 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @return string the base url that the published asset files can be accessed.
 | 
					 | 
				
			||||||
	 * Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'.
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public function getBaseUrl()
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		if($this->_baseUrl===null)
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			$request=\Yii::$app->getRequest();
 | 
					 | 
				
			||||||
			$this->setBaseUrl($request->getBaseUrl().'/'.self::DEFAULT_BASEPATH);
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return $this->_baseUrl;
 | 
							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];
 | 
				
			||||||
	 * @param string $value the base url that the published asset files can be accessed
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public function setBaseUrl($value)
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		$this->_baseUrl=rtrim($value,'/');
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@ -173,69 +154,65 @@ class CAssetManager extends CApplicationComponent
 | 
				
			|||||||
	 * @return string an absolute URL to the published asset
 | 
						 * @return string an absolute URL to the published asset
 | 
				
			||||||
	 * @throws CException if the asset to be published does not exist.
 | 
						 * @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];
 | 
								return $this->_published[$path];
 | 
				
			||||||
		else if(($src=realpath($path))!==false)
 | 
							} else {
 | 
				
			||||||
		{
 | 
								if (($src = realpath($path)) !== false) {
 | 
				
			||||||
			if(is_file($src))
 | 
									if (is_file($src)) {
 | 
				
			||||||
			{
 | 
										$dir = $this->hash($hashByName ? basename($src) : dirname($src) . filemtime($src));
 | 
				
			||||||
				$dir=$this->hash($hashByName ? basename($src) : dirname($src).filemtime($src));
 | 
										$fileName = basename($src);
 | 
				
			||||||
				$fileName=basename($src);
 | 
										$dstDir = $this->getBasePath() . DIRECTORY_SEPARATOR . $dir;
 | 
				
			||||||
				$dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;
 | 
										$dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName;
 | 
				
			||||||
				$dstFile=$dstDir.DIRECTORY_SEPARATOR.$fileName;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if($this->linkAssets)
 | 
										if ($this->linkAssets) {
 | 
				
			||||||
				{
 | 
											if (!is_file($dstFile)) {
 | 
				
			||||||
					if(!is_file($dstFile))
 | 
												if (!is_dir($dstDir)) {
 | 
				
			||||||
					{
 | 
					 | 
				
			||||||
						if(!is_dir($dstDir))
 | 
					 | 
				
			||||||
						{
 | 
					 | 
				
			||||||
								mkdir($dstDir);
 | 
													mkdir($dstDir);
 | 
				
			||||||
								@chmod($dstDir, $this->newDirMode);
 | 
													@chmod($dstDir, $this->newDirMode);
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
						symlink($src,$dstFile);
 | 
												symlink($src, $dstFile);
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
				}
 | 
										} else {
 | 
				
			||||||
				else if(@filemtime($dstFile)<@filemtime($src))
 | 
											if (@filemtime($dstFile) < @filemtime($src)) {
 | 
				
			||||||
				{
 | 
												if (!is_dir($dstDir)) {
 | 
				
			||||||
					if(!is_dir($dstDir))
 | 
					 | 
				
			||||||
					{
 | 
					 | 
				
			||||||
								mkdir($dstDir);
 | 
													mkdir($dstDir);
 | 
				
			||||||
								@chmod($dstDir, $this->newDirMode);
 | 
													@chmod($dstDir, $this->newDirMode);
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
					copy($src,$dstFile);
 | 
												copy($src, $dstFile);
 | 
				
			||||||
							@chmod($dstFile, $this->newFileMode);
 | 
												@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;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if($this->linkAssets)
 | 
										return $this->_published[$path] = $this->getBaseUrl() . "/$dir/$fileName";
 | 
				
			||||||
				{
 | 
									} else {
 | 
				
			||||||
					if(!is_dir($dstDir))
 | 
										if (is_dir($src)) {
 | 
				
			||||||
						symlink($src,$dstDir);
 | 
											$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)
 | 
											} else {
 | 
				
			||||||
				{
 | 
												if (!is_dir($dstDir) || $forceCopy) {
 | 
				
			||||||
					CFileHelper::copyDirectory($src,$dstDir,array(
 | 
													CFileHelper::copyDirectory($src, $dstDir, array(
 | 
				
			||||||
						'exclude'=>$this->excludeFiles,
 | 
														'exclude' => $this->excludeFiles,
 | 
				
			||||||
						'level'=>$level,
 | 
														'level' => $level,
 | 
				
			||||||
						'newDirMode'=>$this->newDirMode,
 | 
														'newDirMode' => $this->newDirMode,
 | 
				
			||||||
						'newFileMode'=>$this->newFileMode,
 | 
														'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.',
 | 
							throw new CException(Yii::t('yii|The asset "{asset}" to be published does not exist.',
 | 
				
			||||||
			array('{asset}'=>$path)));
 | 
								array('{asset}' => $path)));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@ -249,19 +226,19 @@ class CAssetManager extends CApplicationComponent
 | 
				
			|||||||
	 * different extensions.
 | 
						 * different extensions.
 | 
				
			||||||
	 * @return string the published file path. False if the file or directory does not exist
 | 
						 * @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)
 | 
							if (($path = realpath($path)) !== false) {
 | 
				
			||||||
		{
 | 
								$base = $this->getBasePath() . DIRECTORY_SEPARATOR;
 | 
				
			||||||
			$base=$this->getBasePath().DIRECTORY_SEPARATOR;
 | 
								if (is_file($path)) {
 | 
				
			||||||
			if(is_file($path))
 | 
									return $base . $this->hash($hashByName ? basename($path) : dirname($path) . filemtime($path)) . DIRECTORY_SEPARATOR . basename($path);
 | 
				
			||||||
				return $base . $this->hash($hashByName ? basename($path) : dirname($path).filemtime($path)) . DIRECTORY_SEPARATOR . basename($path);
 | 
								} else {
 | 
				
			||||||
			else
 | 
									return $base . $this->hash($hashByName ? basename($path) : $path . filemtime($path));
 | 
				
			||||||
				return $base . $this->hash($hashByName ? basename($path) : $path.filemtime($path));
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		else
 | 
							} else {
 | 
				
			||||||
			return false;
 | 
								return false;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Returns the URL of a published file path.
 | 
						 * Returns the URL of a published file path.
 | 
				
			||||||
@ -274,20 +251,21 @@ class CAssetManager extends CApplicationComponent
 | 
				
			|||||||
	 * different extensions.
 | 
						 * different extensions.
 | 
				
			||||||
	 * @return string the published URL for the file or directory. False if the file or directory does not exist.
 | 
						 * @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];
 | 
								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;
 | 
								return false;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Generate a CRC32 hash for the directory path. Collisions are higher
 | 
						 * Generate a CRC32 hash for the directory path. Collisions are higher
 | 
				
			||||||
@ -297,6 +275,6 @@ class CAssetManager extends CApplicationComponent
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	protected function hash($path)
 | 
						protected function hash($path)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		return sprintf('%x',crc32($path.Yii::getVersion()));
 | 
							return sprintf('%x', crc32($path . Yii::getVersion()));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user