mirror of
https://github.com/flarum/framework.git
synced 2024-12-11 13:05:50 +08:00
feat: refactor extension-manager extension
This commit is contained in:
parent
6f942addb0
commit
3644d81829
|
@ -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')
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
53
extensions/package-manager/src/Api/Resource/TaskResource.php
Normal file
53
extensions/package-manager/src/Api/Resource/TaskResource.php
Normal 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'),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user