1441 |
ariadna |
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\renderer_factory;
|
|
|
18 |
|
|
|
19 |
use core_component;
|
|
|
20 |
use core\exception\coding_exception;
|
|
|
21 |
use core\output\theme_config;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* This is a base class to help you implement the renderer_factory interface.
|
|
|
25 |
*
|
|
|
26 |
* It keeps a cache of renderers that have been constructed, so you only need
|
|
|
27 |
* to construct each one once in you subclass.
|
|
|
28 |
*
|
|
|
29 |
* It also has a method to get the name of, and include the renderer.php with
|
|
|
30 |
* the definition of, the standard renderer class for a given module.
|
|
|
31 |
*
|
|
|
32 |
* @copyright 2009 Tim Hunt
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
* @since Moodle 2.0
|
|
|
35 |
* @package core
|
|
|
36 |
* @category output
|
|
|
37 |
*/
|
|
|
38 |
abstract class renderer_factory_base implements renderer_factory_interface {
|
|
|
39 |
/**
|
|
|
40 |
* Constructor.
|
|
|
41 |
*
|
|
|
42 |
* @param theme_config $theme the theme we belong to.
|
|
|
43 |
*/
|
|
|
44 |
public function __construct(
|
|
|
45 |
/** @var theme_config The theme we belong to */
|
|
|
46 |
protected theme_config $theme,
|
|
|
47 |
) {
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Returns suffix of renderer class expected for given target.
|
|
|
52 |
*
|
|
|
53 |
* @param string $target one of the renderer target constants, target is guessed if null used
|
|
|
54 |
* @return array two element array, first element is target, second the target suffix string
|
|
|
55 |
*/
|
|
|
56 |
protected function get_target_suffix($target) {
|
|
|
57 |
if (empty($target) || $target === RENDERER_TARGET_MAINTENANCE) {
|
|
|
58 |
// If the target hasn't been specified we need to guess the defaults.
|
|
|
59 |
// We also override the target with the default if the maintenance target has been provided.
|
|
|
60 |
// This ensures we don't use the maintenance renderer if we are processing a special target.
|
|
|
61 |
if (defined('PREFERRED_RENDERER_TARGET')) {
|
|
|
62 |
$target = PREFERRED_RENDERER_TARGET;
|
|
|
63 |
} else if (CLI_SCRIPT) {
|
|
|
64 |
$target = RENDERER_TARGET_CLI;
|
|
|
65 |
} else if (AJAX_SCRIPT) {
|
|
|
66 |
$target = RENDERER_TARGET_AJAX;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
switch ($target) {
|
|
|
71 |
case RENDERER_TARGET_CLI:
|
|
|
72 |
$suffix = '_cli';
|
|
|
73 |
break;
|
|
|
74 |
case RENDERER_TARGET_AJAX:
|
|
|
75 |
$suffix = '_ajax';
|
|
|
76 |
break;
|
|
|
77 |
case RENDERER_TARGET_TEXTEMAIL:
|
|
|
78 |
$suffix = '_textemail';
|
|
|
79 |
break;
|
|
|
80 |
case RENDERER_TARGET_HTMLEMAIL:
|
|
|
81 |
$suffix = '_htmlemail';
|
|
|
82 |
break;
|
|
|
83 |
case RENDERER_TARGET_MAINTENANCE:
|
|
|
84 |
$suffix = '_maintenance';
|
|
|
85 |
break;
|
|
|
86 |
default:
|
|
|
87 |
$target = RENDERER_TARGET_GENERAL;
|
|
|
88 |
$suffix = '';
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
return [$target, $suffix];
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* For a given module name, return the possible class names
|
|
|
96 |
* that defines the renderer interface for that module.
|
|
|
97 |
*
|
|
|
98 |
* Newer auto-loaded class names are returned as well as the old style _renderable classnames.
|
|
|
99 |
*
|
|
|
100 |
* Also, if it exists, include the renderer.php file for that module, so
|
|
|
101 |
* the class definition of the default renderer has been loaded.
|
|
|
102 |
*
|
|
|
103 |
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
|
|
104 |
* @param string $subtype optional subtype such as 'news' resulting to:
|
|
|
105 |
* '\mod_forum\output\news_renderer'
|
|
|
106 |
* or '\mod_forum\output\news\renderer'
|
|
|
107 |
* or non-autoloaded 'mod_forum_news'
|
|
|
108 |
* @return array[] Each element of the array is an array with keys:
|
|
|
109 |
* classname - The class name to search
|
|
|
110 |
* autoloaded - Does this classname assume autoloading?
|
|
|
111 |
* validwithprefix - Is this class name valid when a prefix is added to it?
|
|
|
112 |
* validwithoutprefix - Is this class name valid when no prefix is added to it?
|
|
|
113 |
* @throws coding_exception
|
|
|
114 |
*/
|
|
|
115 |
protected function standard_renderer_classnames($component, $subtype = null) {
|
|
|
116 |
global $CFG; // Needed in included files.
|
|
|
117 |
$classnames = [];
|
|
|
118 |
|
|
|
119 |
// Standardize component name ala frankenstyle.
|
|
|
120 |
[$plugin, $type] = core_component::normalize_component($component);
|
|
|
121 |
if ($type === null) {
|
|
|
122 |
$component = $plugin;
|
|
|
123 |
} else {
|
|
|
124 |
$component = $plugin . '_' . $type;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
if ($component !== 'core') {
|
|
|
128 |
// Renderers are stored in renderer.php files.
|
|
|
129 |
if (!$compdirectory = core_component::get_component_directory($component)) {
|
|
|
130 |
throw new coding_exception('Invalid component specified in renderer request', $component);
|
|
|
131 |
}
|
|
|
132 |
$rendererfile = $compdirectory . '/renderer.php';
|
|
|
133 |
if (file_exists($rendererfile)) {
|
|
|
134 |
include_once($rendererfile);
|
|
|
135 |
}
|
|
|
136 |
} else if (!empty($subtype)) {
|
|
|
137 |
$coresubsystems = core_component::get_core_subsystems();
|
|
|
138 |
if (!array_key_exists($subtype, $coresubsystems)) { // There may be nulls.
|
|
|
139 |
throw new coding_exception('Invalid core subtype "' . $subtype . '" in renderer request', $subtype);
|
|
|
140 |
}
|
|
|
141 |
if ($coresubsystems[$subtype]) {
|
|
|
142 |
$rendererfile = $coresubsystems[$subtype] . '/renderer.php';
|
|
|
143 |
if (file_exists($rendererfile)) {
|
|
|
144 |
include_once($rendererfile);
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
if (empty($subtype)) {
|
|
|
150 |
// Theme specific auto-loaded name (only valid when prefixed with the theme name).
|
|
|
151 |
$classnames[] = [
|
|
|
152 |
'validwithprefix' => true,
|
|
|
153 |
'validwithoutprefix' => false,
|
|
|
154 |
'autoloaded' => true,
|
|
|
155 |
'classname' => '\\output\\' . $component . '_renderer',
|
|
|
156 |
];
|
|
|
157 |
|
|
|
158 |
// Standard autoloaded plugin name (not valid with a prefix).
|
|
|
159 |
$classnames[] = [
|
|
|
160 |
'validwithprefix' => false,
|
|
|
161 |
'validwithoutprefix' => true,
|
|
|
162 |
'autoloaded' => true,
|
|
|
163 |
'classname' => '\\' . $component . '\\output\\renderer',
|
|
|
164 |
];
|
|
|
165 |
// Legacy class name - (valid with or without a prefix).
|
|
|
166 |
$classnames[] = [
|
|
|
167 |
'validwithprefix' => true,
|
|
|
168 |
'validwithoutprefix' => true,
|
|
|
169 |
'autoloaded' => false,
|
|
|
170 |
'classname' => $component . '_renderer',
|
|
|
171 |
];
|
|
|
172 |
} else {
|
|
|
173 |
// Theme specific auto-loaded name (only valid when prefixed with the theme name).
|
|
|
174 |
$classnames[] = [
|
|
|
175 |
'validwithprefix' => true,
|
|
|
176 |
'validwithoutprefix' => false,
|
|
|
177 |
'autoloaded' => true,
|
|
|
178 |
'classname' => '\\output\\' . $component . '\\' . $subtype . '_renderer',
|
|
|
179 |
];
|
|
|
180 |
// Version of the above with subtype being a namespace level on it's own.
|
|
|
181 |
$classnames[] = [
|
|
|
182 |
'validwithprefix' => true,
|
|
|
183 |
'validwithoutprefix' => false,
|
|
|
184 |
'autoloaded' => true,
|
|
|
185 |
'classname' => '\\output\\' . $component . '\\' . $subtype . '\\renderer',
|
|
|
186 |
];
|
|
|
187 |
// Standard autoloaded plugin name (not valid with a prefix).
|
|
|
188 |
$classnames[] = [
|
|
|
189 |
'validwithprefix' => false,
|
|
|
190 |
'validwithoutprefix' => true,
|
|
|
191 |
'autoloaded' => true,
|
|
|
192 |
'classname' => '\\' . $component . '\\output\\' . $subtype . '_renderer',
|
|
|
193 |
];
|
|
|
194 |
// Version of the above with subtype being a namespace level on it's own.
|
|
|
195 |
$classnames[] = [
|
|
|
196 |
'validwithprefix' => false,
|
|
|
197 |
'validwithoutprefix' => true,
|
|
|
198 |
'autoloaded' => true,
|
|
|
199 |
'classname' => '\\' . $component . '\\output\\' . $subtype . '\\renderer',
|
|
|
200 |
];
|
|
|
201 |
// Legacy class name - (valid with or without a prefix).
|
|
|
202 |
$classnames[] = [
|
|
|
203 |
'validwithprefix' => true,
|
|
|
204 |
'validwithoutprefix' => true,
|
|
|
205 |
'autoloaded' => false,
|
|
|
206 |
'classname' => $component . '_' . $subtype . '_renderer',
|
|
|
207 |
];
|
|
|
208 |
}
|
|
|
209 |
return $classnames;
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
// Alias this class to the old name.
|
|
|
214 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
215 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
216 |
class_alias(renderer_factory_base::class, \renderer_factory_base::class);
|