mirror of
https://github.com/flarum/core.git
synced 2025-08-13 20:04:24 +02:00
Add Extension Updating
This commit is contained in:
@@ -1,20 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace SychO\PackageManager\Api\Controller;
|
||||
|
||||
use Flarum\Bus\Dispatcher;
|
||||
use Flarum\Http\RequestUtil;
|
||||
use SychO\PackageManager\Api\Serializer\ExtensionSerializer;
|
||||
use Flarum\Api\Controller\AbstractShowController;
|
||||
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\UpdateExtension;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
class UpdateExtensionController extends AbstractShowController
|
||||
class UpdateExtensionController implements RequestHandlerInterface
|
||||
{
|
||||
public $serializer = ExtensionSerializer::class;
|
||||
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
@@ -28,13 +30,15 @@ class UpdateExtensionController extends AbstractShowController
|
||||
/**
|
||||
* @throws \Flarum\User\Exception\PermissionDeniedException
|
||||
*/
|
||||
protected function data(ServerRequestInterface $request, Document $document)
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$actor = RequestUtil::getActor($request);
|
||||
$extensionId = Arr::get($request->getQueryParams(), 'id');
|
||||
|
||||
return $this->bus->dispatch(
|
||||
$this->bus->dispatch(
|
||||
new UpdateExtension($actor, $extensionId)
|
||||
);
|
||||
|
||||
return new EmptyResponse();
|
||||
}
|
||||
}
|
||||
|
@@ -1,26 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace SychO\PackageManager\Command;
|
||||
|
||||
use Composer\Console\Application;
|
||||
use Flarum\Extension\ExtensionManager;
|
||||
use SychO\PackageManager\Extension\PackageManager;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use SychO\PackageManager\Exception\ComposerUpdateFailedException;
|
||||
use SychO\PackageManager\UpdateExtensionValidator;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\BufferedOutput;
|
||||
|
||||
class UpdateExtensionHandler
|
||||
{
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
protected $composer;
|
||||
|
||||
/**
|
||||
* @var ExtensionManager
|
||||
*/
|
||||
protected $extensions;
|
||||
|
||||
/**
|
||||
* @var PackageManager
|
||||
* @var UpdateExtensionValidator
|
||||
*/
|
||||
protected $packages;
|
||||
protected $validator;
|
||||
|
||||
public function __construct(ExtensionManager $extensions, PackageManager $packages)
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
public function __construct(Application $composer, ExtensionManager $extensions, UpdateExtensionValidator $validator, SettingsRepositoryInterface $settings)
|
||||
{
|
||||
$this->composer = $composer;
|
||||
$this->extensions = $extensions;
|
||||
$this->packages = $packages;
|
||||
$this->validator = $validator;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,12 +52,45 @@ class UpdateExtensionHandler
|
||||
{
|
||||
$command->actor->assertAdmin();
|
||||
|
||||
$this->validator->assertValid(['extensionId' => $command->extensionId]);
|
||||
|
||||
$extension = $this->extensions->getExtension($command->extensionId);
|
||||
|
||||
if (empty($extension)) {
|
||||
// ... exception
|
||||
}
|
||||
|
||||
$this->packages->updatePackage($extension->name);
|
||||
$output = new BufferedOutput();
|
||||
$input = new ArrayInput([
|
||||
'command' => 'require',
|
||||
'packages' => ["$extension->name:*"],
|
||||
]);
|
||||
|
||||
$exitCode = $this->composer->run($input, $output);
|
||||
|
||||
if ($exitCode !== 0) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace SychO\PackageManager\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ComposerCommandFailedException extends Exception
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $packageName;
|
||||
|
||||
public function __construct(string $packageName, string $output)
|
||||
{
|
||||
$this->packageName = $packageName;
|
||||
|
||||
parent::__construct($output);
|
||||
}
|
||||
}
|
@@ -12,7 +12,7 @@ class ComposerCommandFailedExceptionHandler
|
||||
{
|
||||
protected const INCOMPATIBLE_REGEX = '/ +- {PACKAGE_NAME} v[0-9.]+ requires flarum\/core/m';
|
||||
|
||||
public function handle(ComposerRequireFailedException $e): HandledError
|
||||
public function handle(ComposerCommandFailedException $e): HandledError
|
||||
{
|
||||
return (new HandledError(
|
||||
$e,
|
||||
@@ -21,7 +21,7 @@ class ComposerCommandFailedExceptionHandler
|
||||
))->withDetails($this->errorDetails($e));
|
||||
}
|
||||
|
||||
protected function errorDetails(ComposerRequireFailedException $e): array
|
||||
protected function errorDetails(ComposerCommandFailedException $e): array
|
||||
{
|
||||
$details = [
|
||||
'output' => $e->getMessage(),
|
||||
@@ -34,13 +34,14 @@ class ComposerCommandFailedExceptionHandler
|
||||
return [$details];
|
||||
}
|
||||
|
||||
protected function guessCause(ComposerRequireFailedException $e): ?string
|
||||
protected function guessCause(ComposerCommandFailedException $e): ?string
|
||||
{
|
||||
error_log(str_replace('{PACKAGE_NAME}', preg_quote($e->packageName, '/'), self::INCOMPATIBLE_REGEX));
|
||||
$hasMatches = preg_match(str_replace('{PACKAGE_NAME}', preg_quote($e->packageName, '/'), self::INCOMPATIBLE_REGEX), $e->getMessage(), $matches);
|
||||
if ($e instanceof ComposerRequireFailedException) {
|
||||
$hasMatches = preg_match(str_replace('{PACKAGE_NAME}', preg_quote($e->packageName, '/'), self::INCOMPATIBLE_REGEX), $e->getMessage(), $matches);
|
||||
|
||||
if ($hasMatches) {
|
||||
return 'extension_incompatible_with_instance';
|
||||
if ($hasMatches) {
|
||||
return 'extension_incompatible_with_instance';
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@@ -6,19 +6,7 @@
|
||||
|
||||
namespace SychO\PackageManager\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ComposerRequireFailedException extends Exception
|
||||
class ComposerRequireFailedException extends ComposerCommandFailedException
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $packageName;
|
||||
|
||||
public function __construct(string $packageName, string $output)
|
||||
{
|
||||
$this->packageName = $packageName;
|
||||
|
||||
parent::__construct($output);
|
||||
}
|
||||
// ...
|
||||
}
|
||||
|
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace SychO\PackageManager\Exception;
|
||||
|
||||
class ComposerUpdateFailedException extends ComposerCommandFailedException
|
||||
{
|
||||
// ...
|
||||
}
|
15
extensions/package-manager/src/UpdateExtensionValidator.php
Normal file
15
extensions/package-manager/src/UpdateExtensionValidator.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace SychO\PackageManager;
|
||||
|
||||
use Flarum\Foundation\AbstractValidator;
|
||||
|
||||
class UpdateExtensionValidator extends AbstractValidator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $rules = [
|
||||
'extensionId' => 'required|string'
|
||||
];
|
||||
}
|
Reference in New Issue
Block a user