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
 
19
/**
20
 * Class allowing different systems for mapping and rendering icons.
21
 *
22
 * Possible icon styles are:
23
 *   1. standard - image tags are generated which point to pix icons stored in a plugin pix folder.
24
 *   2. fontawesome - font awesome markup is generated with the name of the icon mapped from the moodle icon name.
25
 *   3. inline - inline tags are used for svg and png so no separate page requests are made (at the expense of page size).
26
 *
27
 * @package    core
28
 * @category   output
29
 * @copyright  2016 Damyon Wiese
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
abstract class icon_system {
33
    /**
34
     * @var string Default icon system.
35
     */
36
    const STANDARD = '\\core\\output\\icon_system_standard';
37
    /**
38
     * @var string Default icon system.
39
     */
40
    const FONTAWESOME = '\\core\\output\\icon_system_fontawesome';
41
 
42
    /**
43
     * @var \core\output\icon_system $instance The cached default instance
44
     */
45
    private static $instance = null;
46
 
47
    /**
48
     * @var array $map A cached mapping of moodle icons to other icons
49
     */
50
    private $map = null;
51
 
52
    /**
53
     * Constructor
54
     */
55
    private function __construct() {
56
    }
57
 
58
    /**
59
     * Factory method
60
     *
61
     * @param string $type Either a specific type, or null to get the default type.
62
     * @return \core\output\icon_system
63
     */
64
    final public static function instance($type = null) {
65
        global $PAGE;
66
 
67
        if (empty(self::$instance)) {
68
            $iconsystem = $PAGE->theme->get_icon_system();
69
            self::$instance = new $iconsystem();
70
        }
71
 
72
        if ($type === null) {
73
            // No type specified. Return the icon system for the current theme.
74
            return self::$instance;
75
        }
76
 
77
        if (!static::is_valid_system($type)) {
78
            throw new \coding_exception("Invalid icon system requested '{$type}'");
79
        }
80
 
81
        if (is_a(self::$instance, $type) && is_a($type, get_class(self::$instance), true)) {
82
            // The requested type is an exact match for the current icon system.
83
            return self::$instance;
84
        } else {
85
            // Return the requested icon system.
86
            return new $type();
87
        }
88
    }
89
 
90
    /**
91
     * Validate the theme config setting.
92
     *
93
     * @param string $system
94
     * @return boolean
95
     */
96
    final public static function is_valid_system($system) {
97
        return class_exists($system) && is_a($system, static::class, true);
98
    }
99
 
100
    /**
101
     * The name of an AMD module extending core/icon_system
102
     *
103
     * @return string
104
     */
105
    abstract public function get_amd_name();
106
 
107
    /**
108
     * Render the pix icon according to the icon system.
109
     *
110
     * @param renderer_base $output
111
     * @param pix_icon $icon
112
     * @return string
113
     */
114
    abstract public function render_pix_icon(renderer_base $output, pix_icon $icon);
115
 
116
    /**
117
     * Overridable function to get a mapping of all icons.
118
     * Default is to do no mapping.
119
     */
120
    public function get_icon_name_map() {
121
        return [];
122
    }
123
 
124
    /**
125
     * Overridable function to map the icon name to something else.
126
     * Default is to do no mapping. Map is cached in the singleton.
127
     */
128
    final public function remap_icon_name($iconname, $component) {
129
        if ($this->map === null) {
130
            $this->map = $this->get_icon_name_map();
131
        }
132
        if ($component == null || $component == 'moodle') {
133
            $component = 'core';
134
        } else if ($component != 'theme') {
135
            $component = \core_component::normalize_componentname($component);
136
        }
137
 
138
        if (isset($this->map[$component . ':' . $iconname])) {
139
            return $this->map[$component . ':' . $iconname];
140
        }
141
        return false;
142
    }
143
 
144
    /**
145
     * Clears the instance cache, for use in unit tests
146
     */
147
    public static function reset_caches() {
148
        self::$instance = null;
149
    }
1441 ariadna 150
 
151
    /**
152
     * Overridable function to get the list of deprecated icons.
153
     *
154
     * @return array with the deprecated key icons (for instance, core:a/download_all).
155
     */
156
    public function get_deprecated_icons(): array {
157
        $deprecated = [];
158
        // Include deprecated icons in plugins too.
159
        $callback = 'get_deprecated_icons';
160
 
161
        if ($pluginsfunction = get_plugins_with_function($callback)) {
162
            foreach ($pluginsfunction as $plugintype => $plugins) {
163
                foreach ($plugins as $pluginfunction) {
164
                    $plugindeprecated = $pluginfunction();
165
                    $deprecated += $plugindeprecated;
166
                }
167
            }
168
        }
169
 
170
        return $deprecated;
171
    }
1 efrain 172
}