1
0
mirror of https://github.com/flarum/core.git synced 2025-08-13 20:04:24 +02:00

Global Update (Fixes #2)

This commit is contained in:
SychO9
2021-09-29 12:07:53 +01:00
parent 77f0dca47e
commit d878eeb92a
15 changed files with 290 additions and 22 deletions

View File

@@ -0,0 +1,42 @@
<?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 Psr\Http\Message\ServerRequestInterface;
use SychO\PackageManager\Command\GlobalUpdate;
class GlobalUpdateController implements RequestHandlerInterface
{
/**
* @var Dispatcher
*/
protected $bus;
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}
/**
* @throws \Flarum\User\Exception\PermissionDeniedException
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);
$this->bus->dispatch(
new GlobalUpdate($actor)
);
return new EmptyResponse();
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
*
*/
namespace SychO\PackageManager\Command;
use Flarum\User\User;
class GlobalUpdate
{
/**
* @var \Flarum\User\User
*/
public $actor;
public function __construct(User $actor)
{
$this->actor = $actor;
}
}

View File

@@ -0,0 +1,78 @@
<?php
/**
*
*/
namespace SychO\PackageManager\Command;
use Composer\Console\Application;
use Flarum\Bus\Dispatcher as FlarumDispatcher;
use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Event\FlarumUpdated;
use SychO\PackageManager\Exception\ComposerUpdateFailedException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class GlobalUpdateHandler
{
/**
* @var Application
*/
protected $composer;
/**
* @var Dispatcher
*/
protected $events;
/**
* @var FlarumDispatcher
*/
protected $commandDispatcher;
/**
* @param Application $composer
* @param Dispatcher $events
* @param FlarumDispatcher $commandDispatcher
*/
public function __construct(Application $composer, Dispatcher $events, FlarumDispatcher $commandDispatcher)
{
$this->composer = $composer;
$this->events = $events;
$this->commandDispatcher = $commandDispatcher;
}
/**
* @throws \Flarum\User\Exception\PermissionDeniedException|ComposerUpdateFailedException
*/
public function handle(GlobalUpdate $command)
{
$command->actor->assertAdmin();
$output = new BufferedOutput();
$input = new ArrayInput([
'command' => 'update',
'--prefer-dist' => true,
'--no-dev' => true,
'-a' => true,
'--with-all-dependencies' => true,
]);
$exitCode = $this->composer->run($input, $output);
if ($exitCode !== 0) {
throw new ComposerUpdateFailedException('*', $output->fetch());
}
$this->commandDispatcher->dispatch(
new CheckForUpdates($command->actor)
);
$this->events->dispatch(
new FlarumUpdated(FlarumUpdated::GLOBAL)
);
return true;
}
}

View File

@@ -1,5 +1,9 @@
<?php
/**
*
*/
namespace SychO\PackageManager\Command;
use Flarum\User\User;

View File

@@ -70,7 +70,7 @@ class MinorFlarumUpdateHandler
$this->lastUpdateCheck->forget('flarum/*', true);
$this->events->dispatch(
new FlarumUpdated()
new FlarumUpdated(FlarumUpdated::MINOR)
);
return true;

View File

@@ -1,5 +1,9 @@
<?php
/**
*
*/
namespace SychO\PackageManager\Command;
use Flarum\User\User;

View File

@@ -1,5 +1,9 @@
<?php
/**
*
*/
namespace SychO\PackageManager\Command;
use Flarum\User\User;

View File

@@ -1,5 +1,9 @@
<?php
/**
*
*/
namespace SychO\PackageManager\Command;
use Flarum\User\User;

View File

@@ -4,5 +4,17 @@ namespace SychO\PackageManager\Event;
class FlarumUpdated
{
public const GLOBAL = 'global';
public const MAJOR = 'major';
public const MINOR = 'minor';
/**
* @var string
*/
public $type;
public function __construct(string $type)
{
$this->type = $type;
}
}