feat: refactor extension-manager extension

This commit is contained in:
Sami Mazouz 2024-03-02 12:20:39 +01:00
parent 6f942addb0
commit 3644d81829
No known key found for this signature in database
5 changed files with 56 additions and 143 deletions

View File

@ -10,6 +10,7 @@
namespace Flarum\ExtensionManager;
use Flarum\Extend;
use Flarum\ExtensionManager\Api\Resource\TaskResource;
use Flarum\Foundation\Paths;
use Flarum\Frontend\Document;
use Illuminate\Contracts\Queue\Queue;
@ -25,9 +26,10 @@ return [
->post('/extension-manager/minor-update', 'extension-manager.minor-update', Api\Controller\MinorUpdateController::class)
->post('/extension-manager/major-update', 'extension-manager.major-update', Api\Controller\MajorUpdateController::class)
->post('/extension-manager/global-update', 'extension-manager.global-update', Api\Controller\GlobalUpdateController::class)
->get('/extension-manager-tasks', 'extension-manager.tasks.index', Api\Controller\ListTasksController::class)
->post('/extension-manager/composer', 'extension-manager.composer', Api\Controller\ConfigureComposerController::class),
(new Extend\ApiResource(TaskResource::class)),
(new Extend\Frontend('admin'))
->css(__DIR__.'/less/admin.less')
->js(__DIR__.'/js/dist/admin.js')

View File

@ -1,58 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\ExtensionManager\Api\Controller;
use Flarum\Api\Controller\AbstractListController;
use Flarum\ExtensionManager\Api\Serializer\TaskSerializer;
use Flarum\ExtensionManager\Task\Task;
use Flarum\Http\RequestUtil;
use Flarum\Http\UrlGenerator;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
class ListTasksController extends AbstractListController
{
public ?string $serializer = TaskSerializer::class;
public function __construct(
protected UrlGenerator $url
) {
}
protected function data(ServerRequestInterface $request, Document $document): iterable
{
$actor = RequestUtil::getActor($request);
$actor->assertAdmin();
$limit = $this->extractLimit($request);
$offset = $this->extractOffset($request);
$results = Task::query()
->latest('id')
->offset($offset)
->limit($limit)
->get();
$total = Task::query()->count();
$document->addMeta('total', (string) $total);
$document->addPaginationLinks(
$this->url->to('api')->route('extension-manager.tasks.index'),
$request->getQueryParams(),
$offset,
$limit,
$total
);
return $results;
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace Flarum\ExtensionManager\Api\Resource;
use Flarum\Api\Endpoint;
use Flarum\Api\Resource\AbstractDatabaseResource;
use Flarum\Api\Schema;
use Flarum\Api\Sort\SortColumn;
use Flarum\ExtensionManager\Task\Task;
class TaskResource extends AbstractDatabaseResource
{
public function type(): string
{
return 'package-manager-tasks';
}
public function model(): string
{
return Task::class;
}
public function endpoints(): array
{
return [
Endpoint\Index::make()
->defaultSort('-createdAt')
->paginate(),
];
}
public function fields(): array
{
return [
Schema\Str::make('status'),
Schema\Str::make('operation'),
Schema\Str::make('command'),
Schema\Str::make('package'),
Schema\Str::make('output'),
Schema\DateTime::make('createdAt'),
Schema\DateTime::make('startedAt'),
Schema\DateTime::make('finishedAt'),
Schema\Number::make('peakMemoryUsed'),
];
}
public function sorts(): array
{
return [
SortColumn::make('createdAt'),
];
}
}

View File

@ -1,50 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\ExtensionManager\Api\Serializer;
use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\ExtensionManager\Task\Task;
use InvalidArgumentException;
class TaskSerializer extends AbstractSerializer
{
/**
* {@inheritdoc}
*/
protected $type = 'extension-manager-tasks';
/**
* {@inheritdoc}
*
* @param Task $model
* @throws InvalidArgumentException
*/
protected function getDefaultAttributes($model): array
{
if (! ($model instanceof Task)) {
throw new InvalidArgumentException(
get_class($this).' can only serialize instances of '.Task::class
);
}
return [
'status' => $model->status,
'operation' => $model->operation,
'command' => $model->command,
'package' => $model->package,
'output' => $model->output,
'guessedCause' => $model->guessed_cause,
'createdAt' => $model->created_at,
'startedAt' => $model->started_at,
'finishedAt' => $model->finished_at,
'peakMemoryUsed' => $model->peak_memory_used,
];
}
}

View File

@ -1,34 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\ExtensionManager\Task;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;
class TaskRepository
{
/**
* @return Builder
*/
public function query()
{
return Task::query();
}
/**
* @param int $id
* @param User $actor
* @return Task
*/
public function findOrFail($id, User $actor = null): Task
{
return Task::findOrFail($id);
}
}