From 344a2b97af00bb483543762311a07a2f5297b69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Mon, 4 Sep 2023 18:55:38 +0200 Subject: [PATCH] qa: deprecate `Laminas\Serializer\Serializer` and backport `GenericSerializerFactory` for forward-compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/ConfigProvider.php | 4 ++++ src/GenericSerializerFactory.php | 26 ++++++++++++++++++++ src/Serializer.php | 3 +++ test/GenericSerializerFactoryTest.php | 34 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/GenericSerializerFactory.php create mode 100644 test/GenericSerializerFactoryTest.php diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 63e4648..878dd93 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -8,6 +8,9 @@ namespace Laminas\Serializer; +use Laminas\Serializer\Adapter\AdapterInterface; +use Laminas\Serializer\Adapter\PhpSerialize; + class ConfigProvider { /** @@ -34,6 +37,7 @@ public function getDependencyConfig() 'aliases' => [], 'factories' => [ 'SerializerAdapterManager' => AdapterPluginManagerFactory::class, + AdapterInterface::class => new GenericSerializerFactory(PhpSerialize::class), ], ]; } diff --git a/src/GenericSerializerFactory.php b/src/GenericSerializerFactory.php new file mode 100644 index 0000000..3311259 --- /dev/null +++ b/src/GenericSerializerFactory.php @@ -0,0 +1,26 @@ + $serializerName + * @param array|null $options + */ + public function __construct(private string $serializerName, private array|null $options = null) + { + } + + public function __invoke(ContainerInterface $container): AdapterInterface + { + $plugins = $container->get(AdapterPluginManager::class); + + return $plugins->build($this->serializerName, $this->options); + } +} diff --git a/src/Serializer.php b/src/Serializer.php index dd374ab..2216e34 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -12,6 +12,9 @@ use Laminas\ServiceManager\ServiceManager; use Traversable; +/** + * @deprecated This class will be removed with v3.0.0. Please migrate your code according to the migration guide. + */ // phpcs:ignore abstract class Serializer { diff --git a/test/GenericSerializerFactoryTest.php b/test/GenericSerializerFactoryTest.php new file mode 100644 index 0000000..28019a4 --- /dev/null +++ b/test/GenericSerializerFactoryTest.php @@ -0,0 +1,34 @@ + true]); + $container = $this->createMock(ContainerInterface::class); + $plugins = new AdapterPluginManager($container); + + $container + ->expects(self::once()) + ->method('get') + ->with(AdapterPluginManager::class) + ->willReturn($plugins); + + $adapter = $factory($container); + self::assertInstanceOf(Json::class, $adapter); + self::assertTrue($adapter->getOptions()->getCycleCheck()); + // Verify that default of json options is false so that we do not accidentally test for default `true` value + self::assertFalse((new JsonOptions())->getCycleCheck()); + } +}