MDL-31989 component: Allow classes to be retrieved by namespace

This commit is contained in:
David Monllao
2016-02-23 10:47:58 +00:00
committed by Dan Poltawski
parent db48207e1a
commit f900b2b6c9
2 changed files with 53 additions and 1 deletions
+35 -1
View File
@@ -397,7 +397,7 @@ $cache = '.var_export($cache, true).';
'repository' => $CFG->dirroot.'/repository',
'rss' => $CFG->dirroot.'/rss',
'role' => $CFG->dirroot.'/'.$CFG->admin.'/roles',
'search' => null,
'search' => $CFG->dirroot.'/search',
'table' => null,
'tag' => $CFG->dirroot.'/tag',
'timezones' => null,
@@ -439,6 +439,7 @@ $cache = '.var_export($cache, true).';
'webservice' => $CFG->dirroot.'/webservice',
'repository' => $CFG->dirroot.'/repository',
'portfolio' => $CFG->dirroot.'/portfolio',
'search' => $CFG->dirroot.'/search/engine',
'qbehaviour' => $CFG->dirroot.'/question/behaviour',
'qformat' => $CFG->dirroot.'/question/format',
'plagiarism' => $CFG->dirroot.'/plagiarism',
@@ -897,6 +898,39 @@ $cache = '.var_export($cache, true).';
return $pluginfiles;
}
/**
* Returns all classes in a component matching the provided namespace.
*
* It checks that the class exists.
*
* e.g. get_component_classes_in_namespace('mod_forum', 'event')
*
* @param string $component A valid moodle component (frankenstyle)
* @param string $namespace Namespace from the component name.
* @return array The full class name as key and the class path as value.
*/
public static function get_component_classes_in_namespace($component, $namespace = '') {
// We will add them later.
$namespace = ltrim($namespace, '\\');
// We need add double backslashes as it is how classes are stored into self::$classmap.
$namespace = implode('\\\\', explode('\\', $namespace));
$regex = '/^' . $component . '\\\\' . $namespace . '/';
$it = new RegexIterator(new ArrayIterator(self::$classmap), $regex, RegexIterator::GET_MATCH, RegexIterator::USE_KEY);
// We want to be sure that they exist.
$classes = array();
foreach ($it as $classname => $classpath) {
if (class_exists($classname)) {
$classes[$classname] = $classpath;
}
}
return $classes;
}
/**
* Returns the exact absolute path to plugin directory.
*