MDL-63658 core: new method get_component_names() added to core

This commit is contained in:
Jake Dallimore 2018-10-17 17:12:27 +08:00
parent b7a3ec6f8d
commit 3a1ece149f
2 changed files with 54 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}