Skip to content

Commit

Permalink
[Serializer] symfony/mime isn't actually required for DataUriNormalizer
Browse files Browse the repository at this point in the history
only to create a default mime type guesser.
If none is provided, it'll always fallback to 'application/octet-stream'.
  • Loading branch information
ogizanagi committed Jun 1, 2019
1 parent 188a7e0 commit fc17011
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion UPGRADE-5.0.md
Expand Up @@ -403,7 +403,7 @@ Serializer

were removed, use the default context instead.
* The `AbstractNormalizer::handleCircularReference()` method has two new `$format` and `$context` arguments.
* Removed support for instantiating a `DataUriNormalizer` without the `symfony/mime` component installed.
* Removed support for instantiating a `DataUriNormalizer` with a default MIME type guesser when the `symfony/mime` component isn't installed.

Translation
-----------
Expand Down
Expand Up @@ -41,7 +41,6 @@
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\TransportFactory;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
Expand Down Expand Up @@ -1167,10 +1166,6 @@ public function testRegisterSerializerExtractor()

public function testDataUriNormalizerRegistered()
{
if (!class_exists(MimeTypeGuesserInterface::class)) {
$this->markTestSkipped('Skipped as symfony/mime is not installed.');
}

$container = $this->createContainerFromFile('full');

$definition = $container->getDefinition('serializer.normalizer.data_uri');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/CHANGELOG.md
Expand Up @@ -16,7 +16,7 @@ CHANGELOG
use the default context instead.
* removed `XmlEncoder::setRootNodeName()` & `XmlEncoder::getRootNodeName()`, use the default context instead.
* removed individual encoders/normalizers options as constructor arguments.
* removed support for instantiating a `DataUriNormalizer` without the `symfony/mime` component installed
* removed support for instantiating a `DataUriNormalizer` with a default MIME type guesser when the `symfony/mime` component isn't installed.

4.3.0
-----
Expand Down
Expand Up @@ -32,17 +32,17 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C
];

/**
* @var MimeTypeGuesserInterface
* @var MimeTypeGuesserInterface|null
*/
private $mimeTypeGuesser;

public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
{
if (!$mimeTypeGuesser && !class_exists(MimeTypes::class)) {
throw new InvalidArgumentException(sprintf('Cannot pass null to "%s()" without symfony/mime installed, install symfony/mime to use a default mime type guesser.', __METHOD__));
if (!$mimeTypeGuesser && class_exists(MimeTypes::class)) {
$mimeTypeGuesser = MimeTypes::getDefault();
}

$this->mimeTypeGuesser = $mimeTypeGuesser ?? MimeTypes::getDefault();
$this->mimeTypeGuesser = $mimeTypeGuesser;
}

/**
Expand Down Expand Up @@ -144,7 +144,11 @@ private function getMimeType(\SplFileInfo $object)
return $object->getMimeType();
}

return $this->mimeTypeGuesser->guessMimeType($object->getPathname()) ?? 'application/octet-stream';
if ($this->mimeTypeGuesser && $mimeType = $this->mimeTypeGuesser->guessMimeType($object->getPathname())) {
return $mimeType;
}

return 'application/octet-stream';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/composer.json
Expand Up @@ -46,7 +46,7 @@
"symfony/yaml": "For using the default YAML mapping loader.",
"symfony/config": "For using the XML mapping loader.",
"symfony/property-access": "For using the ObjectNormalizer.",
"symfony/mime": "For using the DataUriNormalizer.",
"symfony/mime": "For using a MIME type guesser within the DataUriNormalizer.",
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
"doctrine/cache": "For using the default cached annotation reader and metadata cache."
},
Expand Down

0 comments on commit fc17011

Please sign in to comment.