Proyectos de Subversion Moodle

Rev

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