MDL-41100 libraries: Moved get_plugin_list_with_file to core_component

This commit is contained in:
Frederic Massart
2013-08-09 11:56:48 +08:00
parent 50ff861263
commit d26ec8a598
4 changed files with 103 additions and 16 deletions
+76
View File
@@ -41,6 +41,10 @@ class core_component {
protected static $subsystems = null;
/** @var null list of all known classes that can be autoloaded */
protected static $classmap = null;
/** @var null list of some known files that can be included. */
protected static $filemap = null;
/** @var array list of the files to map. */
protected static $filestomap = array('lib.php', 'settings.php');
/**
* Class loader for Frankenstyle named classes in standard locations.
@@ -105,6 +109,7 @@ class core_component {
self::$plugins = $cache['plugins'];
self::$subsystems = $cache['subsystems'];
self::$classmap = $cache['classmap'];
self::$filemap = $cache['filemap'];
return;
}
@@ -138,6 +143,7 @@ class core_component {
self::$plugins = $cache['plugins'];
self::$subsystems = $cache['subsystems'];
self::$classmap = $cache['classmap'];
self::$filemap = $cache['filemap'];
return;
}
// Note: we do not verify $CFG->admin here intentionally,
@@ -223,6 +229,7 @@ class core_component {
'plugintypes' => self::$plugintypes,
'plugins' => self::$plugins,
'classmap' => self::$classmap,
'filemap' => self::$filemap,
);
return '<?php
@@ -244,6 +251,7 @@ $cache = '.var_export($cache, true).';
}
self::fill_classmap_cache();
self::fill_filemap_cache();
}
/**
@@ -508,6 +516,35 @@ $cache = '.var_export($cache, true).';
self::$classmap['collatorlib'] = "$CFG->dirroot/lib/classes/collator.php";
}
/**
* Fills up the cache defining what plugins have certain files.
*
* @see self::get_plugin_list_with_file
* @return void
*/
protected static function fill_filemap_cache() {
global $CFG;
self::$filemap = array();
foreach (self::$filestomap as $file) {
if (!isset(self::$filemap[$file])) {
self::$filemap[$file] = array();
}
foreach (self::$plugins as $plugintype => $plugins) {
if (!isset(self::$filemap[$file][$plugintype])) {
self::$filemap[$file][$plugintype] = array();
}
foreach ($plugins as $pluginname => $fulldir) {
if (file_exists("$fulldir/$file")) {
self::$filemap[$file][$plugintype][$pluginname] = "$fulldir/$file";
}
}
}
}
}
/**
* Find classes in directory and recurse to subdirs.
* @param string $component
@@ -647,6 +684,45 @@ $cache = '.var_export($cache, true).';
return $pluginclasses;
}
/**
* Get a list of all the plugins of a given type that contain a particular file.
*
* @param string $plugintype the type of plugin, e.g. 'mod' or 'report'.
* @param string $file the name of file that must be present in the plugin.
* (e.g. 'view.php', 'db/install.xml').
* @param bool $include if true (default false), the file will be include_once-ed if found.
* @return array with plugin name as keys (e.g. 'forum', 'courselist') and the path
* to the file relative to dirroot as value (e.g. "$CFG->dirroot/mod/forum/view.php").
*/
public static function get_plugin_list_with_file($plugintype, $file, $include = false) {
global $CFG; // Necessary in case it is referenced by included PHP scripts.
$pluginfiles = array();
if (isset(self::$filemap[$file])) {
// If the file was supposed to be mapped, then it should have been set in the array.
if (isset(self::$filemap[$file][$plugintype])) {
$pluginfiles = self::$filemap[$file][$plugintype];
}
} else {
// Old-style search for non-cached files.
$plugins = self::get_plugin_list($plugintype);
foreach ($plugins as $plugin => $fulldir) {
$path = $fulldir . '/' . $file;
if (file_exists($path)) {
$pluginfiles[$plugin] = $path;
}
}
}
if ($include) {
foreach ($pluginfiles as $path) {
include_once($path);
}
}
return $pluginfiles;
}
/**
* Returns the exact absolute path to plugin directory.
*
+1 -15
View File
@@ -8165,21 +8165,7 @@ function is_valid_plugin_name($name) {
* to the file relative to dirroot as value (e.g. "$CFG->dirroot/mod/forum/view.php").
*/
function get_plugin_list_with_file($plugintype, $file, $include = false) {
global $CFG; // Necessary in case it is referenced by include()d PHP scripts.
$plugins = array();
foreach (core_component::get_plugin_list($plugintype) as $plugin => $dir) {
$path = $dir . '/' . $file;
if (file_exists($path)) {
if ($include) {
include_once($path);
}
$plugins[$plugin] = $path;
}
}
return $plugins;
return core_component::get_plugin_list_with_file($plugintype, $file, $include);
}
/**
+25
View File
@@ -336,5 +336,30 @@ class core_component_testcase extends advanced_testcase {
);
$this->assertSame($expected, $types);
}
public function test_get_plugin_list_with_file() {
// Force the cache reset.
phpunit_util::reset_all_data();
$this->resetAfterTest(true);
$expected = array('completion', 'log', 'loglive', 'outline', 'participation', 'progress', 'stats');
// Test cold.
$list = core_component::get_plugin_list_with_file('report', 'lib.php', false);
$this->assertEquals($expected, array_keys($list));
// Test hot.
$list = core_component::get_plugin_list_with_file('report', 'lib.php', false);
$this->assertEquals($expected, array_keys($list));
// Test with include.
$list = core_component::get_plugin_list_with_file('report', 'lib.php', true);
$this->assertEquals($expected, array_keys($list));
// Test missing.
$list = core_component::get_plugin_list_with_file('report', 'idontexist.php', true);
$this->assertEquals(array(), array_keys($list));
}
}
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2013080800.00; // YYYYMMDD = weekly release date of this DEV branch
$version = 2013080900.00; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes