diff --git a/core/modules/migrate/migrate.services.yml b/core/modules/migrate/migrate.services.yml index eecce2a8d1..9e1f8e94c7 100644 --- a/core/modules/migrate/migrate.services.yml +++ b/core/modules/migrate/migrate.services.yml @@ -43,4 +43,4 @@ services: class: Drupal\migrate\EventSubscriber\MemoryLimitExceeded tags: - { name: 'event_subscriber' } - arguments: ['@entity_type.manager', '@string_translation'] + arguments: ['@entity_type.manager'] diff --git a/core/modules/migrate/src/Event/MigrateMemoryLimitEvent.php b/core/modules/migrate/src/Event/MigrateMemoryLimitEvent.php index 12f3eeb2ea..bf700987c5 100644 --- a/core/modules/migrate/src/Event/MigrateMemoryLimitEvent.php +++ b/core/modules/migrate/src/Event/MigrateMemoryLimitEvent.php @@ -9,53 +9,19 @@ */ class MigrateMemoryLimitEvent extends Event { - /** - * The memory usage ratio. - * - * @var float - */ - protected $usageRatio; - - /** - * The memory usage in bytes. - * - * @var int - */ - protected $usageInBytes; - - /** - * The memory limit in bytes. - * - * @var int - */ - protected $limit; - - /** - * The phase of memory reclamation. - * - * The phases are defined in \Drupal\migrate\MemoryManagerInterface. - * - * @var string - */ - protected $phase; - /** * Constructs a memory limit event object. * - * @param float $usage_ratio - * The usage ratio. - * @param int $usage_in_bytes + * @param float $usageRatio + * The memory usage ratio. + * @param int $usageInBytes * The memory usage in bytes. * @param int $limit * The memory usage limit in bytes. * @param string $phase * The phase of memory reclamation. */ - public function __construct($usage_ratio, $usage_in_bytes, $limit, $phase) { - $this->usageRatio = $usage_ratio; - $this->usageInBytes = $usage_in_bytes; - $this->limit = $limit; - $this->phase = $phase; + public function __construct(protected float $usageRatio, protected int $usageInBytes, protected int $limit, protected string $phase) { } /** @@ -64,7 +30,7 @@ public function __construct($usage_ratio, $usage_in_bytes, $limit, $phase) { * @return float * The memory usage ratio. */ - public function getUsageRatio() { + public function getUsageRatio(): float { return $this->usageRatio; } @@ -74,7 +40,7 @@ public function getUsageRatio() { * @return int * The memory usage in bytes. */ - public function getUsageInBytes() { + public function getUsageInBytes(): int { return $this->usageInBytes; } @@ -84,7 +50,7 @@ public function getUsageInBytes() { * @return int * The memory limit in bytes. */ - public function getLimit() { + public function getLimit(): int { return $this->limit; } @@ -94,7 +60,7 @@ public function getLimit() { * @return string * The phase of memory reclamation. */ - public function getPhase() { + public function getPhase(): string { return $this->phase; } diff --git a/core/modules/migrate/src/EventSubscriber/MemoryLimitExceeded.php b/core/modules/migrate/src/EventSubscriber/MemoryLimitExceeded.php index 6ef4592464..8e8e7e9608 100644 --- a/core/modules/migrate/src/EventSubscriber/MemoryLimitExceeded.php +++ b/core/modules/migrate/src/EventSubscriber/MemoryLimitExceeded.php @@ -3,8 +3,7 @@ namespace Drupal\migrate\EventSubscriber; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\migrate\Event\MigrateEvents; use Drupal\migrate\Event\MigrateMemoryLimitEvent; use Drupal\migrate\MemoryManagerInterface; @@ -16,8 +15,6 @@ */ class MemoryLimitExceeded implements EventSubscriberInterface { - use StringTranslationTrait; - /** * The migrate message class. * @@ -26,31 +23,13 @@ class MemoryLimitExceeded implements EventSubscriberInterface { protected $message; /** - * The entity type manager. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected $entityTypeManager; - - /** - * The translation manager. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** - * MemoryLimitExceeded constructor. + * Constructs MemoryLimitExceeded object. * - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager - * The translation manager. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $translation_manager) { - $this->entityTypeManager = $entity_type_manager; + public function __construct(protected EntityTypeManagerInterface $entityTypeManager) { $this->message = new MigrateMessage(); - $this->setStringTranslation($translation_manager); } /** @@ -71,7 +50,7 @@ public static function getSubscribedEvents() { * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ - public function reclaim(MigrateMemoryLimitEvent $event) { + public function reclaim(MigrateMemoryLimitEvent $event): void { if ($event->getPhase() != MemoryManagerInterface::PRE_RECLAIMED) { return; } @@ -96,7 +75,7 @@ public function reclaim(MigrateMemoryLimitEvent $event) { * @param \Drupal\migrate\Event\MigrateMemoryLimitEvent $event * A migrate memory limit event. */ - public function notify(MigrateMemoryLimitEvent $event) { + public function notify(MigrateMemoryLimitEvent $event): void { $results = [ '@pct' => round($event->getUsageRatio() * 100), '@usage' => format_size($event->getUsageInBytes()), @@ -104,18 +83,18 @@ public function notify(MigrateMemoryLimitEvent $event) { ]; switch ($event->getPhase()) { case MemoryManagerInterface::PRE_RECLAIMED: - $this->message->display($this->t( + $this->message->display(new TranslatableMarkup( 'Memory usage is @usage (@pct% of limit @limit), reclaiming memory.', $results), 'warning'); break; case MemoryManagerInterface::STILL_EXCEEDED: - $this->message->display($this->t( + $this->message->display(new TranslatableMarkup( 'Memory usage is now @usage (@pct% of limit @limit), not enough reclaimed, starting new batch', $results), 'warning'); break; case MemoryManagerInterface::REDUCED_ENOUGH_TO_CONTINUE: case '': - $this->message->display($this->t( + $this->message->display(new TranslatableMarkup( 'Memory usage is now @usage (@pct% of limit @limit), reclaimed enough, continuing', $results)); break; } diff --git a/core/modules/migrate/src/MemoryManager.php b/core/modules/migrate/src/MemoryManager.php index 7431c54b85..ddf00abc9f 100644 --- a/core/modules/migrate/src/MemoryManager.php +++ b/core/modules/migrate/src/MemoryManager.php @@ -12,66 +12,42 @@ */ class MemoryManager implements MemoryManagerInterface { - /** - * The ratio of the memory limit which will trigger a failed reclaim. - * - * @var float - */ - protected $memoryReclaimThreshold; - - /** - * The ratio of the memory limit at which an operation will be interrupted. - * - * @var float - */ - protected $memoryThreshold; - /** * The PHP memory limit expressed in bytes. * * @var int */ - protected $memoryLimit; - - /** - * The event dispatcher. - * - * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface - */ - protected $dispatcher; + protected int $memoryLimit; /** * MemoryManager constructor. * * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher * The event dispatcher. - * @param int $reclaim_threshold + * @param int $memoryReclaimThreshold * The ratio of the memory limit which will trigger a failed reclaim. - * @param int $memory_threshold + * @param int $memoryThreshold * The ratio of the memory limit at which an operation will be interrupted. - * @param int|string $memory_limit + * @param int|string|null $memory_limit * The memory limit, either an integer or string size expressed as a number * of bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, * 10G, 6GiB, 8 bytes, 9mbytes). */ - public function __construct(EventDispatcherInterface $dispatcher, $reclaim_threshold, $memory_threshold, $memory_limit = NULL) { - $this->dispatcher = $dispatcher; - $this->memoryReclaimThreshold = $reclaim_threshold; - $this->memoryThreshold = $memory_threshold; - // Record the memory limit in bytes. - if (empty($memory_limit)) { + public function __construct(protected EventDispatcherInterface $dispatcher, protected int $memoryReclaimThreshold, protected float $memoryThreshold, $memory_limit = NULL) { + if ($memory_limit === NULL) { // Auto detect memory limit. $memory_limit = trim(ini_get('memory_limit')); } + // Record the memory limit in bytes. $this->memoryLimit = ($memory_limit == -1) ? PHP_INT_MAX - : Bytes::toNumber($memory_limit); + : (int) Bytes::toNumber($memory_limit); } /** * {@inheritdoc} */ - public function ensureMemory() { + public function ensureMemory(): bool { if (!$this->isLimitExceeded()) { return TRUE; } @@ -92,7 +68,7 @@ public function ensureMemory() { * @param string $phase * The memory phase. */ - protected function dispatchEvent($phase) { + protected function dispatchEvent(string $phase): void { $event = new MigrateMemoryLimitEvent($this->getUsageRatio(), $this->getUsageInBytes(), $this->getLimit(), $phase); $this->dispatcher->dispatch($event, MigrateEvents::MEMORY_LIMIT); } @@ -103,7 +79,7 @@ protected function dispatchEvent($phase) { * @return int * The memory usage limit in bytes. */ - protected function getLimit() { + protected function getLimit(): int { return $this->memoryLimit; } @@ -113,7 +89,7 @@ protected function getLimit() { * @return int * The memory usage. */ - protected function getUsageInBytes() { + protected function getUsageInBytes(): int { return memory_get_usage(); } @@ -123,7 +99,7 @@ protected function getUsageInBytes() { * @return float|int * The amount of memory used as a percentage. */ - protected function getUsageRatio() { + protected function getUsageRatio(): float|int { return $this->getUsageInBytes() / $this->getLimit(); } @@ -138,7 +114,7 @@ protected function getUsageRatio() { * @return bool * TRUE if the memory limit is exceeded, otherwise FALSE. */ - protected function isLimitExceeded($multiplier = 1.0) { + protected function isLimitExceeded($multiplier = 1.0): bool { if (!$this->memoryThreshold) { return FALSE; } diff --git a/core/modules/migrate/src/MemoryManagerInterface.php b/core/modules/migrate/src/MemoryManagerInterface.php index 00ba0e94c9..8fcb152327 100644 --- a/core/modules/migrate/src/MemoryManagerInterface.php +++ b/core/modules/migrate/src/MemoryManagerInterface.php @@ -34,6 +34,6 @@ interface MemoryManagerInterface { * TRUE if memory usage is low enough, or if enough memory can be reclaimed. * FALSE if memory usage is too high and not enough can be reclaimed. */ - public function ensureMemory(); + public function ensureMemory(): bool; } diff --git a/core/modules/migrate/src/MigrateExecutable.php b/core/modules/migrate/src/MigrateExecutable.php index 6507b58f38..d05422bb50 100644 --- a/core/modules/migrate/src/MigrateExecutable.php +++ b/core/modules/migrate/src/MigrateExecutable.php @@ -39,13 +39,6 @@ class MigrateExecutable implements MigrateExecutableInterface { */ protected $sourceRowStatus; - /** - * The memory manager. - * - * @var \Drupal\migrate\MemoryManagerInterface - */ - protected $memoryManager; - /** * The configuration values of the source. * @@ -92,15 +85,18 @@ class MigrateExecutable implements MigrateExecutableInterface { * (optional) The migrate message service. * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher * (optional) The event dispatcher. - * @param \Drupal\Migrate\MemoryManagerInterface $memory_manager + * @param \Drupal\Migrate\MemoryManagerInterface|null $memoryManager * (optional) The memory manager. */ - public function __construct(MigrationInterface $migration, MigrateMessageInterface $message = NULL, EventDispatcherInterface $event_dispatcher = NULL, MemoryManagerInterface $memory_manager = NULL) { + public function __construct(MigrationInterface $migration, MigrateMessageInterface $message = NULL, EventDispatcherInterface $event_dispatcher = NULL, protected ?MemoryManagerInterface $memoryManager = NULL) { $this->migration = $migration; $this->message = $message ?: new MigrateMessage(); $this->getIdMap()->setMessage($this->message); $this->eventDispatcher = $event_dispatcher; - $this->memoryManager = $memory_manager; + if ($this->memoryManager === NULL) { + @trigger_error('Calling ' . __METHOD__ . '() without the $memoryManager argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3346839', E_USER_DEPRECATED); + $this->memoryManager = \Drupal::service('migrate.memory_manager'); + } } /** @@ -131,19 +127,6 @@ protected function getEventDispatcher() { return $this->eventDispatcher; } - /** - * Gets the migration memory manager. - * - * @return \Drupal\migrate\MemoryManagerInterface - * The migration memory manager. - */ - protected function getMemoryManager() { - if (!$this->memoryManager) { - $this->memoryManager = \Drupal::service('migrate.memory_manager'); - } - return $this->memoryManager; - } - /** * {@inheritdoc} */ @@ -513,7 +496,7 @@ protected function handleException(\Exception $exception, $save = TRUE) { * Checks for exceptional conditions, and display feedback. */ protected function checkStatus() { - return $this->getMemoryManager()->ensureMemory() + return $this->memoryManager->ensureMemory() ? MigrationInterface::RESULT_COMPLETED : MigrationInterface::RESULT_INCOMPLETE; }