MDL-50085 Mustache : Strictly define the search paths for templates.

This change moves several repeated sections of code that was searching
for templates and valid template locations to a new class. It adds
unit tests for the new class and verifies subsystem support for templates.
This commit is contained in:
Damyon Wiese
2015-05-05 03:19:25 +02:00
committed by Eloy Lafuente (stronk7)
parent 890078bf03
commit fcc383db0f
8 changed files with 268 additions and 107 deletions
+31 -18
View File
@@ -23,6 +23,7 @@
*/
namespace tool_templatelibrary;
use core\output\mustache_template_finder;
use stdClass;
use core_component;
use coding_exception;
@@ -43,7 +44,7 @@ class api {
* @param string $search Search string to optionally filter the list of templates.
* @return array[string] Where each template is in the form "component/templatename".
*/
public static function list_templates($component = '', $search = '') {
public static function list_templates($component = '', $search = '', $themename = '') {
global $CFG;
$templatedirs = array();
@@ -51,41 +52,53 @@ class api {
if ($component != '') {
// Just look at one component for templates.
$dir = core_component::get_component_directory($component);
if (!$dir) {
return $templatedirs;
}
$dirs = mustache_template_finder::get_template_directories_for_component($component, $themename);
$templatedirs[$component] = $dir . '/templates';
$templatedirs[$component] = $dirs;
} else {
// Look at all the templates dirs for all installed plugins.
$dir = $CFG->libdir . '/templates';
if (!empty($dir) && is_dir($dir)) {
$templatedirs['core'] = $dir;
// Look at all the templates dirs for core.
$templatedirs['core'] = mustache_template_finder::get_template_directories_for_component('core', $themename);
// Look at all the templates dirs for subsystems.
$subsystems = core_component::get_core_subsystems();
foreach ($subsystems as $subsystem => $dir) {
$dir .= '/templates';
if (is_dir($dir)) {
$dirs = mustache_template_finder::get_template_directories_for_component('core_' . $subsystem, $themename);
$templatedirs['core_' . $subsystem] = $dirs;
}
}
// Look at all the templates dirs for plugins.
$plugintypes = core_component::get_plugin_types();
foreach ($plugintypes as $type => $dir) {
$plugins = core_component::get_plugin_list_with_file($type, 'templates', false);
foreach ($plugins as $plugin => $dir) {
if (!empty($dir) && is_dir($dir)) {
$templatedirs[$type . '_' . $plugin] = $dir;
$pluginname = $type . '_' . $plugin;
$dirs = mustache_template_finder::get_template_directories_for_component($pluginname, $themename);
$templatedirs[$pluginname] = $dirs;
}
}
}
}
foreach ($templatedirs as $templatecomponent => $dir) {
// List it.
$files = glob($dir . '/*.mustache');
foreach ($templatedirs as $templatecomponent => $dirs) {
foreach ($dirs as $dir) {
// List it.
$files = glob($dir . '/*.mustache');
foreach ($files as $file) {
$templatename = basename($file, '.mustache');
if ($search == '' || strpos($templatename, $search) !== false) {
$results[] = $templatecomponent . '/' . $templatename;
foreach ($files as $file) {
$templatename = basename($file, '.mustache');
if ($search == '' || strpos($templatename, $search) !== false) {
$results[$templatecomponent . '/' . $templatename] = 1;
}
}
}
}
$results = array_keys($results);
sort($results);
return $results;
}