Theme.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\page;
  10. use fecshop\services\Service;
  11. use Yii;
  12. use yii\base\InvalidValueException;
  13. /**
  14. * Page Theme services. 关于fecshop模板的更多知识可以参阅文档:http://www.fecshop.com/doc/fecshop-guide/develop/cn-1.0/guide-fecshop-theme.html
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class Theme extends Service
  19. {
  20. /**
  21. * 用户本地的模板路径,优先级最高
  22. */
  23. public $localThemeDir;
  24. /**
  25. * $thirdThemeDir | Array
  26. * 第三方模板的模板路径,数组格式,允许多个,数组第一个模板路径的优先级高于第二个模板路径。
  27. * array[0] priority is higher than array[1],.
  28. */
  29. public $thirdThemeDir;
  30. /**
  31. * fecshop的模板路径,模板文件最全,但是优先级最低,因此本地模板路径和第三方模板路径的文件会覆盖fecshop的文件,来实现模板文件的重写。
  32. */
  33. public $fecshopThemeDir;
  34. /**
  35. * current layout file path.
  36. */
  37. public $layoutFile;
  38. public $viewFileConfig;
  39. /**
  40. * array that contains mutil theme dir.
  41. */
  42. protected $_themeDirArr;
  43. /**
  44. * @return array ,根据模板路径的优先级,以及得到各个模板路径,组合成数组
  45. * 数组前面的模板路径优先级最高
  46. */
  47. protected function actionGetThemeDirArr()
  48. {
  49. if (!$this->_themeDirArr || empty($this->_themeDirArr)) {
  50. $arr = [];
  51. if ($localThemeDir = Yii::getAlias($this->localThemeDir)) {
  52. $arr[] = $localThemeDir;
  53. }
  54. $thirdThemeDirArr = $this->thirdThemeDir;
  55. if (!empty($thirdThemeDirArr) && is_array($thirdThemeDirArr)) {
  56. foreach ($thirdThemeDirArr as $theme) {
  57. $arr[] = Yii::getAlias($theme);
  58. }
  59. }
  60. $arr[] = Yii::getAlias($this->fecshopThemeDir);
  61. $this->_themeDirArr = $arr;
  62. }
  63. return $this->_themeDirArr;
  64. }
  65. /**
  66. * @param $view | String ,view路径的字符串。
  67. * @param $throwError | boolean,view文件找不到的时候是否抛出异常。
  68. * 根据模板路径的优先级,依次查找view文件,找到后,返回view文件的绝对路径。
  69. */
  70. protected function actionGetViewFile($view, $throwError = true)
  71. {
  72. $view = trim($view);
  73. if (substr($view, 0, 1) == '@') {
  74. return Yii::getAlias($view);
  75. }
  76. $relativeFile = '';
  77. $module = Yii::$app->controller->module;
  78. if ($module && $module->id) {
  79. $relativeFile = $module->id.'/';
  80. }
  81. $routerPath = $relativeFile.Yii::$app->controller->id.'/'.$view;
  82. // 从配置中查看是否存在指定的view路径。
  83. if (isset($this->viewFileConfig[$routerPath])) {
  84. $relativeFile = $this->viewFileConfig[$routerPath];
  85. // 如果view是以@开头,则说明是绝对路径,直接返回
  86. if (substr($relativeFile, 0, 1) == '@') {
  87. return Yii::getAlias($relativeFile);
  88. }
  89. } else {
  90. $relativeFile .= Yii::$app->controller->id.'/'.$view.'.php';
  91. }
  92. $absoluteDir = Yii::$service->page->theme->getThemeDirArr();
  93. foreach ($absoluteDir as $dir) {
  94. if ($dir) {
  95. $file = $dir.'/'.$relativeFile;
  96. if (file_exists($file)) {
  97. return $file;
  98. }
  99. }
  100. }
  101. // not find view file
  102. if ($throwError) {
  103. $notExistFile = [];
  104. foreach ($absoluteDir as $dir) {
  105. if ($dir) {
  106. $file = $dir.'/'.$relativeFile;
  107. $notExistFile[] = $file;
  108. }
  109. }
  110. throw new InvalidValueException('view file is not exist in'.implode(',', $notExistFile));
  111. } else {
  112. return false;
  113. }
  114. }
  115. /**
  116. * @param $dir | string 设置本地模板路径
  117. */
  118. protected function actionSetLocalThemeDir($dir)
  119. {
  120. $this->localThemeDir = $dir;
  121. }
  122. /**
  123. * @param $dir | string 设置第三方模板路径
  124. */
  125. protected function actionSetThirdThemeDir($dir)
  126. {
  127. $this->thirdThemeDir = $dir;
  128. }
  129. }