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 12, 2024
1 parent ccbdde8 commit f085fc6
Show file tree
Hide file tree
Showing 2 changed files with 32 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'));
}
}
}
23 changes: 23 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

0 comments on commit f085fc6

Please sign in to comment.