MimeTypePass.php in Drupal 9
File
core/lib/Drupal/Core/DependencyInjection/Compiler/MimeTypePass.php
View source
<?php
namespace Drupal\Core\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface as LegacyMimeTypeGuesserInterface;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
class MimeTypePass implements CompilerPassInterface {
public function process(ContainerBuilder $container) {
$consumer = $container
->getDefinition('file.mime_type.guesser');
$tag = 'mime_type_guesser';
$interface = MimeTypeGuesserInterface::class;
$deprecated_interface = LegacyMimeTypeGuesserInterface::class;
$handlers = [];
foreach ($container
->findTaggedServiceIds($tag) as $id => $attributes) {
$handler = $container
->getDefinition($id);
if (!is_subclass_of($handler
->getClass(), $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));
}
arsort($handlers, SORT_NUMERIC);
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);
}
}
}
}
Classes
Name |
Description |
MimeTypePass Deprecated |
Adds @mime_type_guesser tagged services to handle forwards compatibility. |