MDL-22015 more advances in frankenstyle usage needed for new amos_string_manager
authorPetr Skoda <skodak@moodle.org>
Thu, 1 Apr 2010 20:54:27 +0000 (20:54 +0000)
committerPetr Skoda <skodak@moodle.org>
Thu, 1 Apr 2010 20:54:27 +0000 (20:54 +0000)
lib/moodlelib.php
lib/outputfactories.php
lib/outputrequirementslib.php

index 982069a..95d5384 100644 (file)
@@ -6676,7 +6676,7 @@ function get_plugin_directory($plugintype, $name, $fullpaths=true) {
  * this method support "simpletest_" prefix designed for unit testing.
  * @param string $component name such as 'moodle', 'mod_forum' or special simpletest value
  * @param bool $fullpaths false means relative paths from dirroot
- * @return directory path, full or relative to dirroot
+ * @return directory path, full or relative to dirroot; NULL if not found
  */
 function get_component_directory($component, $fullpaths=true) {
     global $CFG;
@@ -6686,21 +6686,124 @@ function get_component_directory($component, $fullpaths=true) {
         $subdir = substr($component, strlen('simpletest_'));
         return $subdir;
     }
-    if ($component == 'moodle') {
-        $path = ($fullpaths ? $CFG->libdir : 'lib');
-    } else {
-        if (strpos($component, '_') === false) {
-            $type   = 'mod';
-            $plugin = $component;
+
+    list($type, $plugin) = normalize_component($component);
+
+    if ($type === 'core') {
+        if ($plugin === NULL ) {
+            $path = ($fullpaths ? $CFG->libdir : 'lib');
         } else {
-            list($type, $plugin) = explode('_', $component, 2);
+            $subsystems = get_core_subsystems();
+            if (isset($subsystems[$plugin])) {
+                $path = ($fullpaths ? $CFG->dirroot.'/'.$subsystems[$plugin] : $subsystems[$plugin]);
+            } else {
+                $path = NULL;
+            }
         }
+
+    } else {
         $path = get_plugin_directory($type, $plugin, $fullpaths);
     }
 
     return $path;
 }
 
+/**
+ * Normalize the component name using the "frankenstyle" names.
+ * @param string $component
+ * @return array $type+$plugin elements
+ */
+function normalize_component($component) {
+    if ($component === 'moodle' or $component === 'core') {
+        $type = 'core';
+        $plugin = NULL;
+
+    } else if (strpos($component, '_') === false) {
+        $subsystems = get_core_subsystems();
+        if (array_key_exists($component, $subsystems)) {
+            $type   = 'core';
+            $plugin = $component;
+        } else {
+            // everything else is a module
+            $type   = 'mod';
+            $plugin = $component;
+        }
+
+    } else {
+        list($type, $plugin) = explode('_', $component, 2);
+    }
+
+    return array($type, $plugin);
+}
+
+/**
+ * List all core subsystems, this is especially useful for get_string()
+ * and output renderers. The relative location is not always included.
+ *
+ * @return array of strings - name=>location
+ */
+function get_core_subsystems() {
+    global $CFG;
+
+    static $info     = null;
+
+    if (!$info) {
+        $info = array(
+            'access'      => NULL,
+            'admin'       => 'admin',
+            'auth'        => 'auth',
+            'block'       => 'blocks',
+            'blog'        => 'blog',
+            'bulkusers'   => NULL,
+            'calendar'    => 'calendar',
+            'condition'   => NULL,
+            'completion'  => NULL,
+            'countries'   => NULL,
+            'currencies'  => NULL,
+            'dbtransfer'  => NULL,
+            'debug'       => NULL,
+            'dock'        => NULL,
+            'editor'      => 'lib/editor',
+            'error'       => NULL,
+            'filepicker'  => NULL,
+            'filters'     => NULL,
+            'flashdetect' => NULL,
+            'fonts'       => NULL,
+            'form'        => 'lib/form',
+            'grades'      => 'grade',
+            'group'       => 'group',
+            'help'        => NULL,
+            'imscc'       => NULL,
+            'install'     => NULL,
+            'langconfig'  => NULL,
+            'license'     => NULL,
+            'message'     => 'message',
+            'mimetypes'   => NULL,
+            'mnet'        => 'mnet',
+            'moodle.org'  => NULL, // the dot is nasty, watch out! should be renamed to moodleorg
+            'my'          => 'my',
+            'notes'       => 'notes',
+            'pagetype'    => NULL,
+            'pix'         => NULL,
+            'portfolio'   => 'portfolio',
+            'question'    => 'question',
+            'rating'      => 'rating',
+            'repository'  => 'repository',
+            'role'        => $CFG->admin.'/role',
+            'simpletest'  => NULL,
+            'search'      => 'search',
+            'table'       => NULL,
+            'tag'         => 'tag',
+            'timezones'   => NULL,
+            'userkey'     => NULL,
+            'webservice'  => 'webservice',
+            'xmldb'       => NULL,
+        );
+    }
+
+    return $info;
+}
+
 /**
  * Lists all plugin types
  * @param bool $fullpaths false means relative paths from dirroot
index 4329172..aebe7a2 100644 (file)
@@ -157,11 +157,15 @@ abstract class renderer_factory_base implements renderer_factory {
     protected function standard_renderer_classname($component, $subtype = null) {
         global $CFG; // needed in incldued files
 
+        // standardize component name ala frankenstyle
+        list($plugin, $type) = normalize_component($component);
+        if ($type === null) {
+            $component = $plugin;
+        } else {
+            $component = $plugin.'_'.$type;
+        }
+
         if ($component !== 'core') {
-            // standardize component names
-            if (strpos($component, '_') === false) {
-                $component = 'mod_' . $component;
-            }
             // renderers are stored in renderer.php files
             if (!$compdirectory = get_component_directory($component)) {
                 throw new coding_exception('Invalid component specified in renderer request');
index 20924bd..d42efa4 100644 (file)
@@ -331,19 +331,19 @@ class page_requirements_manager {
 
     /**
      * Find out if JS modulke present and return details.
-     * @param string $name name of module, ex: core_group, mod_forum
+     * @param string $component name of component in frankenstyle, ex: core_group, mod_forum
      * @return array description of module or null if not found
      */
-    protected function find_module($name) {
+    protected function find_module($component) {
         global $CFG;
 
         $module = null;
 
         
-        if (strpos($name, 'core_') === 0) {
+        if (strpos($component, 'core_') === 0) {
             // must be some core stuff - list here is not complete, this is just the stuff used from multiple places
             // so that we do nto have to repeat the definition of these modules over and over again
-            switch($name) {
+            switch($component) {
                 case 'core_filepicker':
                     $module = array('name'     => 'core_filepicker',
                                     'fullpath' => '/repository/filepicker.js',
@@ -408,9 +408,9 @@ class page_requirements_manager {
             }
 
         } else {
-            if ($dir = get_component_directory($name, false)) {
+            if ($dir = get_component_directory($component, false)) {
                 if (file_exists("$CFG->dirroot/$dir/module.js")) {
-                    $module = array('name'=>$name, 'fullpath'=>"/$dir/module.js", 'requires' => array());
+                    $module = array('name'=>$component, 'fullpath'=>"/$dir/module.js", 'requires' => array());
                 }
             }
         }