]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Refs #4933: cache in plugin constructor, remove $CONFIG
authorSteve Clay <steve@mrclay.org>
Mon, 26 Nov 2012 22:31:53 +0000 (17:31 -0500)
committerSteve Clay <steve@mrclay.org>
Mon, 26 Nov 2012 22:31:53 +0000 (17:31 -0500)
engine/classes/ElggPlugin.php
engine/lib/plugins.php

index 3e43c8e816aef962f5d4f11c45da3447588c1c74..c6ce2905f4207ca010deb76a35c45d153392c8c9 100644 (file)
@@ -36,8 +36,9 @@ class ElggPlugin extends ElggObject {
         * @warning Unlike other ElggEntity objects, you cannot null instantiate
         *          ElggPlugin. You must point it to an actual plugin GUID or location.
         *
-        * @param mixed $plugin The GUID of the ElggPlugin object or the path of
-        *                      the plugin to load.
+        * @param mixed $plugin The GUID of the ElggPlugin object or the path of the plugin to load.
+        *
+        * @throws PluginException
         */
        public function __construct($plugin) {
                if (!$plugin) {
@@ -76,6 +77,8 @@ class ElggPlugin extends ElggObject {
                        // load the rest of the plugin
                        parent::__construct($existing_guid);
                }
+
+               _elgg_cache_plugin_by_id($this);
        }
 
        /**
index 127d4bf8e97ba76472819875943087e9af606e24..ca4a957f487672804bab76a4e4c76128051bbed1 100644 (file)
@@ -107,7 +107,6 @@ function elgg_generate_plugin_entities() {
        $old_access = access_get_show_hidden_status();
        access_show_hidden_entities(true);
        $known_plugins = elgg_get_entities_from_relationship($options);
-       _elgg_add_plugin_ids_mapping($known_plugins);
 
        if (!$known_plugins) {
                $known_plugins = array();
@@ -152,7 +151,6 @@ function elgg_generate_plugin_entities() {
                        // priority is force to last in save() if not set.
                        $plugin = new ElggPlugin($plugin_id);
                        $plugin->save();
-                       _elgg_add_plugin_ids_mapping(array($plugin));
                }
        }
 
@@ -178,22 +176,16 @@ function elgg_generate_plugin_entities() {
 }
 
 /**
- * Registers given plugins in plugin_id to entity mapping data structure
+ * Cache a reference to this plugin by its ID
  * 
- * @param array $plugins ElggPlugin instances to be registered 
+ * @param ElggPlugin $plugin
  * 
  * @access private
  */
-function _elgg_add_plugin_ids_mapping($plugins) {
-       global $CONFIG;
-       if (!is_array($CONFIG->plugin_ids_mapping)) {
-               $CONFIG->plugin_ids_mapping = array();
-       }
-       foreach ($plugins as $plugin) {
-               if ($plugin instanceof ElggPlugin) {
-                       $CONFIG->plugin_ids_mapping[$plugin->getID()] = $plugin;
-               }
-       }
+function _elgg_cache_plugin_by_id(ElggPlugin $plugin) {
+       $map = (array) elgg_get_config('plugins_by_id_map');
+       $map[$plugin->getID()] = $plugin;
+       elgg_set_config('plugins_by_id_map', $map);
 }
 
 /**
@@ -204,30 +196,29 @@ function _elgg_add_plugin_ids_mapping($plugins) {
  * @since 1.8.0
  */
 function elgg_get_plugin_from_id($plugin_id) {
-       global $CONFIG;
-       if (isset($CONFIG->plugin_ids_mapping[$plugin_id])) {
-               return $CONFIG->plugin_ids_mapping[$plugin_id];
-       } else {
-               $plugin_id = sanitize_string($plugin_id);
-               $db_prefix = get_config('dbprefix');
-       
-               $options = array(
-                       'type' => 'object',
-                       'subtype' => 'plugin',
-                       'joins' => array("JOIN {$db_prefix}objects_entity oe on oe.guid = e.guid"),
-                       'wheres' => array("oe.title = '$plugin_id'"),
-                       'limit' => 1
-               );
-       
-               $plugins = elgg_get_entities($options);
-       
-               if ($plugins) {
-                       _elgg_add_plugin_ids_mapping($plugins);
-                       return $plugins[0];
-               }
-       
-               return false;
+       $map = (array) elgg_get_config('plugins_by_id_map');
+       if (isset($map[$plugin_id])) {
+               return $map[$plugin_id];
+       }
+
+       $plugin_id = sanitize_string($plugin_id);
+       $db_prefix = get_config('dbprefix');
+
+       $options = array(
+               'type' => 'object',
+               'subtype' => 'plugin',
+               'joins' => array("JOIN {$db_prefix}objects_entity oe on oe.guid = e.guid"),
+               'wheres' => array("oe.title = '$plugin_id'"),
+               'limit' => 1
+       );
+
+       $plugins = elgg_get_entities($options);
+
+       if ($plugins) {
+               return $plugins[0];
        }
+
+       return false;
 }
 
 /**
@@ -417,7 +408,6 @@ function elgg_get_plugins($status = 'active', $site_guid = null) {
 
        $old_ia = elgg_set_ignore_access(true);
        $plugins = elgg_get_entities_from_relationship($options);
-       _elgg_add_plugin_ids_mapping($plugins);
        elgg_set_ignore_access($old_ia);
 
        return $plugins;
@@ -540,6 +530,8 @@ function elgg_namespace_plugin_private_setting($type, $name, $id = null) {
  * @return string|false Plugin name, or false if no plugin name was called
  * @since 1.8.0
  * @access private
+ *
+ * @todo get rid of this
  */
 function elgg_get_calling_plugin_id($mainfilename = false) {
        if (!$mainfilename) {
@@ -948,6 +940,7 @@ function elgg_set_plugin_setting($name, $value, $plugin_id = null) {
  *
  * @return mixed
  * @since 1.8.0
+ * @todo make $plugin_id required in future version
  */
 function elgg_get_plugin_setting($name, $plugin_id = null) {
        if ($plugin_id) {