mirror of
https://github.com/flarum/core.git
synced 2025-08-13 11:54:32 +02:00
[untested] Major Update handler (#1)
This commit is contained in:
41
extensions/package-manager/src/Api/Controller/MajorUpdateController.php
Executable file
41
extensions/package-manager/src/Api/Controller/MajorUpdateController.php
Executable file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace SychO\PackageManager\Api\Controller;
|
||||
|
||||
use Flarum\Bus\Dispatcher;
|
||||
use Flarum\Http\RequestUtil;
|
||||
use Laminas\Diactoros\Response\EmptyResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Illuminate\Support\Arr;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use SychO\PackageManager\Command\MajorUpdate;
|
||||
|
||||
class MajorUpdateController implements RequestHandlerInterface
|
||||
{
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $bus;
|
||||
|
||||
public function __construct(Dispatcher $bus)
|
||||
{
|
||||
$this->bus = $bus;
|
||||
}
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$actor = RequestUtil::getActor($request);
|
||||
$dryRun = (bool) (int) Arr::get($request->getParsedBody(), 'data.dryRun');
|
||||
|
||||
$this->bus->dispatch(
|
||||
new MajorUpdate($actor, $dryRun)
|
||||
);
|
||||
|
||||
return new EmptyResponse();
|
||||
}
|
||||
}
|
@@ -27,9 +27,6 @@ class UpdateExtensionController implements RequestHandlerInterface
|
||||
$this->bus = $bus;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Flarum\User\Exception\PermissionDeniedException
|
||||
*/
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$actor = RequestUtil::getActor($request);
|
||||
|
24
extensions/package-manager/src/Command/MajorUpdate.php
Normal file
24
extensions/package-manager/src/Command/MajorUpdate.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace SychO\PackageManager\Command;
|
||||
|
||||
use Flarum\User\User;
|
||||
|
||||
class MajorUpdate
|
||||
{
|
||||
/**
|
||||
* @var \Flarum\User\User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $dryRun;
|
||||
|
||||
public function __construct(User $actor, bool $dryRun)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->dryRun = $dryRun;
|
||||
}
|
||||
}
|
152
extensions/package-manager/src/Command/MajorUpdateHandler.php
Normal file
152
extensions/package-manager/src/Command/MajorUpdateHandler.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace SychO\PackageManager\Command;
|
||||
|
||||
use Composer\Console\Application;
|
||||
use Flarum\Foundation\Paths;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Support\Arr;
|
||||
use SychO\PackageManager\Event\FlarumUpdated;
|
||||
use SychO\PackageManager\Exception\ComposerUpdateFailedException;
|
||||
use SychO\PackageManager\LastUpdateCheck;
|
||||
use SychO\PackageManager\OutputLogger;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\BufferedOutput;
|
||||
|
||||
class MajorUpdateHandler
|
||||
{
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
protected $composer;
|
||||
|
||||
/**
|
||||
* @var LastUpdateCheck
|
||||
*/
|
||||
protected $lastUpdateCheck;
|
||||
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @var OutputLogger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var Paths
|
||||
*/
|
||||
protected $paths;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $composerJson;
|
||||
|
||||
public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck, Dispatcher $events, OutputLogger $logger, Paths $paths)
|
||||
{
|
||||
$this->composer = $composer;
|
||||
$this->lastUpdateCheck = $lastUpdateCheck;
|
||||
$this->events = $events;
|
||||
$this->logger = $logger;
|
||||
$this->paths = $paths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the version constraint for all directly required packages in the root composer.json to *.
|
||||
* Set flarum/core version constraint to new major version.
|
||||
* Run composer update --prefer-dist --no-plugins --no-dev -a --with-all-dependencies.
|
||||
* Clear cache.
|
||||
* Run migrations.
|
||||
*
|
||||
* @throws \Flarum\User\Exception\PermissionDeniedException
|
||||
* @throws ComposerUpdateFailedException
|
||||
*/
|
||||
public function handle(MajorUpdate $command)
|
||||
{
|
||||
$command->actor->assertAdmin();
|
||||
|
||||
$majorVersion = $this->getNewMajorVersion();
|
||||
|
||||
if (! $majorVersion) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->updateComposerJson($majorVersion);
|
||||
|
||||
$this->runCommand($command->dryRun);
|
||||
|
||||
if ($command->dryRun) {
|
||||
$this->revertComposerJson();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->lastUpdateCheck->forget('flarum/*', true);
|
||||
|
||||
$this->events->dispatch(
|
||||
new FlarumUpdated(FlarumUpdated::MAJOR)
|
||||
);
|
||||
|
||||
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';
|
||||
file_put_contents($composerJsonPath, $this->composerJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ComposerUpdateFailedException
|
||||
*/
|
||||
protected function runCommand(bool $dryRun): void
|
||||
{
|
||||
$output = new BufferedOutput();
|
||||
$input = new ArrayInput([
|
||||
'command' => 'update',
|
||||
'--prefer-dist' => true,
|
||||
'--no-plugins' => true,
|
||||
'--no-dev' => true,
|
||||
'-a' => true,
|
||||
'--with-all-dependencies' => true,
|
||||
'--dry-run' => $dryRun,
|
||||
]);
|
||||
|
||||
$exitCode = $this->composer->run($input, $output);
|
||||
$output = $output->fetch();
|
||||
|
||||
$this->logger->log($input->__toString(), $output, $exitCode);
|
||||
|
||||
if ($exitCode !== 0) {
|
||||
throw new ComposerUpdateFailedException('*', $output);
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,6 +9,7 @@ namespace SychO\PackageManager\Listener;
|
||||
use Composer\Command\ClearCacheCommand;
|
||||
use Flarum\Database\Console\MigrateCommand;
|
||||
use Flarum\Foundation\Console\AssetsPublishCommand;
|
||||
use SychO\PackageManager\Event\FlarumUpdated;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\NullOutput;
|
||||
|
||||
@@ -39,7 +40,7 @@ class PostUpdateListener
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function handle($event)
|
||||
public function handle(FlarumUpdated $event)
|
||||
{
|
||||
$this->clearCache->run(new ArrayInput([]), new NullOutput());
|
||||
$this->migrate->run(new ArrayInput([]), new NullOutput());
|
||||
|
Reference in New Issue
Block a user