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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

willdurand/Hateoas#325 support for php attributes #104

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BazingaHateoasBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down
27 changes: 27 additions & 0 deletions DependencyInjection/Compiler/AttributeDriverPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* This file is part of the HateoasBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Bazinga\Bundle\HateoasBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AttributeDriverPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (PHP_VERSION_ID < 80100) {
$container->removeDefinition('hateoas.configuration.metadata.attribute_driver');
}
}
}
21 changes: 19 additions & 2 deletions Resources/config/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,30 @@
<argument type="service" id="jms_serializer.type_parser" on-invalid="null" />
</service>

<!-- The `Hateoas\Configuration\Metadata\Driver\AnnotationDriver` class and its corresponding `hateoas.configuration.metadata.annotation_driver` service are deprecated in favor of the `hateoas.configuration.metadata.attribute_driver` service -->
<service id="hateoas.configuration.metadata.annotation_driver" class="Hateoas\Configuration\Metadata\Driver\AnnotationDriver" public="false">
<argument type="service" id="annotation_reader" />
<argument type="service" id="hateoas.configuration.metadata.annotation_reader" />
<argument type="service" id="jms_serializer.expression_evaluator" />
<argument type="service" id="hateoas.configuration.provider" />
<argument type="service" id="jms_serializer.type_parser" on-invalid="null" />
</service>

<service id="hateoas.configuration.metadata.attribute_driver" class="Hateoas\Configuration\Metadata\Driver\AttributeDriver" public="false">
<argument type="service" id="jms_serializer.expression_evaluator" />
<argument type="service" id="hateoas.configuration.provider" />
<argument type="service" id="jms_serializer.type_parser" on-invalid="null" />
</service>

<!-- The `hateoas.configuration.metadata.annotation_or_attribute_driver` is necessary to provide the extension feature to the `hateoas.configuration.metadata.attribute_driver` service -->
<service id="hateoas.configuration.metadata.annotation_or_attribute_driver" class="Metadata\Driver\DriverChain" public="false">
<argument type="collection">
<argument type="service" id="hateoas.configuration.metadata.attribute_driver" on-invalid="ignore" />
<argument type="service" id="hateoas.configuration.metadata.annotation_driver" />
</argument>
</service>

<service id="hateoas.configuration.metadata.extension_driver" class="Hateoas\Configuration\Metadata\Driver\ExtensionDriver" public="false">
<argument type="service" id="hateoas.configuration.metadata.annotation_driver" />
<argument type="service" id="hateoas.configuration.metadata.annotation_or_attribute_driver" />
</service>

<service id="hateoas.configuration.metadata.chain_driver" class="Metadata\Driver\DriverChain" public="false">
Expand All @@ -69,6 +84,8 @@
<argument /><!-- Directory -->
</service>

<service id="hateoas.configuration.metadata.annotation_reader" class="Doctrine\Common\Annotations\AnnotationReader" public="false" />

<service id="hateoas.configuration.metadata.cache" alias="hateoas.configuration.metadata.cache.file_cache" public="false" />

<service id="hateoas.configuration.metadata_factory" class="Metadata\MetadataFactory" public="false">
Expand Down
20 changes: 18 additions & 2 deletions Tests/DependencyInjection/BazingaHateoasExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -194,7 +206,8 @@ private function getContainerForConfig(array $configs, ?KernelInterface $kernel
$container->setParameter('kernel.cache_dir', $this->getTempDir());
$container->setParameter('kernel.bundles', []);
$container->setParameter('kernel.bundles_metadata', []);
ixarlie marked this conversation as resolved.
Show resolved Hide resolved
$container->set('annotation_reader', new AnnotationReader());
// 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);
Expand All @@ -216,6 +229,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;
}

Expand Down
4 changes: 4 additions & 0 deletions Tests/Fixtures/SimpleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading