Skip to content
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
41 changes: 40 additions & 1 deletion src/Annotation/ApiProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@
*
* @Annotation
* @Target({"METHOD", "PROPERTY"})
* @Attributes(
* @Attribute("fetchable", type="bool"),
* @Attribute("fetchEager", type="bool"),
* @Attribute("jsonldContext", type="array"),
* @Attribute("swaggerContext", type="array")
* )
*/
final class ApiProperty
{
use AttributesHydratorTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -64,7 +72,38 @@ final class ApiProperty
public $identifier;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $fetchable;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $fetchEager;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
private $jsonldContext;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
public $attributes = [];
private $swaggerContext;

/**
* @throws InvalidArgumentException
*/
public function __construct(array $values = [])
{
$this->hydrateAttributes($values);
}
}
45 changes: 20 additions & 25 deletions src/Annotation/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

namespace ApiPlatform\Core\Annotation;

use ApiPlatform\Core\Exception\InvalidArgumentException;
use Doctrine\Common\Util\Inflector;
use Doctrine\Common\Annotations\Annotation\Attribute;

/**
* ApiResource annotation.
Expand All @@ -34,6 +33,7 @@
* @Attribute("forceEager", type="bool"),
* @Attribute("filters", type="string[]"),
* @Attribute("graphql", type="array"),
* @Attribute("hydraContext", type="array"),
* @Attribute("iri", type="string"),
* @Attribute("itemOperations", type="array"),
* @Attribute("maximumItemsPerPage", type="int"),
Expand All @@ -49,11 +49,14 @@
* @Attribute("routePrefix", type="string"),
* @Attribute("shortName", type="string"),
* @Attribute("subresourceOperations", type="array"),
* @Attribute("swaggerContext", type="array"),
* @Attribute("validationGroups", type="mixed")
* )
*/
final class ApiResource
{
use AttributesHydratorTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -89,11 +92,6 @@ final class ApiResource
*/
public $graphql;

/**
* @var array
*/
public $attributes = [];

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
Expand Down Expand Up @@ -136,6 +134,13 @@ final class ApiResource
*/
private $filters;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string[]
*/
private $hydraContext;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
Expand Down Expand Up @@ -213,6 +218,13 @@ final class ApiResource
*/
private $routePrefix;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
private $swaggerContext;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
Expand All @@ -225,23 +237,6 @@ final class ApiResource
*/
public function __construct(array $values = [])
{
if (isset($values['attributes'])) {
$this->attributes = $values['attributes'];
unset($values['attributes']);
}

foreach ($values as $key => $value) {
if (!property_exists($this, $key)) {
throw new InvalidArgumentException(sprintf('Unknown property "%s" on annotation "%s".', $key, self::class));
}

$property = new \ReflectionProperty($this, $key);

if ($property->isPublic()) {
$this->$key = $value;
} else {
$this->attributes += [Inflector::tableize($key) => $value];
}
}
$this->hydrateAttributes($values);
}
}
60 changes: 60 additions & 0 deletions src/Annotation/AttributesHydratorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Annotation;

use ApiPlatform\Core\Exception\InvalidArgumentException;
use Doctrine\Common\Util\Inflector;

/**
* Hydrates attributes from annotation's parameters.
*
* @internal
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
trait AttributesHydratorTrait
{
/**
* @var array
*/
public $attributes = [];

/**
* @throws InvalidArgumentException
*/
private function hydrateAttributes(array $values)
{
if (isset($values['attributes'])) {
$this->attributes = $values['attributes'];
unset($values['attributes']);
}

foreach ($values as $key => $value) {
if (!property_exists($this, $key)) {
throw new InvalidArgumentException(sprintf('Unknown property "%s" on annotation "%s".', $key, self::class));
}

(new \ReflectionProperty($this, $key))->isPublic() ? $this->$key = $value : $this->attributes += [Inflector::tableize($key) => $value];
}
}
}
9 changes: 8 additions & 1 deletion src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,14 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
$inAttributes = null;
}

if (false === $fetchEager = $propertyMetadata->getAttribute('fetchEager')) {
if (
(null === $fetchEager = $propertyMetadata->getAttribute('fetch_eager')) &&
(null !== $fetchEager = $propertyMetadata->getAttribute('fetchEager'))
) {
@trigger_error('The "fetchEager" attribute is deprecated since 2.3. Please use "fetch_eager" instead.', E_USER_DEPRECATED);
}

if (false === $fetchEager) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class PropertyTest extends TestCase
class ApiPropertyTest extends TestCase
{
public function testAssignation()
{
Expand All @@ -44,4 +44,22 @@ public function testAssignation()
$this->assertTrue($property->identifier);
$this->assertEquals(['foo' => 'bar'], $property->attributes);
}

public function testConstruct()
{
$property = new ApiProperty([
'fetchable' => true,
'fetchEager' => false,
'jsonldContext' => ['foo' => 'bar'],
'swaggerContext' => ['foo' => 'baz'],
'attributes' => ['unknown' => 'unknown', 'fetchable' => false],
]);
$this->assertEquals([
'fetchable' => false,
'fetch_eager' => false,
'jsonld_context' => ['foo' => 'bar'],
'swagger_context' => ['foo' => 'baz'],
'unknown' => 'unknown',
], $property->attributes);
}
}
30 changes: 28 additions & 2 deletions tests/Bridge/Doctrine/Orm/Extension/EagerLoadingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,19 @@ public function testApplyToCollectionNoPartial()
}

public function testApplyToCollectionWithANonRedableButFetchEagerProperty()
{
$this->doTestApplyToCollectionWithANonRedableButFetchEagerProperty(false);
}

/**
* @group legacy
*/
public function testLegacyApplyToCollectionWithANonRedableButFetchEagerProperty()
{
$this->doTestApplyToCollectionWithANonRedableButFetchEagerProperty(true);
}

private function doTestApplyToCollectionWithANonRedableButFetchEagerProperty(bool $legacy)
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata());
Expand All @@ -817,7 +830,7 @@ public function testApplyToCollectionWithANonRedableButFetchEagerProperty()

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$relationPropertyMetadata = new PropertyMetadata();
$relationPropertyMetadata = $relationPropertyMetadata->withAttributes(['fetchEager' => true]);
$relationPropertyMetadata = $relationPropertyMetadata->withAttributes([$legacy ? 'fetchEager' : 'fetch_eager' => true]);
$relationPropertyMetadata = $relationPropertyMetadata->withReadableLink(false);
$relationPropertyMetadata = $relationPropertyMetadata->withReadable(false);

Expand Down Expand Up @@ -852,6 +865,19 @@ public function testApplyToCollectionWithANonRedableButFetchEagerProperty()
}

public function testApplyToCollectionWithARedableButNotFetchEagerProperty()
{
$this->doTestApplyToCollectionWithARedableButNotFetchEagerProperty(false);
}

/**
* @group legacy
*/
public function testLeacyApplyToCollectionWithARedableButNotFetchEagerProperty()
{
$this->doTestApplyToCollectionWithARedableButNotFetchEagerProperty(true);
}

private function doTestApplyToCollectionWithARedableButNotFetchEagerProperty(bool $legacy)
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata());
Expand All @@ -860,7 +886,7 @@ public function testApplyToCollectionWithARedableButNotFetchEagerProperty()

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$relationPropertyMetadata = new PropertyMetadata();
$relationPropertyMetadata = $relationPropertyMetadata->withAttributes(['fetchEager' => false]);
$relationPropertyMetadata = $relationPropertyMetadata->withAttributes([$legacy ? 'fetchEager' : 'fetch_eager' => false]);
$relationPropertyMetadata = $relationPropertyMetadata->withReadableLink(true);
$relationPropertyMetadata = $relationPropertyMetadata->withReadable(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function testFetchEagerWithNoForceEager()
$filterEagerLoadingExtension = new FilterEagerLoadingExtension($resourceMetadataFactoryProphecy->reveal(), false);
$filterEagerLoadingExtension->applyToCollection($qb, $queryNameGenerator->reveal(), CompositeRelation::class, 'get');

$expected = <<<SQL
$expected = <<<DQL
SELECT o
FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o
INNER JOIN o.compositeItem item
Expand All @@ -354,7 +354,7 @@ public function testFetchEagerWithNoForceEager()
LEFT JOIN o_2.foo foo_2 WITH o_2.bar = item_2.foo
WHERE item_2.field1 = :foo
)
SQL;
DQL;

$this->assertEquals($this->toDQLString($expected), $qb->getDQL());
}
Expand Down