Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
namespace core\output;
18
 
1441 ariadna 19
use core\exception\coding_exception;
20
use core\exception\moodle_exception;
1 efrain 21
use core_component;
22
 
23
/**
24
 * Get information about valid locations for mustache templates.
25
 *
26
 * @copyright  2015 Damyon Wiese
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @since      2.9
1441 ariadna 29
 * @package core
1 efrain 30
 */
31
class mustache_template_finder {
32
    /**
33
     * Helper function for getting a list of valid template directories for a specific component.
34
     *
35
     * @param string $component The component to search
36
     * @param string $themename The current theme name
37
     * @return string[] List of valid directories for templates for this compoonent. Directories are not checked for existence.
38
     */
39
    public static function get_template_directories_for_component($component, $themename = '') {
40
        global $CFG, $PAGE;
41
 
42
        // Default the param.
43
        if ($themename == '') {
44
            $themename = $PAGE->theme->name;
45
        }
46
 
47
        // Clean params for safety.
48
        $component = clean_param($component, PARAM_COMPONENT);
49
        $themename = clean_param($themename, PARAM_COMPONENT);
50
 
51
        // Validate the component.
1441 ariadna 52
        $dirs = [];
1 efrain 53
        $compdirectory = core_component::get_component_directory($component);
54
        if (!$compdirectory) {
55
            throw new coding_exception("Component was not valid: " . s($component));
56
        }
57
 
58
        // Find the parent themes.
1441 ariadna 59
        $parents = [];
1 efrain 60
        if ($themename === $PAGE->theme->name) {
61
            $parents = $PAGE->theme->parents;
62
        } else {
63
            $themeconfig = theme_config::load($themename);
64
            $parents = $themeconfig->parents;
65
        }
66
 
67
        // First check the theme.
68
        $dirs[] = $CFG->dirroot . '/theme/' . $themename . '/templates/' . $component . '/';
69
        if (isset($CFG->themedir)) {
70
            $dirs[] = $CFG->themedir . '/' . $themename . '/templates/' . $component . '/';
71
        }
72
        // Now check the parent themes.
73
        // Search each of the parent themes second.
74
        foreach ($parents as $parent) {
75
            $dirs[] = $CFG->dirroot . '/theme/' . $parent . '/templates/' . $component . '/';
76
            if (isset($CFG->themedir)) {
77
                $dirs[] = $CFG->themedir . '/' . $parent . '/templates/' . $component . '/';
78
            }
79
        }
80
 
81
        $dirs[] = $compdirectory . '/templates/';
82
 
83
        return $dirs;
84
    }
85
 
86
    /**
87
     * Helper function for getting a filename for a template from the template name.
88
     *
89
     * @param string $name - This is the componentname/templatename combined.
90
     * @param string $themename - This is the current theme name.
91
     * @return string
92
     */
93
    public static function get_template_filepath($name, $themename = '') {
94
        global $CFG, $PAGE;
95
 
96
        if (strpos($name, '/') === false) {
97
            throw new coding_exception('Templates names must be specified as "componentname/templatename"' .
98
                                       ' (' . s($name) . ' requested) ');
99
        }
100
 
1441 ariadna 101
        [$component, $templatename] = explode('/', $name, 2);
1 efrain 102
        $component = clean_param($component, PARAM_COMPONENT);
103
 
104
        $dirs = self::get_template_directories_for_component($component, $themename);
105
 
106
        foreach ($dirs as $dir) {
107
            $candidate = $dir . $templatename . '.mustache';
108
            if (file_exists($candidate)) {
109
                return $candidate;
110
            }
111
        }
112
 
113
        throw new moodle_exception('filenotfound', 'error', '', null, $name);
114
    }
115
}