Skip to content

Commit

Permalink
willdurand/Hateoas#325 support for php attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ixarlie committed Jan 10, 2023
1 parent f9f09f0 commit 360ca18
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions DependencyInjection/BazingaHateoasExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Bazinga\Bundle\HateoasBundle\DependencyInjection;

use Hateoas\Configuration\Metadata\Driver\AttributeDriver\AttributeReader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -60,5 +61,13 @@ public function load(array $configs, ContainerBuilder $container): void
->getDefinition('hateoas.event_listener.xml')
->setPublic(true)
->replaceArgument(0, new Reference($config['serializer']['xml']));

if (PHP_VERSION_ID >= 80100 && class_exists(AttributeReader::class)) {
$container->register('hateoas.metadata.annotation_and_attributes_reader', AttributeReader::class)
->setArgument(0, new Reference('annotation_reader'));

$container->findDefinition('hateoas.configuration.metadata.annotation_driver')
->setArgument(0, new Reference('hateoas.metadata.annotation_and_attributes_reader'));
}
}
}
24 changes: 24 additions & 0 deletions Tests/DependencyInjection/BazingaHateoasExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle;
use Bazinga\Bundle\HateoasBundle\Tests\Fixtures\SimpleObject;
use Doctrine\Common\Annotations\AnnotationReader;
use Hateoas\Configuration\Metadata\Driver\AnnotationDriver;
use Hateoas\Configuration\Metadata\Driver\AttributeDriver\AttributeReader;
use JMS\SerializerBundle\JMSSerializerBundle;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -143,6 +145,27 @@ public function testLoadInvalidConfigurationExtension()
$container->compile();
}

public function testAnnotationDriverSupportsAttributes()
{
if (PHP_VERSION_ID < 80100 || !class_exists(AttributeReader::class)) {
$this->markTestSkipped('Attributes are available only on php 8.1 or higher and AttributeReader exists');
}

$container = $this->getContainerForConfig([[]]);

$definition = $container->getDefinition('hateoas.metadata.annotation_and_attributes_reader');
$arguments = $definition->getArguments();
$this->assertCount(1, $arguments);
$this->assertEquals('annotation_reader', $arguments[0]);

$definition = $container->getDefinition('hateoas.configuration.metadata.annotation_driver');
$arguments = $definition->getArguments();
$this->assertCount(4, $arguments);
$this->assertEquals('hateoas.metadata.annotation_and_attributes_reader', $arguments[0]);

$container->compile();
}

private function clearTempDir()
{
// clear temporary directory
Expand Down Expand Up @@ -193,6 +216,7 @@ private function getContainerForConfig(array $configs, ?KernelInterface $kernel
$container->setParameter('kernel.debug', true);
$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));
Expand Down

0 comments on commit 360ca18

Please sign in to comment.