mirror of
https://github.com/flarum/core.git
synced 2025-08-15 12:54:47 +02:00
Add Update Checker
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace SychO\PackageManager\Api\Controller;
|
||||
|
||||
use Flarum\Http\RequestUtil;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Illuminate\Support\Arr;
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use SychO\PackageManager\Command\CheckForUpdates;
|
||||
|
||||
class CheckForUpdatesController implements RequestHandlerInterface
|
||||
{
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $bus;
|
||||
|
||||
public function __construct(Dispatcher $bus)
|
||||
{
|
||||
$this->bus = $bus;
|
||||
}
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$actor = RequestUtil::getActor($request);
|
||||
|
||||
$lastUpdateCheck = $this->bus->dispatch(
|
||||
new CheckForUpdates($actor)
|
||||
);
|
||||
|
||||
return new JsonResponse($lastUpdateCheck);
|
||||
}
|
||||
}
|
22
extensions/package-manager/src/Command/CheckForUpdates.php
Normal file
22
extensions/package-manager/src/Command/CheckForUpdates.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace SychO\PackageManager\Command;
|
||||
|
||||
use Flarum\User\User;
|
||||
|
||||
class CheckForUpdates
|
||||
{
|
||||
/**
|
||||
* @var \Flarum\User\User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
public function __construct(User $actor)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
}
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace SychO\PackageManager\Command;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Composer\Console\Application;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\BufferedOutput;
|
||||
|
||||
class CheckForUpdatesHandler
|
||||
{
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
protected $composer;
|
||||
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @param Application $composer
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
*/
|
||||
public function __construct(Application $composer, SettingsRepositoryInterface $settings)
|
||||
{
|
||||
$this->composer = $composer;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Flarum\User\Exception\PermissionDeniedException
|
||||
*/
|
||||
public function handle(CheckForUpdates $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
|
||||
$actor->assertAdmin();
|
||||
|
||||
$output = new BufferedOutput();
|
||||
$input = new ArrayInput([
|
||||
'command' => 'outdated',
|
||||
'-D' => true,
|
||||
'--format' => 'json',
|
||||
]);
|
||||
|
||||
$this->composer->run($input, $output);
|
||||
|
||||
$lastUpdateCheck = [
|
||||
'checkedAt' => Carbon::now(),
|
||||
'updates' => json_decode($output->fetch(), true),
|
||||
];
|
||||
|
||||
$this->settings->set('sycho-package-manager.last_update_check', json_encode($lastUpdateCheck));
|
||||
|
||||
return $lastUpdateCheck;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user