Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug that exception normalizer cannot support symfony 7 #6755

Merged
merged 10 commits into from
May 15, 2024
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
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
{
}
}
10 changes: 8 additions & 2 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 Down Expand Up @@ -73,7 +74,7 @@ public function supportsDenormalization($data, $type, $format = null)
return class_exists($type) && is_a($type, Throwable::class, true);
}

public function normalize($object, ?string $format = null, array $context = [])
public function normalize($object, ?string $format = null, array $context = []): null|array|ArrayObject|bool|float|int|string
huangdijia marked this conversation as resolved.
Show resolved Hide resolved
{
if ($object instanceof Serializable) {
return serialize($object);
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
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
Loading