Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / imagemagick / src / ImagemagickMimeTypeMapper.php
1 <?php
2
3 namespace Drupal\imagemagick;
4
5 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
6
7 /**
8  * Maps MIME types to file extensions.
9  */
10 class ImagemagickMimeTypeMapper {
11
12   /**
13    * The extension MIME type guesser.
14    *
15    * @var \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser
16    */
17   protected $mimeTypeGuesser;
18
19   /**
20    * The MIME types mapping array after going through the module handler.
21    *
22    * Copied via Reflection from
23    * \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser.
24    *
25    * @var array
26    */
27   protected $mapping;
28
29   /**
30    * Constructs an ImagemagickMimeTypeMapper object.
31    *
32    * @param \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $extension_mimetype_guesser
33    *   The extension MIME type guesser.
34    */
35   public function __construct(MimeTypeGuesserInterface $extension_mimetype_guesser) {
36     $this->mimeTypeGuesser = $extension_mimetype_guesser;
37   }
38
39   /**
40    * Returns the MIME types mapping array from ExtensionMimeTypeGuesser.
41    *
42    * Copied via Reflection from
43    * \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser.
44    *
45    * @return array
46    *   The MIME types mapping array.
47    */
48   protected function getMapping() {
49     if (!$this->mapping) {
50       // Guess a fake file name just to ensure the guesser loads any mapping
51       // alteration through the hooks.
52       $this->mimeTypeGuesser->guess('fake.png');
53       // Use Reflection to get a copy of the protected $mapping property in the
54       // guesser class. Get the proxied service first, then the actual mapping.
55       $reflection = new \ReflectionObject($this->mimeTypeGuesser);
56       $proxied_service = $reflection->getProperty('service');
57       $proxied_service->setAccessible(TRUE);
58       $service = $proxied_service->getValue(clone $this->mimeTypeGuesser);
59       $reflection = new \ReflectionObject($service);
60       $reflection_mapping = $reflection->getProperty('mapping');
61       $reflection_mapping->setAccessible(TRUE);
62       $this->mapping = $reflection_mapping->getValue(clone $service);
63     }
64     return $this->mapping;
65   }
66
67   /**
68    * Returns the appropriate extensions for a given MIME type.
69    *
70    * @param string $mimetype
71    *   A MIME type.
72    *
73    * @return string[]
74    *   An array of file extensions matching the MIME type, without leading dot.
75    */
76   public function getExtensionsForMimeType($mimetype) {
77     $mapping = $this->getMapping();
78     if (!in_array($mimetype, $mapping['mimetypes'])) {
79       return [];
80     }
81     $key = array_search($mimetype, $mapping['mimetypes']);
82     $extensions = array_keys($mapping['extensions'], $key, TRUE);
83     sort($extensions);
84     return $extensions;
85   }
86
87   /**
88    * Returns known MIME types.
89    *
90    * @return string[]
91    *   An array of MIME types.
92    */
93   public function getMimeTypes() {
94     return array_values($this->getMapping()['mimetypes']);
95   }
96
97 }