maj grav
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav.Console
|
||||
* @package Grav\Console\Gpm
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Console\Gpm;
|
||||
|
||||
use Grav\Common\Cache;
|
||||
use Grav\Common\Filesystem\Folder;
|
||||
use Grav\Common\GPM\Installer;
|
||||
use Grav\Common\GPM\Remote\Package;
|
||||
use Grav\Common\GPM\Response;
|
||||
use Grav\Common\GPM\Upgrader;
|
||||
use Grav\Common\Grav;
|
||||
@@ -20,45 +23,33 @@ use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
|
||||
class SelfupgradeCommand extends ConsoleCommand
|
||||
{
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
/** @var array */
|
||||
protected $data;
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
|
||||
protected $extensions;
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
|
||||
protected $updatable;
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
|
||||
/** @var string */
|
||||
protected $file;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
|
||||
/** @var array */
|
||||
protected $types = ['plugins', 'themes'];
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
|
||||
/** @var string */
|
||||
private $tmp;
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
|
||||
private $upgrader;
|
||||
|
||||
/** @var string */
|
||||
protected $all_yes;
|
||||
|
||||
protected $overwrite;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName("self-upgrade")
|
||||
->setName('self-upgrade')
|
||||
->setAliases(['selfupgrade', 'selfupdate'])
|
||||
->addOption(
|
||||
'force',
|
||||
@@ -78,13 +69,10 @@ class SelfupgradeCommand extends ConsoleCommand
|
||||
InputOption::VALUE_NONE,
|
||||
'Option to overwrite packages if they already exist'
|
||||
)
|
||||
->setDescription("Detects and performs an update of Grav itself when available")
|
||||
->setDescription('Detects and performs an update of Grav itself when available')
|
||||
->setHelp('The <info>update</info> command updates Grav itself when a new version is available');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null|void
|
||||
*/
|
||||
protected function serve()
|
||||
{
|
||||
$this->upgrader = new Upgrader($this->input->getOption('force'));
|
||||
@@ -100,26 +88,26 @@ class SelfupgradeCommand extends ConsoleCommand
|
||||
$release = strftime('%c', strtotime($this->upgrader->getReleaseDate()));
|
||||
|
||||
if (!$this->upgrader->meetsRequirements()) {
|
||||
$this->output->writeln("<red>ATTENTION:</red>");
|
||||
$this->output->writeln(" Grav has increased the minimum PHP requirement.");
|
||||
$this->output->writeln(" You are currently running PHP <red>" . phpversion() . "</red>, but PHP <green>" . $this->upgrader->minPHPVersion() . "</green> is required.");
|
||||
$this->output->writeln(" Additional information: <white>http://getgrav.org/blog/changing-php-requirements</white>");
|
||||
$this->output->writeln("");
|
||||
$this->output->writeln("Selfupgrade aborted.");
|
||||
$this->output->writeln("");
|
||||
$this->output->writeln('<red>ATTENTION:</red>');
|
||||
$this->output->writeln(' Grav has increased the minimum PHP requirement.');
|
||||
$this->output->writeln(' You are currently running PHP <red>' . phpversion() . '</red>, but PHP <green>' . $this->upgrader->minPHPVersion() . '</green> is required.');
|
||||
$this->output->writeln(' Additional information: <white>http://getgrav.org/blog/changing-php-requirements</white>');
|
||||
$this->output->writeln('');
|
||||
$this->output->writeln('Selfupgrade aborted.');
|
||||
$this->output->writeln('');
|
||||
exit;
|
||||
}
|
||||
|
||||
if (!$this->overwrite && !$this->upgrader->isUpgradable()) {
|
||||
$this->output->writeln("You are already running the latest version of Grav (v" . $local . ") released on " . $release);
|
||||
$this->output->writeln("You are already running the latest version of Grav (v{$local}) released on {$release}");
|
||||
exit;
|
||||
}
|
||||
|
||||
Installer::isValidDestination(GRAV_ROOT . '/system');
|
||||
if (Installer::IS_LINK === Installer::lastErrorCode()) {
|
||||
$this->output->writeln("<red>ATTENTION:</red> Grav is symlinked, cannot upgrade, aborting...");
|
||||
$this->output->writeln('<red>ATTENTION:</red> Grav is symlinked, cannot upgrade, aborting...');
|
||||
$this->output->writeln('');
|
||||
$this->output->writeln("You are currently running a symbolically linked Grav v" . $local . ". Latest available is v". $remote . ".");
|
||||
$this->output->writeln("You are currently running a symbolically linked Grav v{$local}. Latest available is v{$remote}.");
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -129,51 +117,51 @@ class SelfupgradeCommand extends ConsoleCommand
|
||||
$questionHelper = $this->getHelper('question');
|
||||
|
||||
|
||||
$this->output->writeln("Grav v<cyan>$remote</cyan> is now available [release date: $release].");
|
||||
$this->output->writeln("You are currently using v<cyan>" . GRAV_VERSION . "</cyan>.");
|
||||
$this->output->writeln("Grav v<cyan>{$remote}</cyan> is now available [release date: {$release}].");
|
||||
$this->output->writeln('You are currently using v<cyan>' . GRAV_VERSION . '</cyan>.');
|
||||
|
||||
if (!$this->all_yes) {
|
||||
$question = new ConfirmationQuestion("Would you like to read the changelog before proceeding? [y|N] ",
|
||||
$question = new ConfirmationQuestion('Would you like to read the changelog before proceeding? [y|N] ',
|
||||
false);
|
||||
$answer = $questionHelper->ask($this->input, $this->output, $question);
|
||||
|
||||
if ($answer) {
|
||||
$changelog = $this->upgrader->getChangelog(GRAV_VERSION);
|
||||
|
||||
$this->output->writeln("");
|
||||
$this->output->writeln('');
|
||||
foreach ($changelog as $version => $log) {
|
||||
$title = $version . ' [' . $log['date'] . ']';
|
||||
$content = preg_replace_callback('/\d\.\s\[\]\(#(.*)\)/', function ($match) {
|
||||
return "\n" . ucfirst($match[1]) . ":";
|
||||
return "\n" . ucfirst($match[1]) . ':';
|
||||
}, $log['content']);
|
||||
|
||||
$this->output->writeln($title);
|
||||
$this->output->writeln(str_repeat('-', strlen($title)));
|
||||
$this->output->writeln(str_repeat('-', \strlen($title)));
|
||||
$this->output->writeln($content);
|
||||
$this->output->writeln("");
|
||||
$this->output->writeln('');
|
||||
}
|
||||
|
||||
$question = new ConfirmationQuestion("Press [ENTER] to continue.", true);
|
||||
$question = new ConfirmationQuestion('Press [ENTER] to continue.', true);
|
||||
$questionHelper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
$question = new ConfirmationQuestion("Would you like to upgrade now? [y|N] ", false);
|
||||
$question = new ConfirmationQuestion('Would you like to upgrade now? [y|N] ', false);
|
||||
$answer = $questionHelper->ask($this->input, $this->output, $question);
|
||||
|
||||
if (!$answer) {
|
||||
$this->output->writeln("Aborting...");
|
||||
$this->output->writeln('Aborting...');
|
||||
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$this->output->writeln("");
|
||||
$this->output->writeln("Preparing to upgrade to v<cyan>$remote</cyan>..");
|
||||
$this->output->writeln('');
|
||||
$this->output->writeln("Preparing to upgrade to v<cyan>{$remote}</cyan>..");
|
||||
|
||||
$this->output->write(" |- Downloading upgrade [" . $this->formatBytes($update['size']) . "]... 0%");
|
||||
$this->output->write(" |- Downloading upgrade [{$this->formatBytes($update['size'])}]... 0%");
|
||||
$this->file = $this->download($update);
|
||||
|
||||
$this->output->write(" |- Installing upgrade... ");
|
||||
$this->output->write(' |- Installing upgrade... ');
|
||||
$installation = $this->upgrade();
|
||||
|
||||
if (!$installation) {
|
||||
@@ -189,20 +177,20 @@ class SelfupgradeCommand extends ConsoleCommand
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $package
|
||||
* @param Package $package
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function download($package)
|
||||
{
|
||||
$tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
|
||||
$this->tmp = $tmp_dir . '/Grav-' . uniqid();
|
||||
$this->tmp = $tmp_dir . '/Grav-' . uniqid('', false);
|
||||
$output = Response::get($package['download'], [], [$this, 'progress']);
|
||||
|
||||
Folder::mkdir($this->tmp);
|
||||
Folder::create($this->tmp);
|
||||
|
||||
$this->output->write("\x0D");
|
||||
$this->output->write(" |- Downloading upgrade [" . $this->formatBytes($package['size']) . "]... 100%");
|
||||
$this->output->write(" |- Downloading upgrade [{$this->formatBytes($package['size'])}]... 100%");
|
||||
$this->output->writeln('');
|
||||
|
||||
file_put_contents($this->tmp . DS . $package['name'], $output);
|
||||
@@ -215,15 +203,24 @@ class SelfupgradeCommand extends ConsoleCommand
|
||||
*/
|
||||
private function upgrade()
|
||||
{
|
||||
Installer::install($this->file, GRAV_ROOT,
|
||||
['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]);
|
||||
if ($this->file) {
|
||||
$folder = Installer::unZip($this->file, $this->tmp . '/zip');
|
||||
} else {
|
||||
$folder = false;
|
||||
}
|
||||
|
||||
static::upgradeGrav($this->file, $folder);
|
||||
|
||||
$errorCode = Installer::lastErrorCode();
|
||||
Folder::delete($this->tmp);
|
||||
|
||||
if ($this->tmp) {
|
||||
Folder::delete($this->tmp);
|
||||
}
|
||||
|
||||
if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
|
||||
$this->output->write("\x0D");
|
||||
// extra white spaces to clear out the buffer properly
|
||||
$this->output->writeln(" |- Installing upgrade... <red>error</red> ");
|
||||
$this->output->writeln(' |- Installing upgrade... <red>error</red> ');
|
||||
$this->output->writeln(" | '- " . Installer::lastErrorMsg());
|
||||
|
||||
return false;
|
||||
@@ -231,23 +228,23 @@ class SelfupgradeCommand extends ConsoleCommand
|
||||
|
||||
$this->output->write("\x0D");
|
||||
// extra white spaces to clear out the buffer properly
|
||||
$this->output->writeln(" |- Installing upgrade... <green>ok</green> ");
|
||||
$this->output->writeln(' |- Installing upgrade... <green>ok</green> ');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $progress
|
||||
* @param array $progress
|
||||
*/
|
||||
public function progress($progress)
|
||||
{
|
||||
$this->output->write("\x0D");
|
||||
$this->output->write(" |- Downloading upgrade [" . $this->formatBytes($progress["filesize"]) . "]... " . str_pad($progress['percent'],
|
||||
5, " ", STR_PAD_LEFT) . '%');
|
||||
$this->output->write(" |- Downloading upgrade [{$this->formatBytes($progress['filesize']) }]... " . str_pad($progress['percent'],
|
||||
5, ' ', STR_PAD_LEFT) . '%');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $size
|
||||
* @param int|float $size
|
||||
* @param int $precision
|
||||
*
|
||||
* @return string
|
||||
@@ -257,6 +254,44 @@ class SelfupgradeCommand extends ConsoleCommand
|
||||
$base = log($size) / log(1024);
|
||||
$suffixes = array('', 'k', 'M', 'G', 'T');
|
||||
|
||||
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[(int)floor($base)];
|
||||
return round(1024 ** ($base - floor($base)), $precision) . $suffixes[(int)floor($base)];
|
||||
}
|
||||
|
||||
private function upgradeGrav($zip, $folder, $keepFolder = false)
|
||||
{
|
||||
static $ignores = [
|
||||
'backup',
|
||||
'cache',
|
||||
'images',
|
||||
'logs',
|
||||
'tmp',
|
||||
'user',
|
||||
'.htaccess',
|
||||
'robots.txt'
|
||||
];
|
||||
|
||||
if (!is_dir($folder)) {
|
||||
Installer::setError('Invalid source folder');
|
||||
}
|
||||
|
||||
try {
|
||||
$script = $folder . '/system/install.php';
|
||||
/** Install $installer */
|
||||
if ((file_exists($script) && $install = include $script) && is_callable($install)) {
|
||||
$install($zip);
|
||||
} else {
|
||||
Installer::install(
|
||||
$zip,
|
||||
GRAV_ROOT,
|
||||
['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true, 'ignores' => $ignores],
|
||||
$folder,
|
||||
$keepFolder
|
||||
);
|
||||
|
||||
Cache::clearCache();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Installer::setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user