Browse files

MDL-39087 Add new helper methods to the plugin_manager API

These are mainly intended for callers that had to iterate over
get_plugins() result manually.
  • Loading branch information...
1 parent c6f4c88 commit d7d48b40910092f0e4f1e7851980776c5e5cf8d9 @mudrd8mz mudrd8mz committed Apr 11, 2013
Showing with 92 additions and 10 deletions.
  1. +1 −2 admin/courseformats.php
  2. +2 −4 lib/adminlib.php
  3. +1 −2 lib/editor/tinymce/settings.php
  4. +61 −2 lib/pluginlib.php
  5. +25 −0 lib/tests/pluginlib_test.php
  6. +2 −0 lib/upgrade.txt
View
3 admin/courseformats.php
@@ -40,8 +40,7 @@
$return = new moodle_url('/admin/settings.php', array('section' => 'manageformats'));
-$allplugins = plugin_manager::instance()->get_plugins();
-$formatplugins = $allplugins['format'];
+$formatplugins = plugin_manager::instance()->get_plugins_of_type('format');
$sortorder = array_flip(array_keys($formatplugins));
if (!isset($formatplugins[$formatname])) {
View
6 lib/adminlib.php
@@ -6071,8 +6071,7 @@ public function is_related($query) {
if (parent::is_related($query)) {
return true;
}
- $allplugins = plugin_manager::instance()->get_plugins();
- $formats = $allplugins['format'];
+ $formats = plugin_manager::instance()->get_plugins_of_type('format');
foreach ($formats as $format) {
if (strpos($format->component, $query) !== false ||
strpos(textlib::strtolower($format->displayname), $query) !== false) {
@@ -6095,8 +6094,7 @@ public function output_html($data, $query='') {
$return = $OUTPUT->heading(new lang_string('courseformats'), 3, 'main');
$return .= $OUTPUT->box_start('generalbox formatsui');
- $allplugins = plugin_manager::instance()->get_plugins();
- $formats = $allplugins['format'];
+ $formats = plugin_manager::instance()->get_plugins_of_type('format');
// display strings
$txt = get_strings(array('settings', 'name', 'enable', 'disable', 'up', 'down', 'default', 'delete'));
View
3 lib/editor/tinymce/settings.php
@@ -48,8 +48,7 @@
unset($settings);
require_once("$CFG->libdir/pluginlib.php");
-$allplugins = plugin_manager::instance()->get_plugins();
-foreach ($allplugins['tinymce'] as $plugin) {
+foreach (plugin_manager::instance()->get_plugins_of_type('tinymce') as $plugin) {
$plugin->load_settings($ADMIN, 'editortinymce', $hassiteconfig);
}
View
63 lib/pluginlib.php
@@ -108,6 +108,27 @@ public function get_plugin_types($fullpaths = true) {
}
/**
+ * Returns list of known plugins of the given type
+ *
+ * This method returns the subset of the tree returned by {@link self::get_plugins()}.
+ * If the given type is not known, empty array is returned.
+ *
+ * @param string $type plugin type, e.g. 'mod' or 'workshopallocation'
+ * @param bool $disablecache force reload, cache can be used otherwise
+ * @return array (string)plugin name (e.g. 'workshop') => corresponding subclass of {@link plugininfo_base}
+ */
+ public function get_plugins_of_type($type, $disablecache=false) {
+
+ $plugins = $this->get_plugins($disablecache);
+
+ if (!isset($plugins[$type])) {
+ return array();
+ }
+
+ return $plugins[$type];
+ }
+
+ /**
* Returns a tree of known plugins and information about them
*
* @param bool $disablecache force reload, cache can be used otherwise
@@ -162,6 +183,41 @@ public function get_plugins($disablecache=false) {
}
/**
+ * Returns list of all known subplugins of the given plugin
+ *
+ * For plugins that do not provide subplugins (i.e. there is no support for it),
+ * empty array is returned.
+ *
+ * @param string $component full component name, e.g. 'mod_workshop'
+ * @param bool $disablecache force reload, cache can be used otherwise
+ * @return array (string) component name (e.g. 'workshopallocation_random') => subclass of {@link plugininfo_base}
+ */
+ public function get_subplugins_of_plugin($component, $disablecache=false) {
+
+ $pluginfo = $this->get_plugin_info($component, $disablecache);
+
+ if (is_null($pluginfo)) {
+ return array();
+ }
+
+ $subplugins = $this->get_subplugins($disablecache);
+
+ if (!isset($subplugins[$pluginfo->component])) {
+ return array();
+ }
+
+ $list = array();
+
+ foreach ($subplugins[$pluginfo->component] as $subdata) {
+ foreach ($this->get_plugins_of_type($subdata->type) as $subpluginfo) {
+ $list[$subpluginfo->component] = $subpluginfo;
+ }
+ }
+
+ return $list;
+ }
+
+ /**
* Returns list of plugins that define their subplugins and the information
* about them from the db/subplugins.php file.
*
@@ -294,12 +350,15 @@ public function plugintype_name_plural($type) {
}
/**
+ * Returns information about the known plugin, or null
+ *
* @param string $component frankenstyle component name.
+ * @param bool $disablecache force reload, cache can be used otherwise
* @return plugininfo_base|null the corresponding plugin information.
*/
- public function get_plugin_info($component) {
+ public function get_plugin_info($component, $disablecache=false) {
list($type, $name) = $this->normalize_component($component);
- $plugins = $this->get_plugins();
+ $plugins = $this->get_plugins($disablecache);
if (isset($plugins[$type][$name])) {
return $plugins[$type][$name];
} else {
View
25 lib/tests/pluginlib_test.php
@@ -49,9 +49,24 @@ public function test_plugin_manager_instance() {
$this->assertTrue($pluginman instanceof testable_plugin_manager);
}
+ public function test_get_plugins_of_type() {
+ $pluginman = testable_plugin_manager::instance();
+ $mods = $pluginman->get_plugins_of_type('mod');
+ $this->assertEquals('array', gettype($mods));
+ $this->assertEquals(2, count($mods));
+ $this->assertTrue($mods['foo'] instanceof testable_plugininfo_mod);
+ $this->assertTrue($mods['bar'] instanceof testable_plugininfo_mod);
+ $foolishes = $pluginman->get_plugins_of_type('foolish');
+ $this->assertEquals(1, count($foolishes));
+ $this->assertTrue($foolishes['frog'] instanceof testable_pluginfo_foolish);
+ $unknown = $pluginman->get_plugins_of_type('muhehe');
+ $this->assertSame(array(), $unknown);
+ }
+
public function test_get_plugins() {
$pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins();
+ $this->assertEquals('array', gettype($plugins));
$this->assertTrue(isset($plugins['mod']['foo']));
$this->assertTrue(isset($plugins['mod']['bar']));
$this->assertTrue(isset($plugins['foolish']['frog']));
@@ -60,6 +75,16 @@ public function test_get_plugins() {
$this->assertTrue($plugins['foolish']['frog'] instanceof testable_pluginfo_foolish);
}
+ public function test_get_subplugins_of_plugin() {
+ $pluginman = testable_plugin_manager::instance();
+ $this->assertSame(array(), $pluginman->get_subplugins_of_plugin('mod_missing'));
+ $this->assertSame(array(), $pluginman->get_subplugins_of_plugin('mod_bar'));
+ $foosubs = $pluginman->get_subplugins_of_plugin('mod_foo');
+ $this->assertEquals('array', gettype($foosubs));
+ $this->assertEquals(1, count($foosubs));
+ $this->assertTrue($foosubs['foolish_frog'] instanceof testable_pluginfo_foolish);
+ }
+
public function test_get_subplugins() {
$pluginman = testable_plugin_manager::instance();
$subplugins = $pluginman->get_subplugins();
View
2 lib/upgrade.txt
@@ -51,6 +51,8 @@ information provided here is intended especially for developers.
* Additional (optional) param $onlyactive has been added to get_enrolled_users, count_enrolled_users
functions to get information for only active (excluding suspended enrolments) users. Included two
helper functions extract_suspended_users, get_suspended_userids to extract suspended user information.
+* The plugin_manager class now provides two new helper methods for getting information
+ about known plugins: get_plugins_of_type() and get_subplugins_of_plugin().
Database (DML) layer:
* $DB->sql_empty() is deprecated, you have to use sql parameters with empty values instead,

0 comments on commit d7d48b4

Please sign in to comment.