Merge branch 'MDL-57742_master' of git://github.com/markn86/moodle
[moodle.git] / lib / classes / output / mustache_template_finder.php
blob397fa761ed1c60f67d78a62f301b920cdf7e1474
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
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/>.
17 /**
18 * List the valid locations to search for a template with a given name.
20 * @package core
21 * @category output
22 * @copyright 2015 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core\output;
28 use coding_exception;
29 use moodle_exception;
30 use core_component;
31 use theme_config;
33 /**
34 * Get information about valid locations for mustache templates.
36 * @copyright 2015 Damyon Wiese
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since 2.9
40 class mustache_template_finder {
42 /**
43 * Helper function for getting a list of valid template directories for a specific component.
45 * @param string $component The component to search
46 * @param string $themename The current theme name
47 * @return string[] List of valid directories for templates for this compoonent. Directories are not checked for existence.
49 public static function get_template_directories_for_component($component, $themename = '') {
50 global $CFG, $PAGE;
52 // Default the param.
53 if ($themename == '') {
54 $themename = $PAGE->theme->name;
57 // Clean params for safety.
58 $component = clean_param($component, PARAM_COMPONENT);
59 $themename = clean_param($themename, PARAM_COMPONENT);
61 // Validate the component.
62 $dirs = array();
63 $compdirectory = core_component::get_component_directory($component);
64 if (!$compdirectory) {
65 throw new coding_exception("Component was not valid: " . s($component));
68 // Find the parent themes.
69 $parents = array();
70 if ($themename === $PAGE->theme->name) {
71 $parents = $PAGE->theme->parents;
72 } else {
73 $themeconfig = theme_config::load($themename);
74 $parents = $themeconfig->parents;
77 // First check the theme.
78 $dirs[] = $CFG->dirroot . '/theme/' . $themename . '/templates/' . $component . '/';
79 if (isset($CFG->themedir)) {
80 $dirs[] = $CFG->themedir . '/' . $themename . '/templates/' . $component . '/';
82 // Now check the parent themes.
83 // Search each of the parent themes second.
84 foreach ($parents as $parent) {
85 $dirs[] = $CFG->dirroot . '/theme/' . $parent . '/templates/' . $component . '/';
86 if (isset($CFG->themedir)) {
87 $dirs[] = $CFG->themedir . '/' . $parent . '/templates/' . $component . '/';
91 $dirs[] = $compdirectory . '/templates/';
93 return $dirs;
96 /**
97 * Helper function for getting a filename for a template from the template name.
99 * @param string $name - This is the componentname/templatename combined.
100 * @param string $themename - This is the current theme name.
101 * @return string
103 public static function get_template_filepath($name, $themename = '') {
104 global $CFG, $PAGE;
106 if (strpos($name, '/') === false) {
107 throw new coding_exception('Templates names must be specified as "componentname/templatename"' .
108 ' (' . s($name) . ' requested) ');
110 list($component, $templatename) = explode('/', $name, 2);
111 $component = clean_param($component, PARAM_COMPONENT);
112 if (strpos($templatename, '/') !== false) {
113 throw new coding_exception('Templates cannot be placed in sub directories (' . s($name) . ' requested)');
116 $dirs = self::get_template_directories_for_component($component, $themename);
118 foreach ($dirs as $dir) {
119 $candidate = $dir . $templatename . '.mustache';
120 if (file_exists($candidate)) {
121 return $candidate;
125 throw new moodle_exception('filenotfound', 'error', '', null, $name);