Skip to content

Commit

Permalink
Fixed bin/gpm selfupgrade error on Call to undefined method [#3160]
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Jan 20, 2021
1 parent eb98597 commit f5b1056
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Sanitize valid Page extensions from `Page::template_format()`
* Fixed `bin/gpm index` erroring out [#3158](https://github.com/getgrav/grav/issues/3158)
* Fixed `bin/gpm selfupgrade` failing to report failed Grav update [#3116](https://github.com/getgrav/grav/issues/3116)
* Fixed `bin/gpm selfupgrade` error on `Call to undefined method` [#3160](https://github.com/getgrav/grav/issues/3160)
* Flex Pages: Fixed fatal error when trying to move a page to Root (/) [#3161](https://github.com/getgrav/grav/issues/3161)

# v1.7.0
Expand Down
9 changes: 9 additions & 0 deletions system/src/Grav/Console/Cli/ClearCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Grav\Common\Cache;
use Grav\Console\GravCommand;
use Symfony\Component\Console\Input\InputOption;
use function is_callable;

/**
* Class ClearCacheCommand
Expand Down Expand Up @@ -44,6 +45,14 @@ protected function configure(): void
*/
protected function serve(): int
{
// Old versions of Grav called this command after grav upgrade.
// We need make this command to work with older GravCommand instance:
if (!is_callable($this, 'initializePlugins')) {
Cache::clearCache('all');

return 0;
}

$this->initializePlugins();
$this->cleanPaths();

Expand Down
31 changes: 6 additions & 25 deletions system/src/Grav/Console/Gpm/DirectInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected function serve(): int
{
$input = $this->getInput();
$io = $this->getIO();

if (!class_exists(ZipArchive::class)) {
$io->title('Direct Install');
$io->error('php-zip extension needs to be enabled!');
Expand Down Expand Up @@ -266,6 +267,9 @@ protected function serve(): int
],
$extracted
);

// clear cache after successful upgrade
$this->clearCache();
}

Folder::delete($tmp_source);
Expand All @@ -289,31 +293,16 @@ protected function serve(): int

Folder::delete($tmp_zip);

// clear cache after successful upgrade
$this->clearCache();

return 0;
}

/**
* @param string $zip
* @param string $folder
* @param bool $keepFolder
* @return void
*/
private function upgradeGrav(string $zip, string $folder, bool $keepFolder = false): void
private function upgradeGrav(string $zip, string $folder): void
{
static $ignores = [
'backup',
'cache',
'images',
'logs',
'tmp',
'user',
'.htaccess',
'robots.txt'
];

if (!is_dir($folder)) {
Installer::setError('Invalid source folder');
}
Expand All @@ -324,15 +313,7 @@ private function upgradeGrav(string $zip, string $folder, bool $keepFolder = fal
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();
throw new RuntimeException('Uploaded archive file is not a valid Grav update package');
}
} catch (Exception $e) {
Installer::setError($e->getMessage());
Expand Down
58 changes: 14 additions & 44 deletions system/src/Grav/Console/Gpm/SelfupgradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
namespace Grav\Console\Gpm;

use Exception;
use Grav\Common\Cache;
use Grav\Common\Filesystem\Folder;
use Grav\Common\GPM\Installer;
use Grav\Common\GPM\Response;
use Grav\Common\GPM\Upgrader;
use Grav\Common\Grav;
use Grav\Console\GpmCommand;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ConfirmationQuestion;
Expand All @@ -36,7 +36,7 @@ class SelfupgradeCommand extends GpmCommand
protected $file;
/** @var array */
protected $types = ['plugins', 'themes'];
/** @var string */
/** @var string|null */
private $tmp;
/** @var Upgrader */
private $upgrader;
Expand Down Expand Up @@ -202,8 +202,9 @@ protected function serve(): int
$io->newLine();
}

// clear cache after successful upgrade
$this->clearCache(['all']);
if ($this->tmp && is_dir($this->tmp)) {
Folder::delete($this->tmp);
}

return $error;
}
Expand All @@ -217,7 +218,7 @@ private function download(array $package): string
$io = $this->getIO();

$tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
$this->tmp = $tmp_dir . '/Grav-' . uniqid('', false);
$this->tmp = $tmp_dir . '/grav-update-' . uniqid('', false);
$options = [
'curl' => [
CURLOPT_TIMEOUT => $this->timeout,
Expand Down Expand Up @@ -247,17 +248,7 @@ private function upgrade(): bool
{
$io = $this->getIO();

if ($this->file) {
$folder = Installer::unZip($this->file, $this->tmp . '/zip');
} else {
$folder = false;
}

$this->upgradeGrav($this->file, $folder);

if ($this->tmp) {
Folder::delete($this->tmp);
}
$this->upgradeGrav($this->file);

$errorCode = Installer::lastErrorCode();
if ($errorCode) {
Expand Down Expand Up @@ -308,42 +299,21 @@ public function formatBytes($size, int $precision = 2): string

/**
* @param string $zip
* @param string $folder
* @param bool $keepFolder
* @return void
*/
private function upgradeGrav(string $zip, string $folder, bool $keepFolder = false): void
private function upgradeGrav(string $zip): void
{
static $ignores = [
'backup',
'cache',
'images',
'logs',
'tmp',
'user',
'.htaccess',
'robots.txt'
];

if (!is_dir($folder)) {
Installer::setError('Invalid source folder');
}

try {
$folder = Installer::unZip($zip, $this->tmp . '/zip');
if ($folder === false) {
throw new RuntimeException(Installer::lastErrorMsg());
}

$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();
throw new RuntimeException('Uploaded archive file is not a valid Grav update package');
}
} catch (Exception $e) {
Installer::setError($e->getMessage());
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Installer/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function finalize(): void
{
$this->updater->postflight();

Cache::clearCache();
Cache::clearCache('all');

clearstatcache();
if (function_exists('opcache_reset')) {
Expand Down

0 comments on commit f5b1056

Please sign in to comment.