From ef540161f525b869df26ade97bbb5aea472bac4e Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 21 Jun 2024 16:51:29 -0300 Subject: [PATCH] Avoid circular dependency. --- dgi_fixity.module | 10 ++++--- dgi_fixity.services.yml | 7 ++--- src/FixityCheckService.php | 44 +++++------------------------ src/FixityCheckServiceInterface.php | 5 ++++ 4 files changed, 20 insertions(+), 46 deletions(-) diff --git a/dgi_fixity.module b/dgi_fixity.module index dc45a6d..6d9f783 100644 --- a/dgi_fixity.module +++ b/dgi_fixity.module @@ -17,6 +17,7 @@ use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; +use Drupal\dgi_fixity\FixityCheckServiceInterface; use Drupal\dgi_fixity\Form\SettingsForm; use Drupal\file\Plugin\Field\FieldWidget\FileWidget; use Drupal\user\Entity\User; @@ -145,15 +146,16 @@ function dgi_fixity_cron() { * Implements hook_entity_type_alter(). */ function dgi_fixity_entity_type_alter(array &$entity_types) { - /** @var \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity */ - $fixity = \Drupal::service('dgi_fixity.fixity_check'); - $supported_entity_types = $fixity->fromEntityTypes(); - foreach ($supported_entity_types as $entity_type_id) { + // XXX: Cannot reference dgi_fixity.fixity_check:fromEntityTypes() due to + // circular dependencies, as dgi_fixity.fixity_check makes use of the + // entity_type.manager that we are in the middle of trying to build. + foreach (FixityCheckServiceInterface::ENTITY_TYPES as $entity_type_id) { $entity_type = &$entity_types[$entity_type_id]; $entity_type->setLinkTemplate('fixity-audit', "/fixity/$entity_type_id/{{$entity_type_id}}"); $entity_type->setLinkTemplate('fixity-check', "/fixity/$entity_type_id/{{$entity_type_id}}/check"); $entity_type->setFormClass('fixity-check', 'Drupal\dgi_fixity\Form\CheckForm'); } + unset($entity_type); } /** diff --git a/dgi_fixity.services.yml b/dgi_fixity.services.yml index 94f42b5..08375df 100644 --- a/dgi_fixity.services.yml +++ b/dgi_fixity.services.yml @@ -1,9 +1,7 @@ services: dgi_fixity.fixity_check: class: Drupal\dgi_fixity\FixityCheckService - arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@filehash'] - calls: - - [setLoggerFactory, ['@logger.factory']] + arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@filehash', '@logger.channel.dgi_fixity'] dgi_fixity.route_subscriber: class: Drupal\dgi_fixity\Routing\FixityCheckRouteSubscriber arguments: ['@entity_type.manager', '@dgi_fixity.fixity_check'] @@ -15,6 +13,5 @@ services: tags: - { name: paramconverter } logger.channel.dgi_fixity: - class: Drupal\Core\Logger\LoggerChannel - factory: logger.factory:get + parent: logger.channel_base arguments: ['dgi_fixity'] diff --git a/src/FixityCheckService.php b/src/FixityCheckService.php index e6cc552..e56fda7 100644 --- a/src/FixityCheckService.php +++ b/src/FixityCheckService.php @@ -18,6 +18,7 @@ use Drupal\filehash\FileHash; use Drupal\media\MediaInterface; use Drupal\views\ViewExecutable; use Drupal\views\Views; +use Psr\Log\LoggerInterface; /** * Decorates the FileHash services adding additional functionality. @@ -61,13 +62,6 @@ class FixityCheckService implements FixityCheckServiceInterface { */ protected $filehash; - /** - * The logger factory. - * - * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface - */ - protected LoggerChannelFactoryInterface $loggerFactory; - /** * Constructor. */ @@ -76,46 +70,22 @@ class FixityCheckService implements FixityCheckServiceInterface { ConfigFactoryInterface $config, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, - FileHash $filehash + FileHash $filehash, + LoggerInterface $logger, ) { $this->stringTranslation = $string_translation; $this->config = $config; $this->entityTypeManager = $entity_type_manager; $this->time = $time; $this->filehash = $filehash; - } - - /** - * Sets the logger factory. - * - * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory - * The logger factory. - */ - public function setLoggerFactory(LoggerChannelFactoryInterface $loggerFactory) { - $this->loggerFactory = $loggerFactory; - } - - /** - * Gets the logger channel. - * - * @return \Psr\Log\LoggerInterface - * The logger channel. - */ - protected function getLogger() { - if (!isset($this->logger)) { - $this->logger = $this->loggerFactory->get('dgi_fixity'); - } - return $this->logger; + $this->logger = $logger; } /** * {@inheritdoc} */ public function fromEntityTypes(): array { - return [ - 'media', - 'file', - ]; + return static::ENTITY_TYPES; } /** @@ -298,10 +268,10 @@ class FixityCheckService implements FixityCheckServiceInterface { )->toString(), ]; if ($check->passed()) { - $this->getLogger()->info($message, $args); + $this->logger->info($message, $args); } else { - $this->getLogger()->error($message, $args); + $this->logger->error($message, $args); } return $check; diff --git a/src/FixityCheckServiceInterface.php b/src/FixityCheckServiceInterface.php index 347da6a..5d23148 100644 --- a/src/FixityCheckServiceInterface.php +++ b/src/FixityCheckServiceInterface.php @@ -12,6 +12,11 @@ use Drupal\views\ViewExecutable; */ interface FixityCheckServiceInterface { + const ENTITY_TYPES = [ + 'media', + 'file', + ]; + /** * A list of entity types which be converted into a fixity_check entity. *