1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Implemented [Suggestion] Is AddOn Installed?

Discussion in 'Closed Suggestions' started by Uniphix, Jan 12, 2013.

  1. Uniphix

    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($addOnIdself::$_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($oldAddOnIdself::$_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
     
  2. xf_phantom

    xf_phantom Well-Known Member

    Implemented in xf 1.2:)

    All active addons are available in the simpleCache (key = addOns)
     
    cclaerhout and Jake Bunce like this.
  3. Uniphix

    Uniphix Active Member

    Awesome glad to hear this...
     
  4. cclaerhout

    cclaerhout Well-Known Member

    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;
     
    Pereira likes this.
  5. Dan

    Dan Well-Known Member

    You can also use
    PHP:
    XenForo_Application::get('addOns');
    no need for
    XenForo_Model::create('XenForo_Model_DataRegistry')->get('addOns')
     
    cclaerhout likes this.

Share This Page