From 0aff5ccd0190db06accb80fbde31f577e827e86e Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 15 Feb 2023 17:50:42 +0000 Subject: [PATCH] MDL-77259 tool_monitor: use core component API for event list. The previous method of hardcoded filepaths, specifically for events belonging to core, meant that any events belongs to core subsystems were omitted (core_customfield, core_h5p, core_payment, etc). --- admin/tool/monitor/classes/eventlist.php | 187 +++++++---------------- 1 file changed, 53 insertions(+), 134 deletions(-) diff --git a/admin/tool/monitor/classes/eventlist.php b/admin/tool/monitor/classes/eventlist.php index ccce6260596..e7ec60fd750 100644 --- a/admin/tool/monitor/classes/eventlist.php +++ b/admin/tool/monitor/classes/eventlist.php @@ -14,17 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Event documentation - * - * @package tool_monitor - * @copyright 2014 onwards Ankit Agarwal - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ namespace tool_monitor; -defined('MOODLE_INTERNAL') || die(); +use core_component; +use ReflectionClass; /** * Class for returning event information. @@ -35,126 +29,6 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class eventlist { - /** - * Return all of the core event files. - * - * @return array Core events. - */ - protected static function get_core_eventlist() { - global $CFG; - - // Disable developer debugging as deprecated events will fire warnings. - // Setup backup variables to restore the following settings back to what they were when we are finished. - $debuglevel = $CFG->debug; - $debugdisplay = $CFG->debugdisplay; - $debugdeveloper = $CFG->debugdeveloper; - $CFG->debug = 0; - $CFG->debugdisplay = false; - $CFG->debugdeveloper = false; - - $eventinformation = array(); - $directory = $CFG->libdir . '/classes/event'; - $files = self::get_file_list($directory); - - // Remove exceptional events that will cause problems being displayed. - if (isset($files['unknown_logged'])) { - unset($files['unknown_logged']); - } - foreach ($files as $file => $location) { - $classname = '\\core\\event\\' . $file; - // Check to see if this is actually a valid event. - if (method_exists($classname, 'get_static_info')) { - $ref = new \ReflectionClass($classname); - // Ignore abstracts. - if (!$ref->isAbstract() && $file != 'manager') { - $eventinformation[$classname] = $classname::get_name_with_info(); - } - } - } - // Now enable developer debugging as event information has been retrieved. - $CFG->debug = $debuglevel; - $CFG->debugdisplay = $debugdisplay; - $CFG->debugdeveloper = $debugdeveloper; - return $eventinformation; - } - - /** - * This function returns an array of all events for the plugins of the system. - * - * @param bool $withoutcomponent Return an eventlist without associated components. - * - * @return array A list of events from all plug-ins. - */ - protected static function get_non_core_eventlist($withoutcomponent = false) { - global $CFG; - // Disable developer debugging as deprecated events will fire warnings. - // Setup backup variables to restore the following settings back to what they were when we are finished. - $debuglevel = $CFG->debug; - $debugdisplay = $CFG->debugdisplay; - $debugdeveloper = $CFG->debugdeveloper; - $CFG->debug = 0; - $CFG->debugdisplay = false; - $CFG->debugdeveloper = false; - - $noncorepluginlist = array(); - $plugintypes = \core_component::get_plugin_types(); - foreach ($plugintypes as $plugintype => $notused) { - $pluginlist = \core_component::get_plugin_list($plugintype); - foreach ($pluginlist as $plugin => $directory) { - $plugindirectory = $directory . '/classes/event'; - foreach (self::get_file_list($plugindirectory) as $eventname => $notused) { - $fullpluginname = $plugintype . '_' . $plugin; - $plugineventname = '\\' . $fullpluginname . '\\event\\' . $eventname; - // Check that this is actually an event. - if (method_exists($plugineventname, 'get_static_info') && $fullpluginname !== 'tool_monitor') { // No selfie here. - $ref = new \ReflectionClass($plugineventname); - if (!$ref->isAbstract() && $fullpluginname !== 'logstore_legacy') { - if ($withoutcomponent) { - $noncorepluginlist[$plugineventname] = $plugineventname::get_name_with_info(); - } else { - $noncorepluginlist[$fullpluginname][$plugineventname] = $plugineventname::get_name_with_info(); - } - } - } - } - } - } - - // Now enable developer debugging as event information has been retrieved. - $CFG->debug = $debuglevel; - $CFG->debugdisplay = $debugdisplay; - $CFG->debugdeveloper = $debugdeveloper; - - return $noncorepluginlist; - } - - /** - * Returns a list of files with a full directory path in a specified directory. - * - * @param string $directory location of files. - * @return array full location of files from the specified directory. - */ - protected static function get_file_list($directory) { - global $CFG; - $directoryroot = $CFG->dirroot; - $finalfiles = array(); - if (is_dir($directory)) { - if ($handle = opendir($directory)) { - $files = scandir($directory); - foreach ($files as $file) { - if ($file != '.' && $file != '..') { - // Ignore the file if it is external to the system. - if (strrpos($directory, $directoryroot) !== false) { - $location = substr($directory, strlen($directoryroot)); - $name = substr($file, 0, -4); - $finalfiles[$name] = $location . '/' . $file; - } - } - } - } - } - return $finalfiles; - } /** * Get a list of events present in the system. @@ -164,13 +38,58 @@ class eventlist { * @return array list of events present in the system. */ public static function get_all_eventlist($withoutcomponent = false) { - if ($withoutcomponent) { - $return = array_merge(self::get_core_eventlist(), self::get_non_core_eventlist($withoutcomponent)); - array_multisort($return, SORT_NATURAL); - } else { - $return = array_merge(array('core' => self::get_core_eventlist()), - self::get_non_core_eventlist($withoutcomponent = false)); + global $CFG; + + // Disable developer debugging as deprecated events will fire warnings. + // Setup backup variables to restore the following settings back to what they were when we are finished. + $debuglevel = $CFG->debug; + $debugdisplay = $CFG->debugdisplay; + $debugdeveloper = $CFG->debugdeveloper; + $CFG->debug = 0; + $CFG->debugdisplay = false; + $CFG->debugdeveloper = false; + + // List of exceptional events that will cause problems if displayed. + $eventsignore = [ + \core\event\unknown_logged::class, + \logstore_legacy\event\legacy_logged::class, + ]; + + $return = []; + + $events = core_component::get_component_classes_in_namespace(null, 'event'); + foreach (array_keys($events) as $event) { + // We need to filter all classes that extend event base, or the base class itself. + if (is_a($event, \core\event\base::class, true) && !in_array($event, $eventsignore)) { + + $reflectionclass = new ReflectionClass($event); + if ($reflectionclass->isAbstract()) { + continue; + } + + // We can't choose this component's own events. + [$component] = explode('\\', $event); + if ($component === 'tool_monitor') { + continue; + } + + if ($withoutcomponent) { + $return["\\{$event}"] = $event::get_name_with_info(); + } else { + $return[$component]["\\{$event}"] = $event::get_name_with_info(); + } + } } + + // Now enable developer debugging as event information has been retrieved. + $CFG->debug = $debuglevel; + $CFG->debugdisplay = $debugdisplay; + $CFG->debugdeveloper = $debugdeveloper; + + if ($withoutcomponent) { + array_multisort($return, SORT_NATURAL); + } + return $return; }