Skip to content

Commit

Permalink
Merge branch 'MDL-61875-master' of https://github.com/snake/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Apr 10, 2018
2 parents d25a8e3 + 6e6ba82 commit 7aeb857
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
34 changes: 34 additions & 0 deletions lib/classes/component.php
Original file line number Diff line number Diff line change
Expand Up @@ -1244,4 +1244,38 @@ protected static function load_renamed_classes($fulldir) {
}
}
}

/**
* Returns a list of frankenstyle component names and their paths, for all components (plugins and subsystems).
*
* E.g.
* [
* 'mod' => [
* 'mod_forum' => FORUM_PLUGIN_PATH,
* ...
* ],
* ...
* 'core' => [
* 'core_comment' => COMMENT_SUBSYSTEM_PATH,
* ...
* ]
* ]
*
* @return array an associative array of components and their corresponding paths.
*/
public static function get_component_list() : array {
$components = [];
// Get all plugins.
foreach (self::get_plugin_types() as $plugintype => $typedir) {
$components[$plugintype] = [];
foreach (self::get_plugin_list($plugintype) as $pluginname => $plugindir) {
$components[$plugintype][$plugintype . '_' . $pluginname] = $plugindir;
}
}
// Get all subsystems.
foreach (self::get_core_subsystems() as $subsystemname => $subsystempath) {
$components['core']['core_' . $subsystemname] = $subsystempath;
}
return $components;
}
}
26 changes: 26 additions & 0 deletions lib/tests/component_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,4 +779,30 @@ public function test_get_class_file($classname, $prefix, $path, $separators, $re
$file = $psrclassloader->invokeArgs(null, array($classname, $prefix, $path, $separators));
$this->assertEquals($result, $file);
}

/**
* Confirm the get_component_list method contains an entry for every component.
*/
public function test_get_component_list_contains_all_components() {
global $CFG;
$componentslist = \core_component::get_component_list();

// We should have an entry for each plugin type, and one additional for 'core'.
$plugintypes = \core_component::get_plugin_types();
$numelementsexpected = count($plugintypes) + 1;
$this->assertEquals($numelementsexpected, count($componentslist));

// And an entry for each of the plugin types.
foreach (array_keys($plugintypes) as $plugintype) {
$this->assertArrayHasKey($plugintype, $componentslist);
}

// And finally, one for 'core'.
$this->assertArrayHasKey('core', $componentslist);

// Check a few of the known plugin types to confirm their presence at their respective type index.
$this->assertEquals($componentslist['core']['core_comment'], $CFG->dirroot . '/comment');
$this->assertEquals($componentslist['mod']['mod_forum'], $CFG->dirroot . '/mod/forum');
$this->assertEquals($componentslist['tool']['tool_analytics'], $CFG->dirroot . '/admin/tool/analytics');
}
}
19 changes: 3 additions & 16 deletions privacy/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,9 @@ protected function component_is_core_provider($component) {
* @return array the array of frankenstyle component names.
*/
protected function get_component_list() {
$components = [];
// Get all plugins.
$plugintypes = \core_component::get_plugin_types();
foreach ($plugintypes as $plugintype => $typedir) {
$plugins = \core_component::get_plugin_list($plugintype);
foreach ($plugins as $pluginname => $plugindir) {
$components[] = $plugintype . '_' . $pluginname;
}
}
// Get all subsystems.
foreach (\core_component::get_core_subsystems() as $name => $path) {
if (isset($path)) {
$components[] = 'core_' . $name;
}
}
return $components;
return array_keys(array_reduce(\core_component::get_component_list(), function($carry, $item) {
return array_merge($carry, $item);
}, []));
}

/**
Expand Down

0 comments on commit 7aeb857

Please sign in to comment.