mirror of
https://github.com/flarum/core.git
synced 2025-08-04 07:27:39 +02:00
Split responsibilities across different classes.
This commit is contained in:
@@ -9,10 +9,9 @@
|
||||
|
||||
namespace Flarum\PackageManager\Command;
|
||||
|
||||
use Flarum\Foundation\Paths;
|
||||
use Flarum\PackageManager\Composer\ComposerAdapter;
|
||||
use Flarum\PackageManager\Composer\ComposerJson;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Support\Arr;
|
||||
use Flarum\PackageManager\Event\FlarumUpdated;
|
||||
use Flarum\PackageManager\Exception\ComposerUpdateFailedException;
|
||||
use Flarum\PackageManager\LastUpdateCheck;
|
||||
@@ -36,21 +35,22 @@ class MajorUpdateHandler
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @var Paths
|
||||
*/
|
||||
protected $paths;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* @var ComposerJson
|
||||
*/
|
||||
protected $composerJson;
|
||||
|
||||
public function __construct(ComposerAdapter $composer, LastUpdateCheck $lastUpdateCheck, Dispatcher $events, Paths $paths)
|
||||
/**
|
||||
* @param ComposerAdapter $composer
|
||||
* @param LastUpdateCheck $lastUpdateCheck
|
||||
* @param Dispatcher $events
|
||||
* @param ComposerJson $composerJson
|
||||
*/
|
||||
public function __construct(ComposerAdapter $composer, LastUpdateCheck $lastUpdateCheck, Dispatcher $events, ComposerJson $composerJson)
|
||||
{
|
||||
$this->composer = $composer;
|
||||
$this->lastUpdateCheck = $lastUpdateCheck;
|
||||
$this->events = $events;
|
||||
$this->paths = $paths;
|
||||
$this->composerJson = $composerJson;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +67,7 @@ class MajorUpdateHandler
|
||||
{
|
||||
$command->actor->assertAdmin();
|
||||
|
||||
$majorVersion = $this->getNewMajorVersion();
|
||||
$majorVersion = $this->lastUpdateCheck->getNewMajorVersion();
|
||||
|
||||
if (! $majorVersion) {
|
||||
return false;
|
||||
@@ -78,7 +78,7 @@ class MajorUpdateHandler
|
||||
$this->runCommand($command->dryRun);
|
||||
|
||||
if ($command->dryRun) {
|
||||
$this->revertComposerJson();
|
||||
$this->composerJson->revert();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -92,36 +92,10 @@ class MajorUpdateHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getNewMajorVersion(): ?string
|
||||
{
|
||||
$core = Arr::first($this->lastUpdateCheck->get()['updates']['installed'], function ($package) {
|
||||
return $package['name'] === 'flarum/core';
|
||||
});
|
||||
|
||||
return $core ? $core['latest-major'] : null;
|
||||
}
|
||||
|
||||
protected function updateComposerJson(string $majorVersion): void
|
||||
{
|
||||
$composerJsonPath = $this->paths->base . '/composer.json';
|
||||
$this->composerJson = $newComposerJson = json_decode(file_get_contents($composerJsonPath), true);
|
||||
|
||||
foreach ($newComposerJson['require'] as $name => &$version) {
|
||||
if ($name === 'flarum/core') {
|
||||
$version = '^'.str_replace('v', '', $majorVersion);
|
||||
} else {
|
||||
$version = '*';
|
||||
}
|
||||
}
|
||||
|
||||
file_put_contents($composerJsonPath, json_encode($newComposerJson));
|
||||
}
|
||||
|
||||
protected function revertComposerJson(): void
|
||||
{
|
||||
$composerJsonPath = $this->paths->base . '/composer.json';
|
||||
// @todo use filesystem for all file_get_contents
|
||||
file_put_contents($composerJsonPath, $this->composerJson);
|
||||
$this->composerJson->require('*', '*');
|
||||
$this->composerJson->require('flarum/core', '^'.str_replace('v', '', $majorVersion));
|
||||
}
|
||||
|
||||
/**
|
||||
|
83
extensions/package-manager/src/Composer/ComposerJson.php
Normal file
83
extensions/package-manager/src/Composer/ComposerJson.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\PackageManager\Composer;
|
||||
|
||||
use Flarum\Foundation\Paths;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
|
||||
class ComposerJson
|
||||
{
|
||||
/**
|
||||
* @var Paths
|
||||
*/
|
||||
protected $paths;
|
||||
|
||||
/**
|
||||
* @var Filesystem
|
||||
*/
|
||||
protected $filesystem;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $initialJson;
|
||||
|
||||
public function __construct(Paths $paths, Filesystem $filesystem)
|
||||
{
|
||||
$this->paths = $paths;
|
||||
$this->filesystem = $filesystem;
|
||||
}
|
||||
|
||||
public function require(string $packageName, string $version): void
|
||||
{
|
||||
$composerJson = $this->getComposerJson();
|
||||
|
||||
if (strpos($packageName, '*') === false) {
|
||||
$composerJson['require'][$packageName] = $version;
|
||||
} else {
|
||||
foreach ($composerJson['require'] as $p => $v) {
|
||||
if (preg_match(preg_quote(str_replace('*', '.*', $packageName), '/'), $p, $matches)) {
|
||||
$composerJson['require'][$p] = $version;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->setComposerJson($composerJson);
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
{
|
||||
$this->setComposerJson($this->initialJson);
|
||||
}
|
||||
|
||||
protected function getComposerJsonPath(): string
|
||||
{
|
||||
return $this->paths->base . '/composer.json';
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
||||
*/
|
||||
protected function getComposerJson(): array
|
||||
{
|
||||
$json = json_decode($this->filesystem->get($this->getComposerJsonPath()), true);
|
||||
|
||||
if (! $this->initialJson) {
|
||||
$this->initialJson = $json;
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
protected function setComposerJson(array $json): void
|
||||
{
|
||||
$this->filesystem->put($this->getComposerJsonPath(), json_encode($json, JSON_PRETTY_PRINT));
|
||||
}
|
||||
}
|
@@ -27,7 +27,7 @@ class ComposerCommandFailedException extends Exception
|
||||
|
||||
public function guessCause(): ?string
|
||||
{
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getRawPackageName(): string
|
||||
|
@@ -11,6 +11,7 @@ namespace Flarum\PackageManager;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class LastUpdateCheck
|
||||
@@ -44,6 +45,15 @@ class LastUpdateCheck
|
||||
return json_decode($this->settings->get(self::KEY, '{}'), true);
|
||||
}
|
||||
|
||||
public function getNewMajorVersion(): ?string
|
||||
{
|
||||
$core = Arr::first($this->get()['updates']['installed'], function ($package) {
|
||||
return $package['name'] === 'flarum/core';
|
||||
});
|
||||
|
||||
return $core ? $core['latest-major'] : null;
|
||||
}
|
||||
|
||||
public function forget(string $name, bool $wildcard = false): void
|
||||
{
|
||||
$lastUpdateCheck = json_decode($this->settings->get(self::KEY, '{}'), true);
|
||||
|
Reference in New Issue
Block a user