Theme.php 2.4 KB
Newer Older
R
x  
root 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */
namespace fecshop\services\page;
use Yii;
use yii\base\InvalidValueException;
use yii\base\InvalidConfigException;
use fec\helpers\CSession;
use fec\helpers\CUrl;
use fecshop\services\ChildService;
/**
 * Breadcrumbs services
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class Theme extends ChildService
{
	/**
	 * user current theme dir. Highest priority
	 */
R
add  
root 已提交
26
	public $localThemeDir;
R
x  
root 已提交
27 28 29 30 31 32 33 34 35 36
	/**
	 * $thirdThemeDir | Array
	 * user current theme dir.Second priority.
	 * array[0] priority is higher than array[1],
	 */
	public $thirdThemeDir;
	/**
	 * fecshop theme dir. lower priority
	 */
	public $fecshopThemeDir ;
R
add  
root 已提交
37 38 39 40 41 42 43
	/**
	 * current layout file path.
	 */
	public $layoutFile;
	/**
	 * array that contains mutil theme dir.
	 */
R
x  
root 已提交
44 45 46 47 48 49
	protected $_themeDirArr;
	
	
	public function getThemeDirArr(){
		if(!$this->_themeDirArr){
			$arr = [];
R
add  
root 已提交
50
			$arr[] = Yii::getAlias($this->localThemeDir);
R
x  
root 已提交
51 52 53 54 55 56 57 58 59 60 61 62
			$thirdThemeDirArr = $this->thirdThemeDir;
			if(!empty($thirdThemeDirArr) && is_array($thirdThemeDirArr)){
				foreach($thirdThemeDirArr as $theme){
					$arr[] = Yii::getAlias($theme);
				}
			}
			$arr[] = Yii::getAlias($this->fecshopThemeDir);
			$this->_themeDirArr = $arr;
		}
		return $this->_themeDirArr;
	}
	
R
add  
root 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	/**
	 * find theme file by mutil theme ,if not find view file  and $throwError=true, it will throw InvalidValueException.
	 */ 
	public function getViewFile($view,$throwError=true){
		$view = trim($view);
		if(substr($view,0,1) == '@'){
			return Yii::getAlias($view);
		}
		$relativeFile = '';
		$module = Yii::$app->controller->module;
		if($module && $module->id){
			$relativeFile = $module->id.'/';
		}
		$relativeFile .= Yii::$app->controller->id.'/'.$view.'.php';
		$absoluteDir = Yii::$app->page->theme->getThemeDirArr();
		foreach($absoluteDir as $dir){
			if($dir){
				$file = $dir.'/'.$relativeFile;
				if(file_exists($file)){
					return $file;	
				}
			}
		}
		/* not find view file */
		if($throwError){
			$notExistFile = [];
			foreach($absoluteDir as $dir){
				if($dir){
					$file = $dir.'/'.$relativeFile;
					$notExistFile[] = $file;
				}
			}
			throw new InvalidValueException('view file is not exist in'.implode(',',$notExistFile));
		}else{
			return false;
		}
	}
R
x  
root 已提交
100 101 102 103 104 105 106 107 108 109
	
	
	
	
	
	
	
	
	
}