Vanilla\AddonManager::stopAddonsByKey PHP Method

stopAddonsByKey() public method

Stop one or more addons by specifying their keys.
public stopAddonsByKey ( array $keys, string $type ) : integer
$keys array The keys of the addons. The addon keys can be the keys of the array or the values.
$type string One of the **Addon::TYPE_*** constants.
return integer Returns the number of addons that were stopped.
    public function stopAddonsByKey($keys, $type)
    {
        // Filter out false keys.
        $keys = array_filter((array) $keys);
        $count = 0;
        foreach ($keys as $key => $value) {
            if (in_array($value, [true, 1, '1'], true)) {
                // This addon key is represented as addon => true.
                $addon = $this->lookupByType($key, $type);
            } else {
                // This addon is represented as addon => folder.
                $addon = $this->lookupByType($value, $type);
            }
            if (empty($addon)) {
                trigger_error("The {$type} with key {$key} could not be found and will not be stopped.");
            } else {
                $this->stopAddon($addon);
                $count++;
            }
        }
        return $count;
    }

Usage Example

 /**
  * Disable an application.
  *
  * @param string $applicationName The name of the application to disable.
  * @throws \Exception Throws an exception if the application can't be disabled.
  */
 public function disableApplication($applicationName)
 {
     $addon = $this->addonManager->lookupAddon($applicationName);
     if (!$addon) {
         throw notFoundException('Application');
     }
     $applicationName = $addon->getRawKey();
     // 1. Check to make sure that this application is allowed to be disabled
     if (!$addon->getInfoValue('allowDisable', true)) {
         throw new Exception(sprintf(t('You cannot disable the %s application.'), $applicationName));
     }
     // 2. Check to make sure that no other enabled applications rely on this one.
     try {
         $this->addonManager->checkDependants($addon, true);
     } catch (Exception $ex) {
         throw new Gdn_UserException($ex->getMessage(), $ex->getCode());
     }
     // 2. Disable it
     removeFromConfig("EnabledApplications.{$applicationName}");
     Logger::event('addon_disabled', Logger::NOTICE, 'The {addonName} application was disabled.', array('addonName' => $applicationName));
     // Clear the object caches.
     $this->addonManager->stopAddonsByKey([$applicationName], \Vanilla\Addon::TYPE_ADDON);
 }