Skip to content

Commit

Permalink
Fixed bug that exception normalizer cannot support symfony 7 (#6755)
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed May 15, 2024
1 parent 715c7fa commit 5869d37
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-3.1.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# v3.1.22 - TBD

## Fixed

- [#6755](https://github.com/hyperf/hyperf/pull/6755) Fixed bug that exception normalizer cannot support symfony 7.

## Added

- [#6734](https://github.com/hyperf/hyperf/pull/6734) Auto complete options for as command and closure command.
Expand Down
5 changes: 2 additions & 3 deletions src/command/tests/AsCommandAndClosureCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
use Hyperf\Di\MethodDefinitionCollectorInterface;
use Hyperf\Di\ReflectionManager;
use Hyperf\Di\ScanHandler\NullScanHandler;
use Hyperf\Serializer\SerializerFactory;
use Hyperf\Serializer\SymfonyNormalizer;
use Hyperf\Serializer\SimpleNormalizer;
use HyperfTest\Command\Command\Annotation\TestAsCommand;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
Expand Down Expand Up @@ -193,7 +192,7 @@ protected function getContainer()
return $logger;
});

$container->shouldReceive('get')->with(NormalizerInterface::class)->andReturn(new SymfonyNormalizer((new SerializerFactory())->__invoke()));
$container->shouldReceive('get')->with(NormalizerInterface::class)->andReturn(new SimpleNormalizer());
$container->shouldReceive('has')->with(NormalizerInterface::class)->andReturn(true);
$container->shouldReceive('get')->with(MethodDefinitionCollectorInterface::class)->andReturn(new MethodDefinitionCollector());
$container->shouldReceive('has')->with(MethodDefinitionCollectorInterface::class)->andReturn(true);
Expand Down
25 changes: 25 additions & 0 deletions src/serializer/src/Contract/CacheableSupportsMethodInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Hyperf\Serializer\Contract;

use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as SymfonyCacheableSupportsMethodInterface;

if (interface_exists(SymfonyCacheableSupportsMethodInterface::class)) {
interface CacheableSupportsMethodInterface extends SymfonyCacheableSupportsMethodInterface
{
}
} else {
interface CacheableSupportsMethodInterface
{
}
}
16 changes: 11 additions & 5 deletions src/serializer/src/ExceptionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

namespace Hyperf\Serializer;

use ArrayObject;
use Doctrine\Instantiator\Instantiator;
use Hyperf\Di\ReflectionManager;
use Hyperf\Serializer\Contract\CacheableSupportsMethodInterface;
use ReflectionException;
use RuntimeException;
use Serializable;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Throwable;
Expand All @@ -29,7 +30,7 @@ class ExceptionNormalizer implements NormalizerInterface, DenormalizerInterface,
{
protected ?Instantiator $instantiator = null;

public function denormalize($data, string $type, ?string $format = null, array $context = [])
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
{
if (is_string($data)) {
$ex = unserialize($data);
Expand Down Expand Up @@ -68,12 +69,12 @@ public function denormalize($data, string $type, ?string $format = null, array $
return new RuntimeException('Bad data data: ' . json_encode($data));
}

public function supportsDenormalization($data, $type, $format = null)
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
return class_exists($type) && is_a($type, Throwable::class, true);
}

public function normalize($object, ?string $format = null, array $context = [])
public function normalize(mixed $object, ?string $format = null, array $context = []): null|array|ArrayObject|bool|float|int|string
{
if ($object instanceof Serializable) {
return serialize($object);
Expand All @@ -87,7 +88,7 @@ public function normalize($object, ?string $format = null, array $context = [])
];
}

public function supportsNormalization($data, ?string $format = null)
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof Throwable;
}
Expand All @@ -97,6 +98,11 @@ public function hasCacheableSupportsMethod(): bool
return get_class($this) === __CLASS__;
}

public function getSupportedTypes(?string $format): array
{
return ['object' => static::class === __CLASS__];
}

protected function getInstantiator(): Instantiator
{
if ($this->instantiator instanceof Instantiator) {
Expand Down
16 changes: 11 additions & 5 deletions src/serializer/src/ScalarNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

namespace Hyperf\Serializer;

use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use ArrayObject;
use Hyperf\Serializer\Contract\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

Expand All @@ -26,7 +27,7 @@ public function hasCacheableSupportsMethod(): bool
return get_class($this) === __CLASS__;
}

public function denormalize($data, string $type, ?string $format = null, array $context = [])
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
{
return match ($type) {
'int' => (int) $data,
Expand All @@ -37,7 +38,7 @@ public function denormalize($data, string $type, ?string $format = null, array $
};
}

public function supportsDenormalization($data, $type, ?string $format = null)
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
return in_array($type, [
'int',
Expand All @@ -49,13 +50,18 @@ public function supportsDenormalization($data, $type, ?string $format = null)
]);
}

public function normalize($object, ?string $format = null, array $context = [])
public function normalize(mixed $object, ?string $format = null, array $context = []): null|array|ArrayObject|bool|float|int|string
{
return $object;
}

public function supportsNormalization($data, ?string $format = null)
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return is_scalar($data);
}

public function getSupportedTypes(?string $format): array
{
return ['*' => static::class === __CLASS__];
}
}
1 change: 1 addition & 0 deletions src/serializer/src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ private function getNormalizer($data, ?string $format, array $context): ?Normali
continue;
}

// TODO: Use getSupportedTypes to rewrite this since Symfony 7.
if (! $normalizer instanceof CacheableSupportsMethodInterface || ! $normalizer->hasCacheableSupportsMethod()) {
$this->normalizerCache[$format][$type][$k] = false;
} elseif ($normalizer->supportsNormalization($data, $format)) {
Expand Down

0 comments on commit 5869d37

Please sign in to comment.