1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\View; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Handles finding templates from a stack of template manifest objects. |
7
|
|
|
*/ |
8
|
|
|
class ThemeResourceLoader { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var ThemeResourceLoader |
12
|
|
|
*/ |
13
|
|
|
private static $instance; |
14
|
|
|
|
15
|
|
|
protected $base; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* List of template "sets" that contain a test manifest, and have an alias. |
19
|
|
|
* E.g. '$default' |
20
|
|
|
* |
21
|
|
|
* @var ThemeList[] |
22
|
|
|
*/ |
23
|
|
|
protected $sets = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return ThemeResourceLoader |
27
|
|
|
*/ |
28
|
|
|
public static function instance() { |
29
|
|
|
return self::$instance ? self::$instance : self::$instance = new self(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set instance |
34
|
|
|
* |
35
|
|
|
* @param ThemeResourceLoader $instance |
36
|
|
|
*/ |
37
|
|
|
public static function set_instance(ThemeResourceLoader $instance) { |
38
|
|
|
self::$instance = $instance; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function __construct($base = null) { |
42
|
|
|
$this->base = $base ? $base : BASE_PATH; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add a new theme manifest for a given identifier. E.g. '$default' |
47
|
|
|
* |
48
|
|
|
* @param string $set |
49
|
|
|
* @param ThemeList $manifest |
50
|
|
|
*/ |
51
|
|
|
public function addSet($set, ThemeList $manifest) { |
52
|
|
|
$this->sets[$set] = $manifest; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get a named theme set |
57
|
|
|
* |
58
|
|
|
* @param string $set |
59
|
|
|
* @return ThemeList |
60
|
|
|
*/ |
61
|
|
|
public function getSet($set) { |
62
|
|
|
if(isset($this->sets[$set])) { |
63
|
|
|
return $this->sets[$set]; |
64
|
|
|
} |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Given a theme identifier, determine the path from the root directory |
70
|
|
|
* |
71
|
|
|
* The mapping from $identifier to path follows these rules: |
72
|
|
|
* - A simple theme name ('mytheme') which maps to the standard themes dir (/themes/mytheme) |
73
|
|
|
* - A theme path with a leading slash ('/mymodule/themes/mytheme') which maps directly to that path. |
74
|
|
|
* - or a vendored theme path. (vendor/mymodule:mytheme) which maps to the nested 'theme' within |
75
|
|
|
* that module. ('/mymodule/themes/mytheme'). |
76
|
|
|
* - A vendored module with no nested theme (vendor/mymodule) which maps to the root directory |
77
|
|
|
* of that module. ('/mymodule'). |
78
|
|
|
* |
79
|
|
|
* @param string $identifier Theme identifier. |
80
|
|
|
* @return string Path from root, not including leading or trailing forward slash. E.g. themes/mytheme |
81
|
|
|
*/ |
82
|
|
|
public function getPath($identifier) { |
83
|
|
|
$slashPos = strpos($identifier, '/'); |
84
|
|
|
|
85
|
|
|
// If identifier starts with "/", it's a path from root |
86
|
|
|
if ($slashPos === 0) { |
87
|
|
|
return substr($identifier, 1); |
88
|
|
|
} |
89
|
|
|
// Otherwise if there is a "/", identifier is a vendor'ed module |
90
|
|
|
elseif ($slashPos !== false) { |
91
|
|
|
// Extract from <vendor>/<module>:<theme> format. |
92
|
|
|
// <vendor> is optional, and if <theme> is omitted it defaults to the module root dir. |
93
|
|
|
// If <theme> is included, this is the name of the directory under moduleroot/themes/ |
94
|
|
|
// which contains the theme. |
95
|
|
|
// <module> is always the name of the install directory, not necessarily the composer name. |
96
|
|
|
$parts = explode(':', $identifier, 2); |
97
|
|
|
|
98
|
|
|
list($vendor, $module) = explode('/', $parts[0], 2); |
|
|
|
|
99
|
|
|
$theme = count($parts) > 1 ? $parts[1] : ''; |
100
|
|
|
|
101
|
|
|
$path = $module . ($theme ? '/themes/'.$theme : ''); |
102
|
|
|
|
103
|
|
|
// Right now we require $module to be a silverstripe module (in root) or theme (in themes dir) |
104
|
|
|
// If both exist, we prefer theme |
105
|
|
|
if (is_dir(THEMES_PATH . '/' .$path)) { |
106
|
|
|
return THEMES_DIR . '/' . $path; |
107
|
|
|
} |
108
|
|
|
else { |
109
|
|
|
return $path; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
// Otherwise it's a (deprecated) old-style "theme" identifier |
113
|
|
|
else { |
114
|
|
|
return THEMES_DIR.'/'.$identifier; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Attempts to find possible candidate templates from a set of template |
120
|
|
|
* names from modules, current theme directory and finally the application |
121
|
|
|
* folder. |
122
|
|
|
* |
123
|
|
|
* The template names can be passed in as plain strings, or be in the |
124
|
|
|
* format "type/name", where type is the type of template to search for |
125
|
|
|
* (e.g. Includes, Layout). |
126
|
|
|
* |
127
|
|
|
* @param string|array $template Template name, or template spec in array format with the keys |
128
|
|
|
* 'type' (type string) and 'templates' (template hierarchy in order of precedence). |
129
|
|
|
* If 'templates' is ommitted then any other item in the array will be treated as the template |
130
|
|
|
* list, or list of templates each in the array spec given. |
131
|
|
|
* Templates with an .ss extension will be treated as file paths, and will bypass |
132
|
|
|
* theme-coupled resolution. |
133
|
|
|
* @param array $themes List of themes to use to resolve themes. In most cases |
134
|
|
|
* you should pass in {@see SSViewer::get_themes()} |
135
|
|
|
* @return string Absolute path to resolved template file, or null if not resolved. |
136
|
|
|
* File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss |
137
|
|
|
* Note that type (e.g. 'Layout') is not the root level directory under 'templates'. |
138
|
|
|
*/ |
139
|
|
|
public function findTemplate($template, $themes) { |
140
|
|
|
$type = ''; |
141
|
|
|
if(is_array($template)) { |
142
|
|
|
// Check if templates has type specified |
143
|
|
|
if (array_key_exists('type', $template)) { |
144
|
|
|
$type = $template['type']; |
145
|
|
|
unset($template['type']); |
146
|
|
|
} |
147
|
|
|
// Templates are either nested in 'templates' or just the rest of the list |
148
|
|
|
$templateList = array_key_exists('templates', $template) ? $template['templates'] : $template; |
149
|
|
|
} else { |
150
|
|
|
$templateList = array($template); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
foreach($templateList as $i => $template) { |
154
|
|
|
// Check if passed list of templates in array format |
155
|
|
|
if (is_array($template)) { |
156
|
|
|
$path = $this->findTemplate($template, $themes); |
157
|
|
|
if ($path) { |
158
|
|
|
return $path; |
159
|
|
|
} |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// If we have an .ss extension, this is a path, not a template name. We should |
164
|
|
|
// pass in templates without extensions in order for template manifest to find |
165
|
|
|
// files dynamically. |
166
|
|
|
if(substr($template, -3) == '.ss' && file_exists($template)) { |
167
|
|
|
return $template; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Check string template identifier |
171
|
|
|
$template = str_replace('\\', '/', $template); |
172
|
|
|
$parts = explode('/', $template); |
173
|
|
|
|
174
|
|
|
$tail = array_pop($parts); |
175
|
|
|
$head = implode('/', $parts); |
176
|
|
|
|
177
|
|
|
$themePaths = $this->getThemePaths($themes); |
178
|
|
|
foreach($themePaths as $themePath) { |
179
|
|
|
// Join path |
180
|
|
|
$pathParts = [ $this->base, $themePath, 'templates', $head, $type, $tail ]; |
181
|
|
|
$path = implode('/', array_filter($pathParts)) . '.ss'; |
182
|
|
|
if (file_exists($path)) { |
183
|
|
|
return $path; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// No template found |
189
|
|
|
return null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Resolve themed CSS path |
194
|
|
|
* |
195
|
|
|
* @param string $name Name of CSS file without extension |
196
|
|
|
* @param array $themes List of themes |
197
|
|
|
* @return string Path to resolved CSS file (relative to base dir) |
198
|
|
|
*/ |
199
|
|
View Code Duplication |
public function findThemedCSS($name, $themes) { |
|
|
|
|
200
|
|
|
if(substr($name, -4) !== '.css') $name .= '.css'; |
201
|
|
|
|
202
|
|
|
$filename = $this->findThemedResource("css/$name", $themes); |
203
|
|
|
if($filename === null) { |
204
|
|
|
$filename = $this->findThemedResource($name, $themes); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $filename; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Resolve themed javascript path |
212
|
|
|
* |
213
|
|
|
* A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
214
|
|
|
* and it that doesn't exist and the module parameter is set then a javascript file with that name in |
215
|
|
|
* the module is used. |
216
|
|
|
* |
217
|
|
|
* @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
218
|
|
|
* @param array $themes List of themes |
219
|
|
|
* @return string Path to resolved javascript file (relative to base dir) |
220
|
|
|
*/ |
221
|
|
View Code Duplication |
public function findThemedJavascript($name, $themes) { |
|
|
|
|
222
|
|
|
if(substr($name, -3) !== '.js') $name .= '.js'; |
223
|
|
|
|
224
|
|
|
$filename = $this->findThemedResource("javascript/$name", $themes); |
225
|
|
|
if($filename === null) { |
226
|
|
|
$filename = $this->findThemedResource($name, $themes); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $filename; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Resolve a themed resource |
234
|
|
|
* |
235
|
|
|
* A themed resource and be any file that resides in a theme folder. |
236
|
|
|
* |
237
|
|
|
* @param string $resource A file path relative to the root folder of a theme |
238
|
|
|
* @param array $themes An order listed of themes to search |
239
|
|
|
*/ |
240
|
|
|
public function findThemedResource($resource, $themes) |
241
|
|
|
{ |
242
|
|
|
if($resource[0] !== '/') { |
243
|
|
|
$resource = '/' . $resource; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$paths = $this->getThemePaths($themes); |
247
|
|
|
|
248
|
|
|
foreach ($paths as $themePath) { |
249
|
|
|
$abspath = $this->base . '/' . $themePath; |
250
|
|
|
if (file_exists($abspath . $resource)) { |
251
|
|
|
return $themePath . $resource; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// Resource exists in no context |
256
|
|
|
return null; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Resolve all themes to the list of root folders relative to site root |
261
|
|
|
* |
262
|
|
|
* @param array $themes List of themes to resolve. Supports named theme sets. |
263
|
|
|
* @return array List of root-relative folders in order of precendence. |
264
|
|
|
*/ |
265
|
|
|
public function getThemePaths($themes) { |
266
|
|
|
$paths = []; |
267
|
|
|
foreach($themes as $themename) { |
268
|
|
|
// Expand theme sets |
269
|
|
|
$set = $this->getSet($themename); |
270
|
|
|
$subthemes = $set ? $set->getThemes() : [$themename]; |
271
|
|
|
|
272
|
|
|
// Resolve paths |
273
|
|
|
foreach ($subthemes as $theme) { |
274
|
|
|
$paths[] = $this->getPath($theme); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
return $paths; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
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
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.