Browse Source

Avoid circular dependency.

pull/24/head
Adam Vessey 7 months ago
parent
commit
ef540161f5
No known key found for this signature in database
GPG Key ID: 89B39535BF6D0D39
  1. 10
      dgi_fixity.module
  2. 7
      dgi_fixity.services.yml
  3. 44
      src/FixityCheckService.php
  4. 5
      src/FixityCheckServiceInterface.php

10
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);
}
/**

7
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']

44
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;

5
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.
*

Loading…
Cancel
Save