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.
		
		
		
		
			
				
					130 lines
				
				4.0 KiB
			
		
		
			
		
	
	
					130 lines
				
				4.0 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\base;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | use Yii;
 | ||
| 
											13 years ago
										 | use yii\base\InvalidConfigException;
 | ||
| 
											13 years ago
										 | use yii\helpers\FileHelper;
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											14 years ago
										 | /**
 | ||
|  |  * Theme represents an application theme.
 | ||
|  |  *
 | ||
| 
											13 years ago
										 |  * A theme is directory consisting of view and layout files which are meant to replace their
 | ||
|  |  * non-themed counterparts.
 | ||
|  |  *
 | ||
|  |  * Theme uses [[pathMap]] to achieve the file replacement. A view or layout file will be replaced
 | ||
|  |  * with its themed version if part of its path matches one of the keys in [[pathMap]].
 | ||
|  |  * Then the matched part will be replaced with the corresponding array value.
 | ||
|  |  *
 | ||
|  |  * For example, if [[pathMap]] is `array('/www/views' => '/www/themes/basic')`,
 | ||
|  |  * then the themed version for a view file `/www/views/site/index.php` will be
 | ||
|  |  * `/www/themes/basic/site/index.php`.
 | ||
|  |  *
 | ||
| 
											13 years ago
										 |  * To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
 | ||
|  |  * component like the following:
 | ||
|  |  *
 | ||
|  |  * ~~~
 | ||
|  |  * 'view' => array(
 | ||
|  |  *     'theme' => array(
 | ||
|  |  *         'basePath' => '@wwwroot/themes/basic',
 | ||
|  |  *         'baseUrl' => '@www/themes/basic',
 | ||
|  |  *     ),
 | ||
|  |  * ),
 | ||
|  |  * ~~~
 | ||
|  |  *
 | ||
|  |  * The above configuration specifies a theme located under the "themes/basic" directory of the Web folder
 | ||
|  |  * that contains the entry script of the application. If your theme is designed to handle modules,
 | ||
|  |  * you may configure the [[pathMap]] property like described above.
 | ||
|  |  *
 | ||
| 
											13 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | class Theme extends Component
 | ||
| 
											14 years ago
										 | {
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @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.
 | ||
| 
											13 years ago
										 | 	 * @see pathMap
 | ||
|  | 	 */
 | ||
| 
											14 years ago
										 | 	public $basePath;
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @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;
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @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]].
 | ||
| 
											13 years ago
										 | 	 * This property is used by [[applyTo()]] when a view is trying to apply the theme.
 | ||
|  | 	 * Path aliases can be used when specifying directories.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $pathMap;
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * Initializes the theme.
 | ||
|  | 	 * @throws InvalidConfigException if [[basePath]] is not set.
 | ||
|  | 	 */
 | ||
| 
											14 years ago
										 | 	public function init()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 	 	parent::init();
 | ||
|  | 		if (empty($this->pathMap)) {
 | ||
|  | 			if ($this->basePath !== null) {
 | ||
| 
											13 years ago
										 | 				$this->basePath = Yii::getAlias($this->basePath);
 | ||
| 
											13 years ago
										 | 				$this->pathMap = array(Yii::$app->getBasePath() => $this->basePath);
 | ||
| 
											13 years ago
										 | 			} else {
 | ||
| 
											13 years ago
										 | 				throw new InvalidConfigException('The "basePath" property must be set.');
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		$paths = array();
 | ||
|  | 		foreach ($this->pathMap as $from => $to) {
 | ||
| 
											13 years ago
										 | 			$from = FileHelper::normalizePath(Yii::getAlias($from));
 | ||
|  | 			$to = FileHelper::normalizePath(Yii::getAlias($to));
 | ||
|  | 			$paths[$from . DIRECTORY_SEPARATOR] = $to . DIRECTORY_SEPARATOR;
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		$this->pathMap = $paths;
 | ||
| 
											13 years ago
										 | 		if ($this->baseUrl === null) {
 | ||
| 
											13 years ago
										 | 			throw new InvalidConfigException('The "baseUrl" property must be set.');
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
|  | 			$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
 | ||
|  | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Converts a file to a themed file if possible.
 | ||
|  | 	 * If there is no corresponding themed file, the original file will be returned.
 | ||
|  | 	 * @param string $path the file to be themed
 | ||
|  | 	 * @return string the themed file, or the original file if the themed version is not available.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function applyTo($path)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$path = FileHelper::normalizePath($path);
 | ||
|  | 		foreach ($this->pathMap as $from => $to) {
 | ||
|  | 			if (strpos($path, $from) === 0) {
 | ||
|  | 				$n = strlen($from);
 | ||
|  | 				$file = $to . substr($path, $n);
 | ||
|  | 				if (is_file($file)) {
 | ||
|  | 					return $file;
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		return $path;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Converts a relative URL into an absolute URL using [[baseUrl]].
 | ||
| 
											13 years ago
										 | 	 * @param string $url the relative URL to be converted.
 | ||
|  | 	 * @return string the absolute URL
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function getUrl($url)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		return $this->baseUrl . '/' . ltrim($url, '/');
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | }
 |