From b86f87649ba2167581ef3a7bd7f344e97294628b Mon Sep 17 00:00:00 2001 From: ixarlie Date: Tue, 10 Jan 2023 23:04:34 +0100 Subject: [PATCH 1/5] willdurand/Hateoas#325 support for php attributes update bundle based on hateoas changes --- BazingaHateoasBundle.php | 2 ++ .../Compiler/AttributeDriverPass.php | 27 +++++++++++++++++++ Resources/config/configuration.xml | 21 +++++++++++++-- .../BazingaHateoasExtensionTest.php | 18 +++++++++++-- 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 DependencyInjection/Compiler/AttributeDriverPass.php diff --git a/BazingaHateoasBundle.php b/BazingaHateoasBundle.php index 44652fc..83fd657 100644 --- a/BazingaHateoasBundle.php +++ b/BazingaHateoasBundle.php @@ -10,6 +10,7 @@ namespace Bazinga\Bundle\HateoasBundle; +use Bazinga\Bundle\HateoasBundle\DependencyInjection\Compiler\AttributeDriverPass; use Bazinga\Bundle\HateoasBundle\DependencyInjection\Compiler\CacheWarmupPass; use Bazinga\Bundle\HateoasBundle\DependencyInjection\Compiler\ExtensionDriverPass; use Bazinga\Bundle\HateoasBundle\DependencyInjection\Compiler\RelationProviderPass; @@ -28,6 +29,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new UrlGeneratorPass()); $container->addCompilerPass(new RelationProviderPass()); + $container->addCompilerPass(new AttributeDriverPass()); $container->addCompilerPass(new ExtensionDriverPass()); $container->addCompilerPass(new CacheWarmupPass()); } diff --git a/DependencyInjection/Compiler/AttributeDriverPass.php b/DependencyInjection/Compiler/AttributeDriverPass.php new file mode 100644 index 0000000..6378c80 --- /dev/null +++ b/DependencyInjection/Compiler/AttributeDriverPass.php @@ -0,0 +1,27 @@ +removeDefinition('hateoas.configuration.metadata.attribute_driver'); + } + } +} diff --git a/Resources/config/configuration.xml b/Resources/config/configuration.xml index d0268d0..1512554 100644 --- a/Resources/config/configuration.xml +++ b/Resources/config/configuration.xml @@ -38,15 +38,30 @@ + - + + + + + + + + + + + + + + + - + @@ -69,6 +84,8 @@ + + diff --git a/Tests/DependencyInjection/BazingaHateoasExtensionTest.php b/Tests/DependencyInjection/BazingaHateoasExtensionTest.php index 232b2e6..4d5cda0 100644 --- a/Tests/DependencyInjection/BazingaHateoasExtensionTest.php +++ b/Tests/DependencyInjection/BazingaHateoasExtensionTest.php @@ -6,7 +6,6 @@ use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle; use Bazinga\Bundle\HateoasBundle\Tests\Fixtures\SimpleObject; -use Doctrine\Common\Annotations\AnnotationReader; use JMS\SerializerBundle\JMSSerializerBundle; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -143,6 +142,19 @@ public function testLoadInvalidConfigurationExtension() $container->compile(); } + public function testSupportedAttributeDriver() + { + $container = $this->getContainerForConfig([[]]); + $container->compile(); + + // Hateoas attributes are supported as of php 8.1. + if (PHP_VERSION_ID < 80100) { + self::assertFalse($container->hasDefinition('hateoas.configuration.metadata.attribute_driver')); + } else { + self::assertTrue($container->hasDefinition('hateoas.configuration.metadata.attribute_driver')); + } + } + private function clearTempDir() { // clear temporary directory @@ -194,7 +206,6 @@ private function getContainerForConfig(array $configs, ?KernelInterface $kernel $container->setParameter('kernel.cache_dir', $this->getTempDir()); $container->setParameter('kernel.bundles', []); $container->setParameter('kernel.bundles_metadata', []); - $container->set('annotation_reader', new AnnotationReader()); $container->setDefinition('doctrine', new Definition(Registry::class)); $container->setDefinition('doctrine_phpcr', new Definition(Registry::class)); $container->set('router', $router); @@ -216,6 +227,9 @@ private function getContainerForConfig(array $configs, ?KernelInterface $kernel $container->getDefinition('hateoas.configuration.provider.chain') ->setPublic(true); + $container->getDefinition('hateoas.configuration.metadata.attribute_driver') + ->setPublic(true); + return $container; } From 2bb28570ad66dd28bb2735bf9d6455c4377a6141 Mon Sep 17 00:00:00 2001 From: ixarlie Date: Wed, 16 Oct 2024 00:32:25 +0200 Subject: [PATCH 2/5] add annotation_reader test container --- Tests/DependencyInjection/BazingaHateoasExtensionTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/DependencyInjection/BazingaHateoasExtensionTest.php b/Tests/DependencyInjection/BazingaHateoasExtensionTest.php index 4d5cda0..41a42ed 100644 --- a/Tests/DependencyInjection/BazingaHateoasExtensionTest.php +++ b/Tests/DependencyInjection/BazingaHateoasExtensionTest.php @@ -6,6 +6,7 @@ use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle; use Bazinga\Bundle\HateoasBundle\Tests\Fixtures\SimpleObject; +use Doctrine\Common\Annotations\AnnotationReader; use JMS\SerializerBundle\JMSSerializerBundle; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -210,6 +211,7 @@ private function getContainerForConfig(array $configs, ?KernelInterface $kernel $container->setDefinition('doctrine_phpcr', new Definition(Registry::class)); $container->set('router', $router); $container->set('debug.stopwatch', $this->createMock(Stopwatch::class)); + $container->set('annotation_reader', $this->createMock(AnnotationReader::class)); $container->setParameter('foo', 'bar'); From e7e655f0af78c01d9f3043cb3af785319d45be4b Mon Sep 17 00:00:00 2001 From: ixarlie Date: Wed, 16 Oct 2024 01:42:57 +0200 Subject: [PATCH 3/5] fix tests --- Tests/DependencyInjection/BazingaHateoasExtensionTest.php | 4 ++-- Tests/Fixtures/SimpleObject.php | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Tests/DependencyInjection/BazingaHateoasExtensionTest.php b/Tests/DependencyInjection/BazingaHateoasExtensionTest.php index 41a42ed..ddf7cd9 100644 --- a/Tests/DependencyInjection/BazingaHateoasExtensionTest.php +++ b/Tests/DependencyInjection/BazingaHateoasExtensionTest.php @@ -6,7 +6,6 @@ use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle; use Bazinga\Bundle\HateoasBundle\Tests\Fixtures\SimpleObject; -use Doctrine\Common\Annotations\AnnotationReader; use JMS\SerializerBundle\JMSSerializerBundle; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -211,7 +210,8 @@ private function getContainerForConfig(array $configs, ?KernelInterface $kernel $container->setDefinition('doctrine_phpcr', new Definition(Registry::class)); $container->set('router', $router); $container->set('debug.stopwatch', $this->createMock(Stopwatch::class)); - $container->set('annotation_reader', $this->createMock(AnnotationReader::class)); + // The annotation_reader is used by JMSSerializerBundle versions lower than 5.4.0 + $container->setAlias('annotation_reader', 'hateoas.configuration.metadata.annotation_reader'); $container->setParameter('foo', 'bar'); diff --git a/Tests/Fixtures/SimpleObject.php b/Tests/Fixtures/SimpleObject.php index c7ecf0c..ff685c4 100644 --- a/Tests/Fixtures/SimpleObject.php +++ b/Tests/Fixtures/SimpleObject.php @@ -34,6 +34,10 @@ * ) * ) */ +#[Hateoas\Relation(name: 'all', href: 'http://somewhere/simple-objects', attributes: ['foo' => 'expr(parameter("foo"))'])] +#[Hateoas\Relation(name: 'all_2', href: 'expr(link(object, "all"))')] +#[Hateoas\Relation(name: 'e1', embedded: new Hateoas\Embedded(content: 'expr(1)', type: 'string'))] +#[Hateoas\Relation(name: 'e2', embedded: new Hateoas\Embedded(content: 'expr(2)', type: 'float', exclusion: new Hateoas\Exclusion(excludeIf: 'expr(false)')))] class SimpleObject { /** From 9f2079c5ca85a4a7ccb295cc362c4b370a9d2b72 Mon Sep 17 00:00:00 2001 From: ixarlie Date: Wed, 16 Oct 2024 01:46:44 +0200 Subject: [PATCH 4/5] tests move annotation reader line to original location --- Tests/DependencyInjection/BazingaHateoasExtensionTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/DependencyInjection/BazingaHateoasExtensionTest.php b/Tests/DependencyInjection/BazingaHateoasExtensionTest.php index ddf7cd9..25e7212 100644 --- a/Tests/DependencyInjection/BazingaHateoasExtensionTest.php +++ b/Tests/DependencyInjection/BazingaHateoasExtensionTest.php @@ -206,12 +206,12 @@ private function getContainerForConfig(array $configs, ?KernelInterface $kernel $container->setParameter('kernel.cache_dir', $this->getTempDir()); $container->setParameter('kernel.bundles', []); $container->setParameter('kernel.bundles_metadata', []); + // The annotation_reader is used by JMSSerializerBundle versions lower than 5.4.0 + $container->setAlias('annotation_reader', 'hateoas.configuration.metadata.annotation_reader'); $container->setDefinition('doctrine', new Definition(Registry::class)); $container->setDefinition('doctrine_phpcr', new Definition(Registry::class)); $container->set('router', $router); $container->set('debug.stopwatch', $this->createMock(Stopwatch::class)); - // The annotation_reader is used by JMSSerializerBundle versions lower than 5.4.0 - $container->setAlias('annotation_reader', 'hateoas.configuration.metadata.annotation_reader'); $container->setParameter('foo', 'bar'); From 85201d2bd8024199de2508acdcbe51069f6df164 Mon Sep 17 00:00:00 2001 From: ixarlie Date: Wed, 16 Oct 2024 09:19:01 +0200 Subject: [PATCH 5/5] bump willdurand/hateoas version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8acaeba..6e30c76 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "jms/serializer-bundle": "^3.1 || ^4.2 || ^5.0", "symfony/expression-language": "~3.0 || ~4.0 || ~5.0 || ~6.0 || ~7.0", "symfony/framework-bundle": "~3.0 || ~4.0 || ~5.0 || ~6.0 || ~7.0", - "willdurand/hateoas": "^3.5" + "willdurand/hateoas": "^3.11@beta" }, "require-dev": { "doctrine/annotations": "^1.13.2 || ^2.0",