diff --git a/CHANGELOG.md b/CHANGELOG.md index 39e1ca2..d08009a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/migrations/Version20261022100000.php b/migrations/Version20261022100000.php new file mode 100644 index 0000000..ab4b3e6 --- /dev/null +++ b/migrations/Version20261022100000.php @@ -0,0 +1,29 @@ +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'); + } +} diff --git a/src/Controller/PluginController.php b/src/Controller/PluginController.php new file mode 100644 index 0000000..a2f6945 --- /dev/null +++ b/src/Controller/PluginController.php @@ -0,0 +1,87 @@ +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, + ]); + } +} diff --git a/src/Entity/Plugin.php b/src/Entity/Plugin.php new file mode 100644 index 0000000..e938445 --- /dev/null +++ b/src/Entity/Plugin.php @@ -0,0 +1,174 @@ +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; + } +} diff --git a/src/Repository/PluginRepository.php b/src/Repository/PluginRepository.php new file mode 100644 index 0000000..b998c2a --- /dev/null +++ b/src/Repository/PluginRepository.php @@ -0,0 +1,19 @@ + + */ +class PluginRepository extends AbstractRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Plugin::class); + } +}