Skip to content
Snippets Groups Projects
Commit 142f980b authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3501663 by jurgenhaas: Provide eca:update functionality as a service

parent 538e9b09
No related branches found
No related tags found
No related merge requests found
Pipeline #404801 passed with warnings
......@@ -111,6 +111,10 @@ services:
tags:
- { name: eca.token_data_provider, priority: -50 }
eca.update:
class: Drupal\eca\EcaUpdate
arguments: ['@entity_type.manager', '@eca.service.modeller', '@messenger']
eca.export.recipe:
class: Drupal\eca\Service\ExportRecipe
arguments:
......
......@@ -5,6 +5,7 @@ namespace Drupal\eca\Drush\Commands;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\eca\EcaUpdate;
use Drupal\eca\Service\ExportRecipe;
use Drupal\eca\Service\Modellers;
use Drush\Attributes\Argument;
......@@ -33,6 +34,7 @@ final class EcaCommands extends DrushCommands {
EntityTypeManagerInterface $entityTypeManager,
private readonly Modellers $ecaServiceModeller,
private readonly ExportRecipe $exportRecipe,
private readonly EcaUpdate $ecaUpdate,
) {
parent::__construct();
$this->configStorage = $entityTypeManager->getStorage('eca');
......@@ -52,6 +54,7 @@ final class EcaCommands extends DrushCommands {
$container->get('entity_type.manager'),
$container->get('eca.service.modeller'),
$container->get('eca.export.recipe'),
$container->get('eca.update'),
);
}
......@@ -137,27 +140,12 @@ final class EcaCommands extends DrushCommands {
#[Command(name: 'eca:update', aliases: [])]
#[Usage(name: 'eca:update', description: 'Update all models if plugins got changed.')]
public function updateAllModels(): void {
/** @var \Drupal\eca\Entity\Eca $eca */
foreach ($this->configStorage->loadMultiple() as $eca) {
$modeller = $this->ecaServiceModeller->getModeller($eca->get('modeller'));
if ($modeller === NULL) {
$this->logger->error('This modeller plugin ' . $eca->get('modeller') . ' does not exist.');
continue;
}
$model = $eca->getModel();
$modeller->setConfigEntity($eca);
if ($modeller->updateModel($model)) {
$filename = $model->getFilename();
if ($filename && file_exists($filename)) {
file_put_contents($filename, $model->getModeldata());
}
try {
$modeller->save($model->getModeldata(), $filename);
}
catch (\LogicException | EntityStorageException $e) {
$this->io()->error($e->getMessage());
}
}
$this->ecaUpdate->updateAllModels();
if ($infos = $this->ecaUpdate->getInfos()) {
$this->io()->info(implode(PHP_EOL, $infos));
}
if ($errors = $this->ecaUpdate->getErrors()) {
$this->io()->error(implode(PHP_EOL, $errors));
}
}
......
<?php
declare(strict_types=1);
namespace Drupal\eca;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\eca\Service\Modellers;
/**
* Provides methods to update all existing ECA models and to output messages.
*/
final class EcaUpdate {
/**
* ECA config entity storage manager.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected EntityStorageInterface $configStorage;
/**
* List of errors.
*
* @var array
*/
protected array $errors;
/**
* List of info messages.
*
* @var array
*/
protected array $infos;
/**
* Constructs an EcaUpdate object.
*/
public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager,
private readonly Modellers $ecaServiceModeller,
private readonly MessengerInterface $messenger,
) {
$this->configStorage = $this->entityTypeManager->getStorage('eca');
}
/**
* Updates all existing ECA entities calling ::updateModel in their modeller.
*/
public function updateAllModels(): void {
$this->errors = [];
$this->infos = [];
/** @var \Drupal\eca\Entity\Eca $eca */
foreach ($this->configStorage->loadMultiple() as $eca) {
$modeller = $this->ecaServiceModeller->getModeller($eca->get('modeller'));
if ($modeller === NULL) {
$this->errors[] = '[' . $eca->label() . '] This modeller plugin ' . $eca->get('modeller') . ' for this ECA model does not exist.';
continue;
}
$model = $eca->getModel();
$modeller->setConfigEntity($eca);
if ($modeller->updateModel($model)) {
$filename = $model->getFilename();
if ($filename && file_exists($filename)) {
file_put_contents($filename, $model->getModeldata());
}
try {
$modeller->save($model->getModeldata(), $filename);
$this->infos[] = '[' . $eca->label() . '] Model has been updated.';
}
catch (\LogicException | EntityStorageException $e) {
$this->errors[] = '[' . $eca->label() . '] Error while updating this model: ' . $e->getMessage();
}
}
else {
$this->infos[] = '[' . $eca->label() . '] Model does not require any updates.';
}
}
}
/**
* Gets the list of all collected error messages.
*
* @return array
* The list of all collected error messages.
*/
public function getErrors(): array {
return $this->errors;
}
/**
* Gets the list of all collected info messages.
*
* @return array
* The list of all collected info messages.
*/
public function getInfos(): array {
return $this->infos;
}
/**
* Outputs all messages (info and error) to the user.
*/
public function displayMessages(): void {
foreach ($this->infos ?? [] as $info) {
$this->messenger->addMessage($info);
}
foreach ($this->errors ?? [] as $error) {
$this->messenger->addError($error);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment