1
0
mirror of https://github.com/flarum/core.git synced 2025-08-13 11:54:32 +02:00

Minor flarum update

This commit is contained in:
SychO9
2021-09-27 10:16:01 +01:00
parent 2812eada2c
commit 19da2e9827
53 changed files with 465 additions and 140 deletions

View File

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\MinorFlarumUpdate;
class MinorFlarumUpdateController 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 MinorFlarumUpdate($actor)
);
return new EmptyResponse();
}
}

View File

View File

View File

@@ -9,6 +9,8 @@ namespace SychO\PackageManager\Command;
use Carbon\Carbon;
use Composer\Console\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use SychO\PackageManager\Exception\ComposerCommandFailedException;
use SychO\PackageManager\LastUpdateCheck;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
@@ -20,22 +22,23 @@ class CheckForUpdatesHandler
protected $composer;
/**
* @var SettingsRepositoryInterface
* @var LastUpdateCheck
*/
protected $settings;
protected $lastUpdateCheck;
/**
* @param Application $composer
* @param SettingsRepositoryInterface $settings
* @param LastUpdateCheck $lastUpdateCheck
*/
public function __construct(Application $composer, SettingsRepositoryInterface $settings)
public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck)
{
$this->composer = $composer;
$this->settings = $settings;
$this->lastUpdateCheck = $lastUpdateCheck;
}
/**
* @throws \Flarum\User\Exception\PermissionDeniedException
* @throws ComposerCommandFailedException
*/
public function handle(CheckForUpdates $command)
{
@@ -50,15 +53,12 @@ class CheckForUpdatesHandler
'--format' => 'json',
]);
$this->composer->run($input, $output);
$exitCode = $this->composer->run($input, $output);
$lastUpdateCheck = [
'checkedAt' => Carbon::now(),
'updates' => json_decode($output->fetch(), true),
];
if ($exitCode !== 0) {
throw new ComposerCommandFailedException('', $output->fetch());
}
$this->settings->set('sycho-package-manager.last_update_check', json_encode($lastUpdateCheck));
return $lastUpdateCheck;
return $this->lastUpdateCheck->save(json_decode($output->fetch(), true));
}
}

View File

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

View File

@@ -0,0 +1,65 @@
<?php
/**
*
*/
namespace SychO\PackageManager\Command;
use Composer\Console\Application;
use SychO\PackageManager\Exception\ComposerUpdateFailedException;
use SychO\PackageManager\LastUpdateCheck;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class MinorFlarumUpdateHandler
{
/**
* @var Application
*/
protected $composer;
/**
* @var LastUpdateCheck
*/
protected $lastUpdateCheck;
/**
* @param Application $composer
* @param LastUpdateCheck $lastUpdateCheck
*/
public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck)
{
$this->composer = $composer;
$this->lastUpdateCheck = $lastUpdateCheck;
}
/**
* @throws \Flarum\User\Exception\PermissionDeniedException
* @throws ComposerUpdateFailedException
*/
public function handle(MinorFlarumUpdate $command)
{
$command->actor->assertAdmin();
$output = new BufferedOutput();
$input = new ArrayInput([
'command' => 'update',
'packages' => ["flarum/*"],
'--prefer-dist' => true,
'--no-dev' => true,
'-a' => true,
'--with-all-dependencies' => true,
]);
$exitCode = $this->composer->run($input, $output);
if ($exitCode !== 0) {
throw new ComposerUpdateFailedException('flarum/*', $output->fetch());
}
$this->lastUpdateCheck->forget('flarum/*', true);
return true;
}
}

View File

View File

View File

View File

View File

View File

@@ -11,6 +11,7 @@ use Flarum\Extension\ExtensionManager;
use Flarum\Settings\SettingsRepositoryInterface;
use SychO\PackageManager\Exception\ComposerUpdateFailedException;
use SychO\PackageManager\UpdateExtensionValidator;
use SychO\PackageManager\LastUpdateCheck;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
@@ -32,16 +33,16 @@ class UpdateExtensionHandler
protected $validator;
/**
* @var SettingsRepositoryInterface
* @var LastUpdateCheck
*/
protected $settings;
protected $lastUpdateCheck;
public function __construct(Application $composer, ExtensionManager $extensions, UpdateExtensionValidator $validator, SettingsRepositoryInterface $settings)
public function __construct(Application $composer, ExtensionManager $extensions, UpdateExtensionValidator $validator, LastUpdateCheck $lastUpdateCheck)
{
$this->composer = $composer;
$this->extensions = $extensions;
$this->validator = $validator;
$this->settings = $settings;
$this->lastUpdateCheck = $lastUpdateCheck;
}
/**
@@ -72,24 +73,7 @@ class UpdateExtensionHandler
throw new ComposerUpdateFailedException($extension->name, $output->fetch());
}
$lastUpdateCheck = json_decode($this->settings->get('sycho-package-manager.last_update_check', '{}'), true);
if (isset($lastUpdateCheck['updates']) && ! empty($lastUpdateCheck['updates']['installed'])) {
$updatesListChanged = false;
foreach ($lastUpdateCheck['updates']['installed'] as $k => $package) {
if ($package['name'] === $extension->name) {
unset($lastUpdateCheck['updates']['installed'][$k]);
$updatesListChanged = true;
break;
}
}
if ($updatesListChanged) {
$lastUpdateCheck['updates']['installed'] = array_values($lastUpdateCheck['updates']['installed']);
$this->settings->set('sycho-package-manager.last_update_check', json_encode($lastUpdateCheck));
}
}
$this->lastUpdateCheck->forget($extension->name);
return true;
}

View File

View File

View File

View File

View File

@@ -0,0 +1,69 @@
<?php
/**
*
*/
namespace SychO\PackageManager;
use Carbon\Carbon;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Str;
class LastUpdateCheck
{
public const KEY = 'sycho-package-manager.last_update_check';
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function save(array $updates): array
{
$lastUpdateCheck = [
'checkedAt' => Carbon::now(),
'updates' => $updates,
];
$this->settings->set(self::KEY, json_encode($lastUpdateCheck));
return $lastUpdateCheck;
}
public function get(): array
{
return json_decode($this->settings->get(self::KEY, '{}'), true);
}
public function forget(string $name, bool $wildcard = false): void
{
$lastUpdateCheck = json_decode($this->settings->get(self::KEY, '{}'), true);
if (isset($lastUpdateCheck['updates']) && ! empty($lastUpdateCheck['updates']['installed'])) {
$updatesListChanged = false;
$pattern = str_replace('*', '.*', preg_quote($name));
foreach ($lastUpdateCheck['updates']['installed'] as $k => $package) {
if (($wildcard && Str::of($package['name'])->test("/($pattern)/")) || $package['name'] === $name) {
unset($lastUpdateCheck['updates']['installed'][$k]);
$updatesListChanged = true;
if (! $wildcard) {
break;
}
}
}
if ($updatesListChanged) {
$lastUpdateCheck['updates']['installed'] = array_values($lastUpdateCheck['updates']['installed']);
$this->settings->set(self::KEY, json_encode($lastUpdateCheck));
}
}
}
}

View File

View File

View File