| Línea 12... |
Línea 12... |
| 12 |
// GNU General Public License for more details.
|
12 |
// GNU General Public License for more details.
|
| 13 |
//
|
13 |
//
|
| 14 |
// You should have received a copy of the GNU General Public License
|
14 |
// You should have received a copy of the GNU General Public License
|
| 15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
| Línea 16... |
Línea -... |
| 16 |
|
- |
|
| 17 |
/**
|
- |
|
| 18 |
* Load template source strings.
|
- |
|
| 19 |
*
|
- |
|
| 20 |
* @package core
|
- |
|
| 21 |
* @category output
|
- |
|
| 22 |
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
|
- |
|
| 23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
- |
|
| 24 |
*/
|
- |
|
| 25 |
|
16 |
|
| Línea 26... |
Línea -... |
| 26 |
namespace core\output;
|
- |
|
| 27 |
|
- |
|
| 28 |
defined('MOODLE_INTERNAL') || die();
|
17 |
namespace core\output;
|
| Línea 29... |
Línea 18... |
| 29 |
|
18 |
|
| 30 |
use \Mustache_Tokenizer;
|
19 |
use Mustache_Tokenizer;
|
| 31 |
|
20 |
|
| 32 |
/**
|
21 |
/**
|
| 33 |
* Load template source strings.
|
22 |
* Load template source strings.
|
| - |
|
23 |
*
|
| 34 |
*
|
24 |
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
|
| 35 |
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
- |
|
| 37 |
*/
|
26 |
* @package core
|
| 38 |
class mustache_template_source_loader {
|
27 |
*/
|
| Línea 39... |
Línea 28... |
| 39 |
|
28 |
class mustache_template_source_loader {
|
| 40 |
/** @var $gettemplatesource Callback function to load the template source from full name */
|
29 |
/** @var $gettemplatesource Callback function to load the template source from full name */
|
| Línea 46... |
Línea 35... |
| 46 |
*
|
35 |
*
|
| 47 |
* If no callback is provided then default to the load from disk implementation.
|
36 |
* If no callback is provided then default to the load from disk implementation.
|
| 48 |
*
|
37 |
*
|
| 49 |
* @param callable|null $gettemplatesource Callback to load template source by template name
|
38 |
* @param callable|null $gettemplatesource Callback to load template source by template name
|
| 50 |
*/
|
39 |
*/
|
| 51 |
public function __construct(callable $gettemplatesource = null) {
|
40 |
public function __construct(?callable $gettemplatesource = null) {
|
| 52 |
if ($gettemplatesource) {
|
41 |
if ($gettemplatesource) {
|
| 53 |
// The calling code has specified a function for retrieving the template source
|
42 |
// The calling code has specified a function for retrieving the template source
|
| 54 |
// code by name and theme.
|
43 |
// code by name and theme.
|
| 55 |
$this->gettemplatesource = $gettemplatesource;
|
44 |
$this->gettemplatesource = $gettemplatesource;
|
| 56 |
} else {
|
45 |
} else {
|
| 57 |
// By default we will pull the template from disk.
|
46 |
// By default we will pull the template from disk.
|
| 58 |
$this->gettemplatesource = function($component, $name, $themename) {
|
47 |
$this->gettemplatesource = function ($component, $name, $themename) {
|
| 59 |
$fulltemplatename = $component . '/' . $name;
|
48 |
$fulltemplatename = $component . '/' . $name;
|
| 60 |
$filename = mustache_template_finder::get_template_filepath($fulltemplatename, $themename);
|
49 |
$filename = mustache_template_finder::get_template_filepath($fulltemplatename, $themename);
|
| 61 |
return file_get_contents($filename);
|
50 |
return file_get_contents($filename);
|
| 62 |
};
|
51 |
};
|
| 63 |
}
|
52 |
}
|
| Línea 149... |
Línea 138... |
| 149 |
string $templatename,
|
138 |
string $templatename,
|
| 150 |
string $themename,
|
139 |
string $themename,
|
| 151 |
bool $includecomments = false,
|
140 |
bool $includecomments = false,
|
| 152 |
array $seentemplates = [],
|
141 |
array $seentemplates = [],
|
| 153 |
array $seenstrings = [],
|
142 |
array $seenstrings = [],
|
| 154 |
string $lang = null
|
143 |
?string $lang = null
|
| 155 |
): array {
|
144 |
): array {
|
| 156 |
// Initialise the return values.
|
145 |
// Initialise the return values.
|
| 157 |
$templates = [];
|
146 |
$templates = [];
|
| 158 |
$strings = [];
|
147 |
$strings = [];
|
| 159 |
$templatecomponent = trim($templatecomponent);
|
148 |
$templatecomponent = trim($templatecomponent);
|
| 160 |
$templatename = trim($templatename);
|
149 |
$templatename = trim($templatename);
|
| 161 |
// Get the requested template source.
|
150 |
// Get the requested template source.
|
| 162 |
$templatesource = $this->load($templatecomponent, $templatename, $themename, $includecomments);
|
151 |
$templatesource = $this->load($templatecomponent, $templatename, $themename, $includecomments);
|
| 163 |
// This is a helper function to save a value in one of the result arrays (either $templates or $strings).
|
152 |
// This is a helper function to save a value in one of the result arrays (either $templates or $strings).
|
| 164 |
$save = function(array $results, array $seenlist, string $component, string $id, $value) use ($lang) {
|
153 |
$save = function (array $results, array $seenlist, string $component, string $id, $value) use ($lang) {
|
| 165 |
if (!isset($results[$component])) {
|
154 |
if (!isset($results[$component])) {
|
| 166 |
// If the results list doesn't already contain this component then initialise it.
|
155 |
// If the results list doesn't already contain this component then initialise it.
|
| 167 |
$results[$component] = [];
|
156 |
$results[$component] = [];
|
| 168 |
}
|
157 |
}
|
| Línea 174... |
Línea 163... |
| 174 |
// Return the updated results and seen list.
|
163 |
// Return the updated results and seen list.
|
| 175 |
return [$results, $seenlist];
|
164 |
return [$results, $seenlist];
|
| 176 |
};
|
165 |
};
|
| 177 |
// This is a helper function for processing a dependency. Does stuff like ignore duplicate processing,
|
166 |
// This is a helper function for processing a dependency. Does stuff like ignore duplicate processing,
|
| 178 |
// common result formatting etc.
|
167 |
// common result formatting etc.
|
| 179 |
$handler = function(array $dependency, array $ignorelist, callable $processcallback) use ($lang) {
|
168 |
$handler = function (array $dependency, array $ignorelist, callable $processcallback) use ($lang) {
|
| 180 |
foreach ($dependency as $component => $ids) {
|
169 |
foreach ($dependency as $component => $ids) {
|
| 181 |
foreach ($ids as $id) {
|
170 |
foreach ($ids as $id) {
|
| 182 |
$dependencyid = "$component/$id";
|
171 |
$dependencyid = "$component/$id";
|
| 183 |
if (array_search($dependencyid, $ignorelist) === false) {
|
172 |
if (array_search($dependencyid, $ignorelist) === false) {
|
| 184 |
$processcallback($component, $id);
|
173 |
$processcallback($component, $id);
|
| Línea 191... |
Línea 180... |
| 191 |
|
180 |
|
| 192 |
return $ignorelist;
|
181 |
return $ignorelist;
|
| Línea 193... |
Línea 182... |
| 193 |
};
|
182 |
};
|
| 194 |
|
183 |
|
| Línea 195... |
Línea 184... |
| 195 |
// Save this template as the first result in the $templates result array.
|
184 |
// Save this template as the first result in the $templates result array.
|
| 196 |
list($templates, $seentemplates) = $save($templates, $seentemplates, $templatecomponent, $templatename, $templatesource);
|
185 |
[$templates, $seentemplates] = $save($templates, $seentemplates, $templatecomponent, $templatename, $templatesource);
|
| Línea 197... |
Línea 186... |
| 197 |
|
186 |
|
| Línea 203... |
Línea 192... |
| 203 |
$seenstrings = $handler(
|
192 |
$seenstrings = $handler(
|
| 204 |
$dependencies['strings'],
|
193 |
$dependencies['strings'],
|
| 205 |
$seenstrings,
|
194 |
$seenstrings,
|
| 206 |
// Include $strings and $seenstrings by reference so that their values can be updated
|
195 |
// Include $strings and $seenstrings by reference so that their values can be updated
|
| 207 |
// outside of this anonymous function.
|
196 |
// outside of this anonymous function.
|
| 208 |
function($component, $id) use ($save, &$strings, &$seenstrings, $lang) {
|
197 |
function ($component, $id) use ($save, &$strings, &$seenstrings, $lang) {
|
| 209 |
$string = get_string_manager()->get_string($id, $component, null, $lang);
|
198 |
$string = get_string_manager()->get_string($id, $component, null, $lang);
|
| 210 |
// Save the string in the $strings results array.
|
199 |
// Save the string in the $strings results array.
|
| 211 |
list($strings, $seenstrings) = $save($strings, $seenstrings, $component, $id, $string);
|
200 |
[$strings, $seenstrings] = $save($strings, $seenstrings, $component, $id, $string);
|
| 212 |
}
|
201 |
}
|
| 213 |
);
|
202 |
);
|
| Línea 214... |
Línea 203... |
| 214 |
|
203 |
|
| 215 |
// Load any child templates that we've found in this template and add them to
|
204 |
// Load any child templates that we've found in this template and add them to
|
| 216 |
// the return list of dependencies.
|
205 |
// the return list of dependencies.
|
| 217 |
$seentemplates = $handler(
|
206 |
$seentemplates = $handler(
|
| 218 |
$dependencies['templates'],
|
207 |
$dependencies['templates'],
|
| 219 |
$seentemplates,
|
208 |
$seentemplates,
|
| 220 |
// Include $strings, $seenstrings, $templates, and $seentemplates by reference so that their values can be updated
|
209 |
// Include $strings, $seenstrings, $templates, and $seentemplates by reference so that their values can be updated
|
| - |
|
210 |
// outside of this anonymous function.
|
| 221 |
// outside of this anonymous function.
|
211 |
function (
|
| - |
|
212 |
$component,
|
| - |
|
213 |
$id
|
| 222 |
function($component, $id) use (
|
214 |
) use (
|
| 223 |
$themename,
|
215 |
$themename,
|
| 224 |
$includecomments,
|
216 |
$includecomments,
|
| 225 |
&$seentemplates,
|
217 |
&$seentemplates,
|
| 226 |
&$seenstrings,
|
218 |
&$seenstrings,
|
| Línea 241... |
Línea 233... |
| 241 |
);
|
233 |
);
|
| Línea 242... |
Línea 234... |
| 242 |
|
234 |
|
| 243 |
foreach ($subdependencies['templates'] as $component => $ids) {
|
235 |
foreach ($subdependencies['templates'] as $component => $ids) {
|
| 244 |
foreach ($ids as $id => $value) {
|
236 |
foreach ($ids as $id => $value) {
|
| 245 |
// Include the child themes in our results.
|
237 |
// Include the child themes in our results.
|
| 246 |
list($templates, $seentemplates) = $save($templates, $seentemplates, $component, $id, $value);
|
238 |
[$templates, $seentemplates] = $save($templates, $seentemplates, $component, $id, $value);
|
| 247 |
}
|
239 |
}
|
| Línea 248... |
Línea 240... |
| 248 |
};
|
240 |
};
|
| 249 |
|
241 |
|
| 250 |
foreach ($subdependencies['strings'] as $component => $ids) {
|
242 |
foreach ($subdependencies['strings'] as $component => $ids) {
|
| 251 |
foreach ($ids as $id => $value) {
|
243 |
foreach ($ids as $id => $value) {
|
| 252 |
// Include any strings that the child templates need in our results.
|
244 |
// Include any strings that the child templates need in our results.
|
| 253 |
list($strings, $seenstrings) = $save($strings, $seenstrings, $component, $id, $value);
|
245 |
[$strings, $seenstrings] = $save($strings, $seenstrings, $component, $id, $value);
|
| 254 |
}
|
246 |
}
|
| 255 |
}
|
247 |
}
|
| Línea 256... |
Línea 248... |
| 256 |
}
|
248 |
}
|
| 257 |
);
|
249 |
);
|
| 258 |
|
250 |
|
| 259 |
return [
|
251 |
return [
|
| 260 |
'templates' => $templates,
|
252 |
'templates' => $templates,
|
| Línea 261... |
Línea 253... |
| 261 |
'strings' => $strings
|
253 |
'strings' => $strings,
|
| 262 |
];
|
254 |
];
|
| Línea 289... |
Línea 281... |
| 289 |
protected function scan_template_source_for_dependencies(string $source): array {
|
281 |
protected function scan_template_source_for_dependencies(string $source): array {
|
| 290 |
$tokenizer = new Mustache_Tokenizer();
|
282 |
$tokenizer = new Mustache_Tokenizer();
|
| 291 |
$tokens = $tokenizer->scan($source);
|
283 |
$tokens = $tokenizer->scan($source);
|
| 292 |
$templates = [];
|
284 |
$templates = [];
|
| 293 |
$strings = [];
|
285 |
$strings = [];
|
| 294 |
$addtodependencies = function($dependencies, $component, $id) {
|
286 |
$addtodependencies = function ($dependencies, $component, $id) {
|
| 295 |
$id = trim($id);
|
287 |
$id = trim($id);
|
| 296 |
$component = trim($component);
|
288 |
$component = trim($component);
|
| Línea 297... |
Línea 289... |
| 297 |
|
289 |
|
| 298 |
if (!isset($dependencies[$component])) {
|
290 |
if (!isset($dependencies[$component])) {
|
| Línea 311... |
Línea 303... |
| 311 |
$name = isset($token['name']) ? $token['name'] : null;
|
303 |
$name = isset($token['name']) ? $token['name'] : null;
|
| Línea 312... |
Línea 304... |
| 312 |
|
304 |
|
| 313 |
if ($name) {
|
305 |
if ($name) {
|
| 314 |
switch ($type) {
|
306 |
switch ($type) {
|
| 315 |
case Mustache_Tokenizer::T_PARTIAL:
|
307 |
case Mustache_Tokenizer::T_PARTIAL:
|
| 316 |
list($component, $id) = explode('/', $name, 2);
|
308 |
[$component, $id] = explode('/', $name, 2);
|
| 317 |
$templates = $addtodependencies($templates, $component, $id);
|
309 |
$templates = $addtodependencies($templates, $component, $id);
|
| 318 |
break;
|
310 |
break;
|
| 319 |
case Mustache_Tokenizer::T_PARENT:
|
311 |
case Mustache_Tokenizer::T_PARENT:
|
| 320 |
list($component, $id) = explode('/', $name, 2);
|
312 |
[$component, $id] = explode('/', $name, 2);
|
| 321 |
$templates = $addtodependencies($templates, $component, $id);
|
313 |
$templates = $addtodependencies($templates, $component, $id);
|
| 322 |
break;
|
314 |
break;
|
| 323 |
case Mustache_Tokenizer::T_SECTION:
|
315 |
case Mustache_Tokenizer::T_SECTION:
|
| 324 |
if ($name == 'str') {
|
316 |
if ($name == 'str') {
|
| Línea 325... |
Línea 317... |
| 325 |
list($id, $component) = $this->get_string_identifiers($tokens, $index);
|
317 |
[$id, $component] = $this->get_string_identifiers($tokens, $index);
|
| 326 |
|
318 |
|
| 327 |
if ($id) {
|
319 |
if ($id) {
|
| 328 |
$strings = $addtodependencies($strings, $component, $id);
|
320 |
$strings = $addtodependencies($strings, $component, $id);
|
| Línea 333... |
Línea 325... |
| 333 |
}
|
325 |
}
|
| 334 |
}
|
326 |
}
|
| Línea 335... |
Línea 327... |
| 335 |
|
327 |
|
| 336 |
return [
|
328 |
return [
|
| 337 |
'templates' => $templates,
|
329 |
'templates' => $templates,
|
| 338 |
'strings' => $strings
|
330 |
'strings' => $strings,
|
| 339 |
];
|
331 |
];
|
| Línea 340... |
Línea 332... |
| 340 |
}
|
332 |
}
|
| 341 |
|
333 |
|