|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\View; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Handles finding templates from a stack of template manifest objects. |
|
7
|
|
|
* |
|
8
|
|
|
* @package framework |
|
9
|
|
|
* @subpackage view |
|
10
|
|
|
*/ |
|
11
|
|
|
class TemplateLoader { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var TemplateLoader |
|
15
|
|
|
*/ |
|
16
|
|
|
private static $instance; |
|
17
|
|
|
|
|
18
|
|
|
protected $base; |
|
19
|
|
|
|
|
20
|
|
|
protected $sets = []; |
|
21
|
|
|
|
|
22
|
|
|
public static function instance() { |
|
23
|
|
|
return self::$instance ? self::$instance : self::$instance = new self(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function set_instance(TemplateLoader $instance) { |
|
27
|
|
|
self::$instance = $instance; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($base = null) { |
|
31
|
|
|
$this->base = $base ? $base : BASE_PATH; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function addSet($set, $manifest) { |
|
35
|
|
|
$this->sets[$set] = $manifest; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getPath($identifier) { |
|
39
|
|
|
$slashPos = strpos($identifier, '/'); |
|
40
|
|
|
|
|
41
|
|
|
// If identifier starts with "/", it's a path from root |
|
42
|
|
|
if ($slashPos === 0) { |
|
43
|
|
|
return substr($identifier, 1); |
|
44
|
|
|
} |
|
45
|
|
|
// Otherwise if there is a "/", identifier is a vendor'ed module |
|
46
|
|
|
elseif ($slashPos !== false) { |
|
47
|
|
|
$parts = explode(':', $identifier, 2); |
|
48
|
|
|
|
|
49
|
|
|
list($vendor, $module) = explode('/', $parts[0], 2); |
|
|
|
|
|
|
50
|
|
|
$theme = count($parts) > 1 ? $parts[1] : ''; |
|
51
|
|
|
|
|
52
|
|
|
$path = $module . ($theme ? '/themes/'.$theme : ''); |
|
53
|
|
|
|
|
54
|
|
|
// Right now we require $module to be a silverstripe module (in root) or theme (in themes dir) |
|
55
|
|
|
// If both exist, we prefer theme |
|
56
|
|
|
if (is_dir(THEMES_PATH . '/' .$path)) { |
|
57
|
|
|
return THEMES_DIR . '/' . $path; |
|
58
|
|
|
} |
|
59
|
|
|
else { |
|
60
|
|
|
return $path; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
// Otherwise it's a (deprecated) old-style "theme" identifier |
|
64
|
|
|
else { |
|
65
|
|
|
return THEMES_DIR.'/'.$identifier; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Attempts to find possible candidate templates from a set of template |
|
71
|
|
|
* names from modules, current theme directory and finally the application |
|
72
|
|
|
* folder. |
|
73
|
|
|
* |
|
74
|
|
|
* The template names can be passed in as plain strings, or be in the |
|
75
|
|
|
* format "type/name", where type is the type of template to search for |
|
76
|
|
|
* (e.g. Includes, Layout). |
|
77
|
|
|
* |
|
78
|
|
|
* @param string|array $templates |
|
|
|
|
|
|
79
|
|
|
* @param string $theme |
|
|
|
|
|
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function findTemplate($template, $themes = []) { |
|
84
|
|
|
|
|
85
|
|
|
if(is_array($template)) { |
|
86
|
|
|
$type = array_key_exists('type', $template) ? $template['type'] : ''; |
|
87
|
|
|
$templateList = array_key_exists('templates', $template) ? $template['templates'] : $template; |
|
88
|
|
|
} |
|
89
|
|
|
else { |
|
90
|
|
|
$type = ''; |
|
91
|
|
|
$templateList = array($template); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if(count($templateList) == 1 && substr($templateList[0], -3) == '.ss') { |
|
95
|
|
|
return $templateList[0]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
foreach($templateList as $i => $template) { |
|
99
|
|
|
$template = str_replace('\\', '/', $template); |
|
100
|
|
|
$parts = explode('/', $template); |
|
101
|
|
|
|
|
102
|
|
|
$tail = array_pop($parts); |
|
103
|
|
|
$head = implode('/', $parts); |
|
104
|
|
|
|
|
105
|
|
|
foreach($themes as $themename) { |
|
106
|
|
|
$subthemes = isset($this->sets[$themename]) ? $this->sets[$themename]->getThemes() : [$themename]; |
|
107
|
|
|
|
|
108
|
|
|
foreach($subthemes as $theme) { |
|
109
|
|
|
$themePath = $this->base . '/' . $this->getPath($theme); |
|
110
|
|
|
|
|
111
|
|
|
$path = $themePath . '/templates/' . implode('/', array_filter([$head, $type, $tail])) . '.ss'; |
|
112
|
|
|
if (file_exists($path)) return $path; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.