Grav\Plugin\Admin\Gpm::uninstall PHP Method

uninstall() public static method

public static uninstall ( Grav\Common\GPM\Common\Package[] | string[] | string $packages, array $options ) : boolean
$packages Grav\Common\GPM\Common\Package[] | string[] | string
$options array
return boolean
    public static function uninstall($packages, array $options)
    {
        $options = array_merge(self::$options, $options);
        $packages = is_array($packages) ? $packages : [$packages];
        $count = count($packages);
        $packages = array_filter(array_map(function ($p) {
            if (is_string($p)) {
                $p = strtolower($p);
                $plugin = static::GPM()->getInstalledPlugin($p);
                $p = $plugin ?: static::GPM()->getInstalledTheme($p);
            }
            return $p instanceof Package ? $p : false;
        }, $packages));
        if (!$options['skip_invalid'] && $count !== count($packages)) {
            return false;
        }
        foreach ($packages as $package) {
            $location = Grav::instance()['locator']->findResource($package->package_type . '://' . $package->slug);
            // Check destination
            Installer::isValidDestination($location);
            if (Installer::lastErrorCode() === Installer::IS_LINK && !$options['ignore_symlinks']) {
                return false;
            }
            Installer::uninstall($location);
            $errorCode = Installer::lastErrorCode();
            if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) {
                $msg = Installer::lastErrorMsg();
                throw new \RuntimeException($msg);
            }
            if (count($packages) == 1) {
                $message = Installer::getMessage();
                if ($message) {
                    return $message;
                }
            }
        }
        return true;
    }

Usage Example

 /**
  * Handle removing a package
  *
  * @return bool
  */
 protected function taskRemovePackage()
 {
     $data = $this->post;
     $package = isset($data['package']) ? $data['package'] : '';
     $type = isset($data['type']) ? $data['type'] : '';
     if (!$this->authorizeTask('uninstall ' . $type, ['admin.' . $type, 'admin.super'])) {
         $this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.INSUFFICIENT_PERMISSIONS_FOR_TASK')];
         return false;
     }
     //check if there are packages that have this as a dependency. Abort and show which ones
     $dependent_packages = $this->admin->getPackagesThatDependOnPackage($package);
     if (count($dependent_packages) > 0) {
         if (count($dependent_packages) > 1) {
             $message = "The installed packages <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove those first.";
         } else {
             $message = "The installed package <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove it first.";
         }
         $this->admin->json_response = ['status' => 'error', 'message' => $message];
         return false;
     }
     try {
         $dependencies = $this->admin->dependenciesThatCanBeRemovedWhenRemoving($package);
         $result = Gpm::uninstall($package, []);
     } catch (\Exception $e) {
         $this->admin->json_response = ['status' => 'error', 'message' => $e->getMessage()];
         return false;
     }
     if ($result) {
         $this->admin->json_response = ['status' => 'success', 'dependencies' => $dependencies, 'message' => $this->admin->translate(is_string($result) ? $result : 'PLUGIN_ADMIN.UNINSTALL_SUCCESSFUL')];
     } else {
         $this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.UNINSTALL_FAILED')];
     }
     return true;
 }
All Usage Examples Of Grav\Plugin\Admin\Gpm::uninstall