mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	Fixes #2303: support setting basePath and baseUrl as aliases during runtime.
This commit is contained in:
		@ -66,17 +66,6 @@ use yii\helpers\FileHelper;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
class Theme extends Component
 | 
					class Theme extends Component
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @var string the root path or path alias of this theme. All resources of this theme are located
 | 
					 | 
				
			||||||
	 * under this directory. This property must be set if [[pathMap]] is not set.
 | 
					 | 
				
			||||||
	 * @see pathMap
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public $basePath;
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @var string the base URL (or path alias) for this theme. All resources of this theme are considered
 | 
					 | 
				
			||||||
	 * to be under this base URL. This property must be set. It is mainly used by [[getUrl()]].
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public $baseUrl;
 | 
					 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @var array the mapping between view directories and their corresponding themed versions.
 | 
						 * @var array the mapping between view directories and their corresponding themed versions.
 | 
				
			||||||
	 * If not set, it will be initialized as a mapping from [[Application::basePath]] to [[basePath]].
 | 
						 * If not set, it will be initialized as a mapping from [[Application::basePath]] to [[basePath]].
 | 
				
			||||||
@ -94,20 +83,54 @@ class Theme extends Component
 | 
				
			|||||||
	{
 | 
						{
 | 
				
			||||||
		parent::init();
 | 
							parent::init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if ($this->baseUrl === null) {
 | 
							if (($basePath = $this->getBasePath()) !== null) {
 | 
				
			||||||
			throw new InvalidConfigException('The "baseUrl" property must be set.');
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if (empty($this->pathMap)) {
 | 
								if (empty($this->pathMap)) {
 | 
				
			||||||
			if ($this->basePath !== null) {
 | 
									$this->pathMap = [Yii::$app->getBasePath() => [$basePath]];
 | 
				
			||||||
				$this->basePath = Yii::getAlias($this->basePath);
 | 
								}
 | 
				
			||||||
				$this->pathMap = [Yii::$app->getBasePath() => [$this->basePath]];
 | 
					 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			throw new InvalidConfigException('The "basePath" property must be set.');
 | 
								throw new InvalidConfigException('The "basePath" property must be set.');
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private $_baseUrl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
 | 
				
			||||||
 | 
						 * to be under this base URL.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public function getBaseUrl()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return $this->_baseUrl;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @param $url string the base URL or path alias for this theme. All resources of this theme are considered
 | 
				
			||||||
 | 
						 * to be under this base URL.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public function setBaseUrl($url)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							$this->_baseUrl = rtrim(Yii::getAlias($url), '/');
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private $_basePath;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return string the root path of this theme. All resources of this theme are located under this directory.
 | 
				
			||||||
 | 
						 * @see pathMap
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public function getBasePath()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return $this->_basePath;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @param string $path the root path or path alias of this theme. All resources of this theme are located
 | 
				
			||||||
 | 
						 * under this directory.
 | 
				
			||||||
 | 
						 * @see pathMap
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public function setBasePath($path)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							$this->_basePath = Yii::getAlias($path);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@ -139,9 +162,24 @@ class Theme extends Component
 | 
				
			|||||||
	 * Converts a relative URL into an absolute URL using [[baseUrl]].
 | 
						 * Converts a relative URL into an absolute URL using [[baseUrl]].
 | 
				
			||||||
	 * @param string $url the relative URL to be converted.
 | 
						 * @param string $url the relative URL to be converted.
 | 
				
			||||||
	 * @return string the absolute URL
 | 
						 * @return string the absolute URL
 | 
				
			||||||
 | 
						 * @throws InvalidConfigException if [[baseUrl]] is not set
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public function getUrl($url)
 | 
						public function getUrl($url)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		return $this->baseUrl . '/' . ltrim($url, '/');
 | 
							if (($baseUrl = $this->getBaseUrl()) !== null) {
 | 
				
			||||||
 | 
								return $baseUrl . '/' . ltrim($url, '/');
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								throw new InvalidConfigException('The "baseUrl" property must be set.');
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Converts a relative file path into an absolute one using [[basePath]].
 | 
				
			||||||
 | 
						 * @param string $path the relative file path to be converted.
 | 
				
			||||||
 | 
						 * @return string the absolute file path
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public function getPath($path)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return $this->getBasePath() . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user