Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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\exception\coding_exception;
20
use core\output\renderer_base;
21
use core\output\theme_config;
22
use moodle_page;
23
 
24
/**
25
 * This is renderer factory allows themes to override the standard renderers using php code.
26
 *
27
 * It will load any code from theme/mytheme/renderers.php and
28
 * theme/parenttheme/renderers.php, if then exist. Then whenever you ask for
29
 * a renderer for 'component', it will create a mytheme_component_renderer or a
30
 * parenttheme_component_renderer, instead of a component_renderer,
31
 * if either of those classes exist.
32
 *
33
 * @copyright 2009 Tim Hunt
34
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @since Moodle 2.0
36
 * @package core
37
 * @category output
38
 */
39
class theme_overridden_renderer_factory extends renderer_factory_base {
40
    /**
41
     * @var array An array of renderer prefixes
42
     */
43
    protected $prefixes = [];
44
 
45
    /**
46
     * Constructor.
47
     * @param theme_config $theme the theme we are rendering for.
48
     */
49
    public function __construct(theme_config $theme) {
50
        parent::__construct($theme);
51
        // Initialise $this->prefixes.
52
        $this->prefixes = $theme->renderer_prefixes();
53
    }
54
 
55
    /**
56
     * Implement the subclass method
57
     *
58
     * @param moodle_page $page the page the renderer is outputting content for.
59
     * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
60
     * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
61
     * @param string $target one of rendering target constants
62
     * @return renderer_base an object implementing the requested renderer interface.
63
     */
64
    public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
65
        $classnames = $this->standard_renderer_classnames($component, $subtype);
66
 
67
        [$target, $suffix] = $this->get_target_suffix($target);
68
 
69
        // Theme lib.php and renderers.php files are loaded automatically
70
        // when loading the theme configs.
71
 
72
        // First try the renderers with correct suffix.
73
        foreach ($this->prefixes as $prefix) {
74
            foreach ($classnames as $classnamedetails) {
75
                if ($classnamedetails['validwithprefix']) {
76
                    if ($classnamedetails['autoloaded']) {
77
                        $newclassname = $prefix . $classnamedetails['classname'] . $suffix;
78
                    } else {
79
                        $newclassname = $prefix . '_' . $classnamedetails['classname'] . $suffix;
80
                    }
81
                    if (class_exists($newclassname)) {
82
                        return new $newclassname($page, $target);
83
                    }
84
                }
85
            }
86
        }
87
        foreach ($classnames as $classnamedetails) {
88
            if ($classnamedetails['validwithoutprefix']) {
89
                $newclassname = $classnamedetails['classname'] . $suffix;
90
                if (class_exists($newclassname)) {
91
                    // Use the specialised renderer for given target, default renderer might also decide
92
                    // to implement support for more targets.
93
                    return new $newclassname($page, $target);
94
                }
95
            }
96
        }
97
 
98
        // Then try general renderer.
99
        foreach ($this->prefixes as $prefix) {
100
            foreach ($classnames as $classnamedetails) {
101
                if ($classnamedetails['validwithprefix']) {
102
                    if ($classnamedetails['autoloaded']) {
103
                        $newclassname = $prefix . $classnamedetails['classname'];
104
                    } else {
105
                        $newclassname = $prefix . '_' . $classnamedetails['classname'];
106
                    }
107
                    if (class_exists($newclassname)) {
108
                        return new $newclassname($page, $target);
109
                    }
110
                }
111
            }
112
        }
113
 
114
        // Final attempt - no prefix or suffix.
115
        foreach ($classnames as $classnamedetails) {
116
            if ($classnamedetails['validwithoutprefix']) {
117
                $newclassname = $classnamedetails['classname'];
118
                if (class_exists($newclassname)) {
119
                    return new $newclassname($page, $target);
120
                }
121
            }
122
        }
123
        throw new coding_exception('Request for an unknown renderer ' . $component . ', ' . $subtype . ', ' . $target);
124
    }
125
}
126
 
127
// Alias this class to the old name.
128
// This file will be autoloaded by the legacyclasses autoload system.
129
// In future all uses of this class will be corrected and the legacy references will be removed.
130
class_alias(theme_overridden_renderer_factory::class, \theme_overridden_renderer_factory::class);