feat(plugins): implement modular plugin infrastructure and bump version to 1.13.0
ogcore-debian-package/pipeline/head This commit looks good Details
ogcore-debian-package/pipeline/tag This commit looks good Details
testing/ogcore-api/pipeline/head There was a failure building this commit Details

devel
Juan Manuel Bardallo 2026-10-22 09:40:35 +02:00
parent 35b75c5823
commit 7f0953b6d1
5 changed files with 318 additions and 0 deletions

View File

@ -1,5 +1,14 @@
# Changelog
## [1.13.0] - 2026-10-22
### Added
- **Infraestructura Modular de Plugins (`Plugin`, `PluginRepository`, `PluginController`, `og_plugins`)**:
- Incorporada la arquitectura base y endpoints agnósticos (`/api/plugins`, `/api/plugins/{name}`, `/api/plugins/{name}/config`) para soportar extensiones y plugins desacoplados (como el próximo plugin de congelación de puestos `ogfreeze`).
- La nueva tabla `og_plugins` almacena de forma dinámica comandos inyectables en la consola de comandos, esquemas de configuración JSON autogenerados y parámetros persistentes de cada plugin.
- Comportamiento 100% neutro e idéntico a 1.12.0 si la tabla de plugins se encuentra vacía.
- Creada migración de base de datos (`Version20261022100000`).
## [1.12.0] - 2026-10-20
### Added

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20261022100000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create og_plugins table for modular plugin infrastructure';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE og_plugins (id INT AUTO_INCREMENT NOT NULL, uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', migration_id VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, created_by VARCHAR(255) DEFAULT NULL, updated_by VARCHAR(255) DEFAULT NULL, name VARCHAR(255) NOT NULL, title VARCHAR(255) NOT NULL, version VARCHAR(50) NOT NULL, description LONGTEXT DEFAULT NULL, author VARCHAR(255) DEFAULT NULL, icon VARCHAR(100) DEFAULT NULL, enabled TINYINT(1) NOT NULL, commands JSON DEFAULT NULL COMMENT \'(DC2Type:json)\', schema_definition JSON DEFAULT NULL COMMENT \'(DC2Type:json)\', config_data JSON DEFAULT NULL COMMENT \'(DC2Type:json)\', UNIQUE INDEX UNIQ_OG_PLUGIN_UUID (uuid), UNIQUE INDEX UNIQ_OG_PLUGIN_NAME (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}
public function down(Schema $schema): void
{
$this->addSql('DROP TABLE og_plugins');
}
}

View File

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\Plugin;
use App\Repository\PluginRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[Route('/api/plugins')]
#[IsGranted('ROLE_USER')]
class PluginController extends AbstractController
{
public function __construct(
private readonly PluginRepository $pluginRepository,
) {
}
#[Route('', name: 'api_plugins_list', methods: ['GET'])]
public function list(): JsonResponse
{
$plugins = $this->pluginRepository->findBy(['enabled' => true], ['name' => 'ASC']);
$data = array_map(static fn(Plugin $plugin) => $plugin->toArray(), $plugins);
return new JsonResponse($data);
}
#[Route('/{name}', name: 'api_plugins_get', methods: ['GET'])]
public function getOne(string $name): JsonResponse
{
$plugin = $this->pluginRepository->findOneBy(['name' => $name]);
if (!$plugin) {
return new JsonResponse(['message' => sprintf('Plugin "%s" no encontrado.', $name)], Response::HTTP_NOT_FOUND);
}
return new JsonResponse($plugin->toArray());
}
#[Route('/{name}/config', name: 'api_plugins_get_config', methods: ['GET'])]
public function getConfig(string $name): JsonResponse
{
$plugin = $this->pluginRepository->findOneBy(['name' => $name]);
if (!$plugin) {
return new JsonResponse(['message' => sprintf('Plugin "%s" no encontrado.', $name)], Response::HTTP_NOT_FOUND);
}
$config = $plugin->getConfigData() ?? [];
$defaultConfig = $plugin->toArray()['defaultConfig'] ?? [];
return new JsonResponse(array_merge($defaultConfig, $config));
}
#[Route('/{name}/config', name: 'api_plugins_update_config', methods: ['POST', 'PUT'])]
public function updateConfig(string $name, Request $request): JsonResponse
{
$plugin = $this->pluginRepository->findOneBy(['name' => $name]);
if (!$plugin) {
return new JsonResponse(['message' => sprintf('Plugin "%s" no encontrado.', $name)], Response::HTTP_NOT_FOUND);
}
$body = json_decode($request->getContent(), true);
if (!is_array($body)) {
return new JsonResponse(['message' => 'Cuerpo de la petición inválido (debe ser un objeto JSON).'], Response::HTTP_BAD_REQUEST);
}
$currentConfig = $plugin->getConfigData() ?? [];
$newConfig = array_merge($currentConfig, $body);
$plugin->setConfigData($newConfig);
$this->pluginRepository->save($plugin, true);
return new JsonResponse([
'message' => 'Configuración actualizada correctamente.',
'config' => $newConfig,
]);
}
}

174
src/Entity/Plugin.php Normal file
View File

@ -0,0 +1,174 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\PluginRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: PluginRepository::class)]
#[ORM\Table(name: 'og_plugins')]
class Plugin extends AbstractEntity
{
use NameableTrait;
use ToggleableTrait;
#[ORM\Column(length: 255)]
private string $title = '';
#[ORM\Column(length: 50)]
private string $version = '1.0.0';
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $author = null;
#[ORM\Column(length: 100, nullable: true)]
private ?string $icon = null;
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $commands = null;
#[ORM\Column(name: 'schema_definition', type: Types::JSON, nullable: true)]
private ?array $schemaDefinition = null;
#[ORM\Column(name: 'config_data', type: Types::JSON, nullable: true)]
private ?array $configData = null;
public function __construct()
{
parent::__construct();
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): static
{
$this->title = $title;
return $this;
}
public function getVersion(): string
{
return $this->version;
}
public function setVersion(string $version): static
{
$this->version = $version;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getAuthor(): ?string
{
return $this->author;
}
public function setAuthor(?string $author): static
{
$this->author = $author;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): static
{
$this->icon = $icon;
return $this;
}
public function getCommands(): ?array
{
return $this->commands;
}
public function setCommands(?array $commands): static
{
$this->commands = $commands;
return $this;
}
public function getSchemaDefinition(): ?array
{
return $this->schemaDefinition;
}
public function setSchemaDefinition(?array $schemaDefinition): static
{
$this->schemaDefinition = $schemaDefinition;
return $this;
}
public function getConfigData(): ?array
{
return $this->configData;
}
public function setConfigData(?array $configData): static
{
$this->configData = $configData;
return $this;
}
public function toArray(): array
{
return [
'id' => $this->id,
'uuid' => (string) $this->uuid,
'name' => $this->name,
'title' => $this->title,
'version' => $this->version,
'description' => $this->description,
'author' => $this->author,
'icon' => $this->icon,
'enabled' => $this->enabled,
'commands' => $this->commands ?? [],
'schema' => $this->schemaDefinition ?? [],
'config' => $this->configData ?? [],
'defaultConfig' => $this->extractDefaultConfig(),
];
}
private function extractDefaultConfig(): array
{
$defaults = [];
if (is_array($this->schemaDefinition)) {
foreach ($this->schemaDefinition as $field) {
if (isset($field['key']) && array_key_exists('default', $field)) {
$defaults[$field['key']] = $field['default'];
}
}
}
return $defaults;
}
}

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Entity\Plugin;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends AbstractRepository<Plugin>
*/
class PluginRepository extends AbstractRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Plugin::class);
}
}