You are here

public function MimeTypePass::process in Drupal 9

File

core/lib/Drupal/Core/DependencyInjection/Compiler/MimeTypePass.php, line 27

Class

MimeTypePass
Adds @mime_type_guesser tagged services to handle forwards compatibility.

Namespace

Drupal\Core\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) {
  $consumer = $container
    ->getDefinition('file.mime_type.guesser');
  $tag = 'mime_type_guesser';
  $interface = MimeTypeGuesserInterface::class;
  $deprecated_interface = LegacyMimeTypeGuesserInterface::class;

  // Find all tagged handlers.
  $handlers = [];
  foreach ($container
    ->findTaggedServiceIds($tag) as $id => $attributes) {

    // Validate the interface.
    $handler = $container
      ->getDefinition($id);
    if (!is_subclass_of($handler
      ->getClass(), $interface)) {

      // Special handling for $deprecated_interface.
      if (!is_subclass_of($handler
        ->getClass(), $deprecated_interface)) {
        throw new LogicException("Service '{$id}' does not implement {$interface}.");
      }
    }
    $handlers[$id] = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
    $interfaces[$id] = $handler
      ->getClass();
  }
  if (empty($handlers)) {
    throw new LogicException(sprintf("At least one service tagged with '%s' is required.", $tag));
  }

  // Sort all handlers by priority.
  arsort($handlers, SORT_NUMERIC);

  // Add a method call for each handler to the consumer service
  // definition.
  foreach ($handlers as $id => $priority) {
    $arguments = [
      new Reference($id),
      $priority,
    ];
    if (is_subclass_of($interfaces[$id], $interface)) {
      $consumer
        ->addMethodCall('addMimeTypeGuesser', $arguments);
    }
    else {
      $consumer
        ->addMethodCall('addGuesser', $arguments);
    }
  }
}