diff --git a/lib/classes/component.php b/lib/classes/component.php index 9284f7d5a01..11ce5a6d552 100644 --- a/lib/classes/component.php +++ b/lib/classes/component.php @@ -1278,4 +1278,31 @@ $cache = '.var_export($cache, true).'; } return $components; } + + /** + * Returns a list of frankenstyle component names. + * + * E.g. + * [ + * 'core_course', + * 'core_message', + * 'mod_assign', + * ... + * ] + * @return array the list of frankenstyle component names. + */ + public static function get_component_names() : array { + $componentnames = []; + // Get all plugins. + foreach (self::get_plugin_types() as $plugintype => $typedir) { + foreach (self::get_plugin_list($plugintype) as $pluginname => $plugindir) { + $componentnames[] = $plugintype . '_' . $pluginname; + } + } + // Get all subsystems. + foreach (self::get_core_subsystems() as $subsystemname => $subsystempath) { + $componentnames[] = 'core_' . $subsystemname; + } + return $componentnames; + } } diff --git a/lib/tests/component_test.php b/lib/tests/component_test.php index 625279d1797..2b79591082b 100644 --- a/lib/tests/component_test.php +++ b/lib/tests/component_test.php @@ -805,4 +805,31 @@ class core_component_testcase extends advanced_testcase { $this->assertEquals($componentslist['mod']['mod_forum'], $CFG->dirroot . '/mod/forum'); $this->assertEquals($componentslist['tool']['tool_usertours'], $CFG->dirroot . '/' . $CFG->admin . '/tool/usertours'); } + + /** + * Test the get_component_names() method. + */ + public function test_get_component_names() { + global $CFG; + $componentnames = \core_component::get_component_names(); + + // We should have an entry for each plugin type. + $plugintypes = \core_component::get_plugin_types(); + $numplugintypes = 0; + foreach ($plugintypes as $type => $typedir) { + foreach (\core_component::get_plugin_list($type) as $plugin) { + $numplugintypes++; + } + } + // And an entry for each core subsystem. + $numcomponents = $numplugintypes + count(\core_component::get_core_subsystems()); + + $this->assertEquals($numcomponents, count($componentnames)); + + // Check a few of the known plugin types to confirm their presence at their respective type index. + $this->assertContains('core_comment', $componentnames); + $this->assertContains('mod_forum', $componentnames); + $this->assertContains('tool_usertours', $componentnames); + $this->assertContains('core_favourites', $componentnames); + } }