diff --git a/core/core.services.yml b/core/core.services.yml index f44783f..47f5c19 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1179,7 +1179,8 @@ services: alias: plugin.manager.element_info file.mime_type.mapper: class: Drupal\Core\File\MimeType\MimeTypeMapper - arguments: ['@module_handler'] + calls: + - [alterMapping, ['@module_handler']] file.mime_type.guesser: class: Drupal\Core\File\MimeType\MimeTypeGuesser tags: diff --git a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php index 8962393..680e94d 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php @@ -863,57 +863,34 @@ class MimeTypeMapper implements MimeTypeMapperInterface { ); /** - * Track if mapping has been altered already. - * - * @see hook_file_mimetype_mapping_alter() + * Track if mapping has been already altered by modules. * * @var bool */ protected $isMappingAltered = FALSE; /** - * The module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** - * Constructs a new MimeTypeMapper. - * - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. + * {@inheritdoc} */ - public function __construct(ModuleHandlerInterface $module_handler) { - $this->moduleHandler = $module_handler; + public function alterMapping(ModuleHandlerInterface $module_handler) { + if (!$this->isMappingAltered) { + $module_handler->alter('file_mimetype_mapping', $this); + $this->isMappingAltered = TRUE; + } + return $this; } /** * Returns the mapping from file extensions to appropriate MIME types. * - * Invokes hook_file_mimetype_mapping_alter() to allow modules to alter - * the mapping. - * * @return array * An array consisting of two arrays: * - mimetypes: MIME types, keyed by a unique number. * - extensions: an associative array with the MIME type key numbers as * values. The keys are file extensions, in lower case and without any * preceding dot. - * - * @see hook_file_mimetype_mapping_alter() */ protected function getMapping() { - static $altering = FALSE; - - // Allow modules to alter the default mapping. - if (!$this->isMappingAltered && !$altering) { - $altering = TRUE; - $this->moduleHandler->alter('file_mimetype_mapping', $this); - $this->isMappingAltered = TRUE; - $altering = FALSE; - } - return $this->mapping; } @@ -921,10 +898,6 @@ protected function getMapping() { * {@inheritdoc} */ public function addMapping($mimetype, $extension) { - // Calls ::getMapping() to make sure new mappings are added to the - // ::$mapping array that has been altered by modules already. - $this->getMapping(); - if (!in_array($mimetype, $this->mapping['mimetypes'])) { $this->mapping['mimetypes'][] = $mimetype; } @@ -938,10 +911,6 @@ public function addMapping($mimetype, $extension) { * {@inheritdoc} */ public function removeMapping($extension) { - // Calls ::getMapping() to make sure mappings are removed from the - // ::$mapping array that has been altered by modules already. - $this->getMapping(); - $extension = strtolower($extension); if (isset($this->mapping['extensions'][$extension])) { unset($this->mapping['extensions'][$extension]); @@ -954,10 +923,6 @@ public function removeMapping($extension) { * {@inheritdoc} */ public function removeMimeType($mimetype) { - // Calls ::getMapping() to make sure mappings are removed from the - // ::$mapping array that has been altered by modules already. - $this->getMapping(); - if (!in_array($mimetype, $this->mapping['mimetypes'])) { return FALSE; } diff --git a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php index 6772cb2..1486c00 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php @@ -6,12 +6,28 @@ namespace Drupal\Core\File\MimeType; +use Drupal\Core\Extension\ModuleHandlerInterface; + /** * Provides a sensible mapping between filename extensions and MIME types. */ interface MimeTypeMapperInterface { /** + * Allow modules to alter the default mapping. + * + * Invokes hook_file_mimetype_mapping_alter(). + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + * + * @return $this + * + * @see hook_file_mimetype_mapping_alter() + */ + public function alterMapping(ModuleHandlerInterface $module_handler); + + /** * Adds a mapping between a MIME type and an extension. * * @param string $mimetype