Implemented [Suggestion] Is AddOn Installed?

Uniphix

Active member
I really think there is a lack of code checks, for example I am having to modify XenFore Core for a lot of functions that a client needs including the default forums. So therefore I have created major AddOns that sometimes requires other addons to be installed before they are functional.

So here is a mockup code I created that works pretty good and that has cache support.

PHP:
<?php 
 
class XenForo_AddOn 
{
     /**
      * @var XenForo_Model_AddOn
      */
     protected static $_addonModelCache = null;
     
     /**
      * Gets a list of all installed AddOns
      * @var array
      */
     protected static $_addOnInstalledCache = array();
     
     protected static function _getAddonModel(){
          if( self::$_addonModelCache == null ){
               self::$_addonModelCache = XenForo_Model::create('XenForo_Model_AddOn');
          }
          return self::$_addonModelCache;
     }
     
     public static function isAddonInstalled($addOnId){
          $addOnModel = self::_getAddonModel();
          if( !array_key_exists($addOnId, self::$_addOnInstalledCache) ){
               if( $addOn = $addOnModel->getAddOnById($addOnId) ){
                    self::$_addOnInstalledCache[$addOnId] = $addOn['active'];
               } else {
                    self::$_addOnInstalledCache[$addOnId] = false;
               }
          }
          return self::$_addOnInstalledCache[$addOnId];
     }
     
     public static function clearCache(){
          self::$_addOnInstalledCache = array();
     }
     
     public static function changeAddOnState($oldAddOnId, $newAddOnId, $state){
          if( $newAddOnId == null ){
               self::$_addOnInstalledCache[$oldAddOnId] = $state;     
               return;
          }
          if( array_key_exists($oldAddOnId, self::$_addonModelCache) ){
               unset(self::$_addOnInstalledCache[$oldAddOnId]);
          }
          self::$_addOnInstalledCache[$newAddOnId] = $state;          
     }
}

Inside the XenForo_Model_AddOn i just do XenForo_AddOn::clearCache(); on the rebuildAddOnCaches() which basically helps to rebuild the cache to determine if an addOn is active or not. Then I have another method that I add within the XenForo_DataWriter_AddOn when the active field changes to update the cache...

Edit: Fixed some code issues above
 
Upvote 0
This suggestion has been implemented. Votes are no longer accepted.
Implemented in xf 1.2:)

All active addons are available in the simpleCache (key = addOns)
Thank you for this :)

It is actually in the registry. To get active addons:
PHP:
        $activeAddons = XenForo_Model::create('XenForo_Model_DataRegistry')->get('addOns');
         $myAddonEnable = (!empty($activeAddons['myAddonId'])) ? true : false;
         $myAddonVersion = ($myAddonEnable) ? $activeAddons['myAddonId'] : false;
 
Thank you for this :)

It is actually in the registry. To get active addons:
PHP:
        $activeAddons = XenForo_Model::create('XenForo_Model_DataRegistry')->get('addOns');
         $myAddonEnable = (!empty($activeAddons['myAddonId'])) ? true : false;
         $myAddonVersion = ($myAddonEnable) ? $activeAddons['myAddonId'] : false;
You can also use
PHP:
XenForo_Application::get('addOns');

no need for
XenForo_Model::create('XenForo_Model_DataRegistry')->get('addOns')
 
Top Bottom