Merge branch 'MDL-57742_master' of git://github.com/markn86/moodle
[moodle.git] / lib / classes / output / icon_system.php
blob08e88b54d2e906f1c38a17d0b572c1fb72066c99
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.
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/>.
17 /**
18 * Contains class \core\output\icon_system
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
26 namespace core\output;
28 use renderer_base;
29 use pix_icon;
31 defined('MOODLE_INTERNAL') || die();
33 /**
34 * Class allowing different systems for mapping and rendering icons.
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).
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
46 abstract class icon_system {
47 /**
48 * @const STANDARD Default icon system.
50 const STANDARD = '\\core\\output\\icon_system_standard';
51 /**
52 * @const FONTAWESOME Default icon system.
54 const FONTAWESOME = '\\core\\output\\icon_system_fontawesome';
56 /**
57 * @var \core\output\icon_system $instance The cached default instance
59 private static $instance = null;
61 /**
62 * @var array $map A cached mapping of moodle icons to other icons
64 private $map = null;
66 /**
67 * Constructor
69 private function __construct() {
72 /**
73 * Factory method
75 * @param $type Either a specific type, or null to get the default type.
76 * @return \core\output\icon_system
78 public final static function instance($type = null) {
79 global $PAGE;
81 if ($type == null) {
82 if (!empty(self::$instance)) {
83 return self::$instance;
85 $type = $PAGE->theme->get_icon_system();
86 self::$instance = new $type();
87 // Default one is a singleton.
88 return self::$instance;
89 } else {
90 // Not a singleton.
91 return new $type();
95 /**
96 * Validate the theme config setting.
98 * @param string $system
99 * @return boolean
101 public final static function is_valid_system($system) {
102 return class_exists($system) && is_subclass_of($system, self::class);
106 * The name of an AMD module extending core/icon_system
108 * @return string
110 public abstract function get_amd_name();
113 * Render the pix icon according to the icon system.
115 * @param renderer_base $output
116 * @param pix_icon $icon
117 * @return string
119 public abstract function render_pix_icon(renderer_base $output, pix_icon $icon);
122 * Overridable function to get a mapping of all icons.
123 * Default is to do no mapping.
125 public function get_icon_name_map() {
126 return [];
130 * Overridable function to map the icon name to something else.
131 * Default is to do no mapping. Map is cached in the singleton.
133 public final function remap_icon_name($iconname, $component) {
134 if ($this->map === null) {
135 $this->map = $this->get_icon_name_map();
137 if ($component == null || $component == 'moodle') {
138 $component = 'core';
139 } else if ($component != 'theme') {
140 $component = \core_component::normalize_componentname($component);
143 if (isset($this->map[$component . ':' . $iconname])) {
144 return $this->map[$component . ':' . $iconname];
146 return false;
150 * Clears the instance cache, for use in unit tests
152 public static function reset_caches() {
153 self::$instance = null;