From ef540161f525b869df26ade97bbb5aea472bac4e Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 21 Jun 2024 16:51:29 -0300 Subject: [PATCH 1/5] 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. * From 16d176335827f22cfc4df305c0b1d8090c2d14a6 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 21 Jun 2024 16:53:45 -0300 Subject: [PATCH 2/5] Back to the original order. --- dgi_fixity.services.yml | 2 +- src/FixityCheckService.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dgi_fixity.services.yml b/dgi_fixity.services.yml index 08375df..d99d361 100644 --- a/dgi_fixity.services.yml +++ b/dgi_fixity.services.yml @@ -1,7 +1,7 @@ services: dgi_fixity.fixity_check: class: Drupal\dgi_fixity\FixityCheckService - arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@filehash', '@logger.channel.dgi_fixity'] + arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@logger.channel.dgi_fixity', '@filehash'] dgi_fixity.route_subscriber: class: Drupal\dgi_fixity\Routing\FixityCheckRouteSubscriber arguments: ['@entity_type.manager', '@dgi_fixity.fixity_check'] diff --git a/src/FixityCheckService.php b/src/FixityCheckService.php index e56fda7..343d321 100644 --- a/src/FixityCheckService.php +++ b/src/FixityCheckService.php @@ -70,15 +70,15 @@ class FixityCheckService implements FixityCheckServiceInterface { ConfigFactoryInterface $config, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, - FileHash $filehash, LoggerInterface $logger, + FileHash $filehash, ) { $this->stringTranslation = $string_translation; $this->config = $config; $this->entityTypeManager = $entity_type_manager; $this->time = $time; - $this->filehash = $filehash; $this->logger = $logger; + $this->filehash = $filehash; } /** From 25fe1c8499f5fb2ddce243ebf14808ace5475197 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 21 Jun 2024 16:58:35 -0300 Subject: [PATCH 3/5] Back to the original order. --- dgi_fixity.services.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dgi_fixity.services.yml b/dgi_fixity.services.yml index d99d361..77e3038 100644 --- a/dgi_fixity.services.yml +++ b/dgi_fixity.services.yml @@ -1,4 +1,7 @@ services: + logger.channel.dgi_fixity: + parent: logger.channel_base + arguments: [ 'dgi_fixity' ] dgi_fixity.fixity_check: class: Drupal\dgi_fixity\FixityCheckService arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@logger.channel.dgi_fixity', '@filehash'] @@ -12,6 +15,3 @@ services: arguments: ['@entity_type.manager', '@entity.repository', '@dgi_fixity.fixity_check'] tags: - { name: paramconverter } - logger.channel.dgi_fixity: - parent: logger.channel_base - arguments: ['dgi_fixity'] From 73c864218c2039997b768ce7a6edec38e2cf1076 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 21 Jun 2024 16:59:15 -0300 Subject: [PATCH 4/5] Drop extra spaces. --- dgi_fixity.services.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dgi_fixity.services.yml b/dgi_fixity.services.yml index 77e3038..f904b04 100644 --- a/dgi_fixity.services.yml +++ b/dgi_fixity.services.yml @@ -1,7 +1,7 @@ services: logger.channel.dgi_fixity: parent: logger.channel_base - arguments: [ 'dgi_fixity' ] + arguments: ['dgi_fixity'] dgi_fixity.fixity_check: class: Drupal\dgi_fixity\FixityCheckService arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@logger.channel.dgi_fixity', '@filehash'] From e16ce3044ee96af9d6b8aa24a1da9ba3b4a79d91 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 21 Jun 2024 17:06:33 -0300 Subject: [PATCH 5/5] Factory is no longer used here, directly. --- src/FixityCheckService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/FixityCheckService.php b/src/FixityCheckService.php index 343d321..7238272 100644 --- a/src/FixityCheckService.php +++ b/src/FixityCheckService.php @@ -7,7 +7,6 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Link; -use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\dgi_fixity\Entity\FixityCheck;