Skip to content

Subresource maxDepth #1520

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

Merged
merged 1 commit into from
Nov 28, 2017
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
4 changes: 4 additions & 0 deletions src/Annotation/ApiSubresource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
*/
final class ApiSubresource
{
/**
* @var int
*/
public $maxDepth;
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ private function updateMetadata(ApiSubresource $annotation, PropertyMetadata $pr
$type = $propertyMetadata->getType();
$isCollection = $type->isCollection();
$resourceClass = $isCollection ? $type->getCollectionValueType()->getClassName() : $type->getClassName();
$maxDepth = $annotation->maxDepth;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check that maxDepth is int here?

Copy link
Member

@dunglas dunglas Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doctrine Annotation will handle that because of the PHPDoc IIRC.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't know that. Nice.


return $propertyMetadata->withSubresource(new SubresourceMetadata($resourceClass, $isCollection));
return $propertyMetadata->withSubresource(new SubresourceMetadata($resourceClass, $isCollection, $maxDepth));
}
}
12 changes: 11 additions & 1 deletion src/Metadata/Property/SubresourceMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ final class SubresourceMetadata
{
private $resourceClass;
private $collection;
private $maxDepth;

public function __construct(string $resourceClass, bool $collection = false)
public function __construct(string $resourceClass, bool $collection = false, int $maxDepth = null)
{
$this->resourceClass = $resourceClass;
$this->collection = $collection;
$this->maxDepth = $maxDepth;
}

public function getResourceClass(): string
Expand Down Expand Up @@ -54,4 +56,12 @@ public function withCollection(bool $collection): self

return $metadata;
}

/**
* @return int|null
*/
public function getMaxDepth()
{
return $this->maxDepth;
}
}
1 change: 1 addition & 0 deletions src/Metadata/schema/metadata.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<xsd:complexType name="subresource" mixed="true">
<xsd:attribute type="xsd:boolean" name="collection"/>
<xsd:attribute type="xsd:string" name="resourceClass"/>
<xsd:attribute type="xsd:integer" name="maxDepth"/>
</xsd:complexType>

<xsd:complexType name="property">
Expand Down
15 changes: 13 additions & 2 deletions src/Operation/Factory/SubresourceOperationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ public function create(string $resourceClass): array
* @param string $rootResourceClass null on the first iteration, it then keeps track of the origin resource class
* @param array $parentOperation the previous call operation
* @param array $visited
* @param int $depth the number of visited
* @param int $maxDepth
*/
private function computeSubresourceOperations(string $resourceClass, array &$tree, string $rootResourceClass = null, array $parentOperation = null, array $visited = [])
private function computeSubresourceOperations(string $resourceClass, array &$tree, string $rootResourceClass = null, array $parentOperation = null, array $visited = [], int $depth = 0, int $maxDepth = null)
{
if (null === $rootResourceClass) {
$rootResourceClass = $resourceClass;
Expand All @@ -79,6 +81,15 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre
$subresourceMetadata = $this->resourceMetadataFactory->create($subresourceClass);

$visiting = "$resourceClass $property $subresourceClass";

// Handle maxDepth
if ($rootResourceClass === $resourceClass) {
$maxDepth = $subresource->getMaxDepth();
}

if (null !== $maxDepth && $depth >= $maxDepth) {
break;
}
if (isset($visited[$visiting])) {
continue;
}
Expand Down Expand Up @@ -154,7 +165,7 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre

$tree[$operation['route_name']] = $operation;

$this->computeSubresourceOperations($subresourceClass, $tree, $rootResourceClass, $operation, $visited + [$visiting => true]);
$this->computeSubresourceOperations($subresourceClass, $tree, $rootResourceClass, $operation, $visited + [$visiting => true], ++$depth, $maxDepth);
}
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/FileConfigurations/resources.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
writableLink="false"
required="true"
>
<subresource collection="true" resourceClass="Foo" />
<subresource collection="true" resourceClass="Foo" maxDepth="1" />
<attribute name="foo">
<attribute>Foo</attribute>
</attribute>
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/FileConfigurations/resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ resources:
iri: 'someirischema'
properties:
'foo':
subresource: {collection: true, resourceClass: 'Foo'}
subresource: {collection: true, resourceClass: 'Foo', maxDepth: 1}
description: 'The dummy foo'
readable: true
writable: true
Expand Down
45 changes: 45 additions & 0 deletions tests/Operation/Factory/SubresourceOperationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,49 @@ public function testCreateByOverriding()
] + SubresourceOperationFactory::ROUTE_OPTIONS,
], $subresourceOperationFactory->create(DummyEntity::class));
}

public function testCreateWithMaxDepth()
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(RelatedDummyEntity::class)->shouldBeCalled()->willReturn(new ResourceMetadata('relatedDummyEntity'));
$resourceMetadataFactoryProphecy->create(DummyEntity::class)->shouldBeCalled()->willReturn(new ResourceMetadata('dummyEntity'));

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(DummyEntity::class)->shouldBeCalled()->willReturn(new PropertyNameCollection(['subresource']));
$propertyNameCollectionFactoryProphecy->create(RelatedDummyEntity::class)->shouldBeCalled()->willReturn(new PropertyNameCollection(['bar', 'anotherSubresource']));

$subresourceMetadataCollectionWithMaxDepth = (new PropertyMetadata())->withSubresource(new SubresourceMetadata(RelatedDummyEntity::class, false, 1));
$anotherSubresourceMetadata = (new PropertyMetadata())->withSubresource(new SubresourceMetadata(DummyEntity::class, false));

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(DummyEntity::class, 'subresource')->shouldBeCalled()->willReturn($subresourceMetadataCollectionWithMaxDepth);
$propertyMetadataFactoryProphecy->create(RelatedDummyEntity::class, 'bar')->shouldBeCalled()->willReturn(new PropertyMetadata());
$propertyMetadataFactoryProphecy->create(RelatedDummyEntity::class, 'anotherSubresource')->shouldBeCalled()->willReturn($anotherSubresourceMetadata);

$pathSegmentNameGeneratorProphecy = $this->prophesize(PathSegmentNameGeneratorInterface::class);
$pathSegmentNameGeneratorProphecy->getSegmentName('dummyEntity', true)->shouldBeCalled()->willReturn('dummy_entities');
$pathSegmentNameGeneratorProphecy->getSegmentName('subresource', false)->shouldBeCalled()->willReturn('subresource');

$subresourceOperationFactory = new SubresourceOperationFactory(
$resourceMetadataFactoryProphecy->reveal(),
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$pathSegmentNameGeneratorProphecy->reveal()
);

$this->assertEquals([
'api_dummy_entities_subresource_get_subresource' => [
'property' => 'subresource',
'collection' => false,
'resource_class' => RelatedDummyEntity::class,
'shortNames' => ['relatedDummyEntity', 'dummyEntity'],
'identifiers' => [
['id', DummyEntity::class, true],
],
'route_name' => 'api_dummy_entities_subresource_get_subresource',
'path' => '/dummy_entities/{id}/subresource.{_format}',
'operation_name' => 'subresource_get_subresource',
] + SubresourceOperationFactory::ROUTE_OPTIONS,
], $subresourceOperationFactory->create(DummyEntity::class));
}
}