forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhooks.php
97 lines (85 loc) · 3.5 KB
/
hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
namespace core;
/**
* Standard hook discovery agent for Moodle which lists
* all non-abstract classes in hooks namespace of core and all plugins
* unless there is a hook discovery agent in a plugin.
*
* @package core
* @copyright Andrew Lyons <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class hooks implements \core\hook\discovery_agent {
/**
* Returns all Moodle hooks in standard hook namespace.
*
* @return array list of hook classes
*/
public static function discover_hooks(): array {
// Look for hooks in hook namespace in core and all components.
$hooks = [];
$hooks = array_merge($hooks, self::discover_hooks_in_namespace('core', 'hook'));
foreach (\core_component::get_component_names() as $component) {
$agent = "$component\\hooks";
if (class_exists($agent) && is_subclass_of($agent, hook\discovery_agent::class)) {
// Let the plugin supply the list of hooks instead.
continue;
}
$hooks = array_merge($hooks, self::discover_hooks_in_namespace($component, 'hook'));
}
return $hooks;
}
/**
* Look up all non-abstract classes in "$component\$namespace" namespace.
*
* @param string $component
* @param string $namespace
* @return array list of hook classes
*/
public static function discover_hooks_in_namespace(string $component, string $namespace): array {
$classes = \core_component::get_component_classes_in_namespace($component, $namespace);
$hooks = [];
foreach (array_keys($classes) as $classname) {
$rc = new \ReflectionClass($classname);
if ($rc->isAbstract()) {
// Skip abstract classes.
continue;
}
if ($classname === \core\hook\manager::class) {
// Skip the manager in core.
continue;
}
$hooks[$classname] = [
'class' => $classname,
'description' => '',
'tags' => [],
];
if (is_subclass_of($classname, \core\hook\described_hook::class)) {
$hooks[$classname]['description'] = $classname::get_hook_description();
$hooks[$classname]['tags'] = $classname::get_hook_tags();
} else {
if ($description = attribute_helper::instance($classname, \core\attribute\label::class)) {
$hooks[$classname]['description'] = (string) $description->label;
}
if ($tags = attribute_helper::instance($classname, \core\attribute\tags::class)) {
$hooks[$classname]['tags'] = $tags->tags;
}
}
}
return $hooks;
}
}