/** * @return bool */ private function upgrade() { Installer::install($this->file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); $errorCode = Installer::lastErrorCode(); Folder::delete($this->tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing upgrade... <red>error</red> "); $this->output->writeln(" | '- " . Installer::lastErrorMsg()); return false; } $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing upgrade... <green>ok</green> "); return true; }
public static function selfupgrade() { $upgrader = new Upgrader(); if (!Installer::isGravInstance(GRAV_ROOT)) { return false; } if (is_link(GRAV_ROOT . DS . 'index.php')) { Installer::setError(Installer::IS_LINK); return false; } if (method_exists($upgrader, 'meetsRequirements') && !$upgrader->meetsRequirements()) { $error = []; $error[] = '<p>Grav has increased the minimum PHP requirement.<br />'; $error[] = 'You are currently running PHP <strong>' . PHP_VERSION . '</strong>'; $error[] = ', but PHP <strong>' . GRAV_PHP_MIN . '</strong> is required.</p>'; $error[] = '<p><a href="http://getgrav.org/blog/changing-php-requirements-to-5.5" class="button button-small secondary">Additional information</a></p>'; Installer::setError(implode("\n", $error)); return false; } $update = $upgrader->getAssets()['grav-update']; $tmp = CACHE_DIR . 'tmp/Grav-' . uniqid(); $file = self::_downloadSelfupgrade($update, $tmp); Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); $errorCode = Installer::lastErrorCode(); Folder::delete($tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { return false; } return true; }
public static function uninstall($packages, $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 = self::getGrav()['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) { return false; } } return true; }
/** * @param $package * * @return bool */ private function installPackage($package) { Installer::install($this->file, $this->destination, ['install_path' => $package->install_path]); $errorCode = Installer::lastErrorCode(); Folder::delete($this->tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing package... <red>error</red> "); $this->output->writeln(" | '- " . Installer::lastErrorMsg()); return false; } $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing package... <green>ok</green> "); return true; }
public static function selfupgrade() { $upgrader = new Upgrader(); if (!Installer::isGravInstance(GRAV_ROOT)) { return false; } if (is_link(GRAV_ROOT . DS . 'index.php')) { Installer::setError(Installer::IS_LINK); return false; } $update = $upgrader->getAssets()['grav-update']; $tmp = CACHE_DIR . 'tmp/Grav-' . uniqid(); $file = self::_downloadSelfupgrade($update, $tmp); Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); $errorCode = Installer::lastErrorCode(); Folder::delete($tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { return false; } return true; }
/** * @param $slug * @param $package * * @return bool */ private function checkDestination($slug, $package) { $path = self::getGrav()['locator']->findResource($package->package_type . '://' . $slug); $questionHelper = $this->getHelper('question'); $skipPrompt = $this->input->getOption('all-yes'); Installer::isValidDestination($path); if (Installer::lastErrorCode() == Installer::IS_LINK) { $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <yellow>symbolic link</yellow>"); if ($skipPrompt) { $this->output->writeln(" | '- <yellow>Skipped automatically.</yellow>"); return false; } $question = new ConfirmationQuestion(" | '- Destination has been detected as symlink, delete symbolic link first? [y|N] ", false); $answer = $questionHelper->ask($this->input, $this->output, $question); if (!$answer) { $this->output->writeln(" | '- <red>You decided to not delete the symlink automatically.</red>"); return false; } } $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <green>ok</green>"); return true; }
/** * Install a package * * @param Package $package * @param bool $is_update True if it's an update. False if it's an install * * @return bool */ private function installPackage($package, $is_update = false) { $type = $package->package_type; Installer::install($this->file, $this->destination, ['install_path' => $package->install_path, 'theme' => $type == 'themes', 'is_update' => $is_update]); $error_code = Installer::lastErrorCode(); Folder::delete($this->tmp); if ($error_code) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing package... <red>error</red> "); $this->output->writeln(" | '- " . Installer::lastErrorMsg()); return false; } $message = Installer::getMessage(); if ($message) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- " . $message); } $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing package... <green>ok</green> "); return true; }
/** * @return int|null|void */ protected function serve() { $package_file = $this->input->getArgument('package-file'); $helper = $this->getHelper('question'); $question = new ConfirmationQuestion('Are you sure you want to direct-install <cyan>' . $package_file . '</cyan> [y|N] ', false); $answer = $helper->ask($this->input, $this->output, $question); if (!$answer) { $this->output->writeln("exiting..."); $this->output->writeln(''); exit; } $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true); $tmp_zip = $tmp_dir . '/Grav-' . uniqid(); $this->output->writeln(""); $this->output->writeln("Preparing to install <cyan>" . $package_file . "</cyan>"); if ($this->isRemote($package_file)) { $zip = $this->downloadPackage($package_file, $tmp_zip); } else { $zip = $this->copyPackage($package_file, $tmp_zip); } if (file_exists($zip)) { $tmp_source = $tmp_dir . '/Grav-' . uniqid(); $this->output->write(" |- Extracting package... "); $extracted = Installer::unZip($zip, $tmp_source); if (!$extracted) { $this->output->write("\r"); $this->output->writeln(" |- Extracting package... <red>failed</red>"); exit; } $this->output->write("\r"); $this->output->writeln(" |- Extracting package... <green>ok</green>"); $type = $this->getPackageType($extracted); if (!$type) { $this->output->writeln(" '- <red>ERROR: Not a valid Grav package</red>"); $this->output->writeln(''); exit; } $blueprint = $this->getBlueprints($extracted); if ($blueprint) { if (isset($blueprint['dependencies'])) { $depencencies = []; foreach ($blueprint['dependencies'] as $dependency) { if (is_array($dependency) && isset($dependency['name'])) { $depencencies[] = $dependency['name']; } else { $depencencies[] = $dependency; } } $this->output->writeln(" |- Dependencies found... <cyan>[" . implode(',', $depencencies) . "]</cyan>"); $question = new ConfirmationQuestion(" | '- Dependencies will not be satisfied. Continue ? [y|N] ", false); $answer = $helper->ask($this->input, $this->output, $question); if (!$answer) { $this->output->writeln("exiting..."); $this->output->writeln(''); exit; } } } if ($type == 'grav') { $this->output->write(" |- Checking destination... "); Installer::isValidDestination(GRAV_ROOT . '/system'); if (Installer::IS_LINK === Installer::lastErrorCode()) { $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <yellow>symbolic link</yellow>"); $this->output->writeln(" '- <red>ERROR: symlinks found...</red> <yellow>" . GRAV_ROOT . "</yellow>"); $this->output->writeln(''); exit; } $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <green>ok</green>"); $this->output->write(" |- Installing package... "); Installer::install($zip, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true], $extracted); } else { $name = $this->getPackageName($extracted); if (!$name) { $this->output->writeln("<red>ERROR: Name could not be determined.</red> Please specify with --name|-n"); $this->output->writeln(''); exit; } $install_path = $this->getInstallPath($type, $name); $is_update = file_exists($install_path); $this->output->write(" |- Checking destination... "); Installer::isValidDestination(GRAV_ROOT . DS . $install_path); if (Installer::lastErrorCode() == Installer::IS_LINK) { $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <yellow>symbolic link</yellow>"); $this->output->writeln(" '- <red>ERROR: symlink found...</red> <yellow>" . GRAV_ROOT . DS . $install_path . '</yellow>'); $this->output->writeln(''); exit; } else { $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <green>ok</green>"); } $this->output->write(" |- Installing package... "); Installer::install($zip, GRAV_ROOT, ['install_path' => $install_path, 'theme' => $type == 'theme', 'is_update' => $is_update], $extracted); } Folder::delete($tmp_source); $this->output->write("\r"); if (Installer::lastErrorCode()) { $this->output->writeln(" '- <red>Installation failed or aborted.</red>"); $this->output->writeln(''); } else { $this->output->writeln(" |- Installing package... <green>ok</green>"); $this->output->writeln(" '- <green>Success!</green> "); $this->output->writeln(''); } } else { $this->output->writeln(" '- <red>ERROR: ZIP package could not be found</red>"); } Folder::delete($tmp_zip); // clear cache after successful upgrade $this->clearCache(); return true; }
/** * Check if package exists * * @param $slug * @param $package * @return int */ private function packageExists($slug, $package) { $path = Grav::instance()['locator']->findResource($package->package_type . '://' . $slug); Installer::isValidDestination($path); return Installer::lastErrorCode(); }