Skip to content

Commit 372bd07

Browse files
committed
fix composite
1 parent b2a9782 commit 372bd07

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/Core/Bridge/Symfony/Bundle/Command/UpgradeApiResourceCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Command;
1515

1616
use ApiPlatform\Core\Annotation\ApiResource;
17+
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
1718
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1819
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
1920
use ApiPlatform\Core\Upgrade\ColorConsoleDiffFormatter;
@@ -42,15 +43,17 @@ final class UpgradeApiResourceCommand extends Command
4243
private $subresourceOperationFactory;
4344
private $subresourceTransformer;
4445
private $reader;
46+
private $identifiersExtractor;
4547
private $localCache = [];
4648

47-
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, SubresourceOperationFactoryInterface $subresourceOperationFactory, SubresourceTransformer $subresourceTransformer, AnnotationReader $reader)
49+
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, SubresourceOperationFactoryInterface $subresourceOperationFactory, SubresourceTransformer $subresourceTransformer, AnnotationReader $reader, IdentifiersExtractorInterface $identifiersExtractor)
4850
{
4951
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
5052
$this->resourceMetadataFactory = $resourceMetadataFactory;
5153
$this->subresourceOperationFactory = $subresourceOperationFactory;
5254
$this->subresourceTransformer = $subresourceTransformer;
5355
$this->reader = $reader;
56+
$this->identifiersExtractor = $identifiersExtractor;
5457

5558
parent::__construct();
5659
}
@@ -204,7 +207,7 @@ private function transformApiResource(InputInterface $input, OutputInterface $ou
204207
continue;
205208
}
206209

207-
$traverser->addVisitor(new UpgradeApiResourceVisitor($attribute, $isAnnotation));
210+
$traverser->addVisitor(new UpgradeApiResourceVisitor($attribute, $isAnnotation, $this->identifiersExtractor, $resourceClass));
208211

209212
$oldCode = file_get_contents($fileName);
210213
$oldStmts = $parser->parse($oldCode);

src/Core/Upgrade/UpgradeApiResourceVisitor.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
use ApiPlatform\Api\UrlGeneratorInterface;
1717
use ApiPlatform\Core\Annotation\ApiResource as LegacyApiResource;
1818
use ApiPlatform\Core\Annotation\ApiSubresource;
19+
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
1920
use ApiPlatform\Metadata\ApiResource;
2021
use ApiPlatform\Metadata\Delete;
2122
use ApiPlatform\Metadata\Get;
2223
use ApiPlatform\Metadata\GetCollection;
2324
use ApiPlatform\Metadata\GraphQl\Mutation;
2425
use ApiPlatform\Metadata\GraphQl\Query;
2526
use ApiPlatform\Metadata\GraphQl\QueryCollection;
27+
use ApiPlatform\Metadata\Link;
2628
use ApiPlatform\Metadata\Patch;
2729
use ApiPlatform\Metadata\Post;
2830
use ApiPlatform\Metadata\Put;
@@ -36,12 +38,16 @@ final class UpgradeApiResourceVisitor extends NodeVisitorAbstract
3638
use RemoveAnnotationTrait;
3739

3840
private LegacyApiResource $resourceAnnotation;
41+
private IdentifiersExtractorInterface $identifiersExtractor;
3942
private bool $isAnnotation = false;
43+
private string $resourceClass;
4044

41-
public function __construct(LegacyApiResource $resourceAnnotation, bool $isAnnotation = false)
45+
public function __construct(LegacyApiResource $resourceAnnotation, bool $isAnnotation, IdentifiersExtractorInterface $identifiersExtractor, string $resourceClass)
4246
{
4347
$this->resourceAnnotation = $resourceAnnotation;
4448
$this->isAnnotation = $isAnnotation;
49+
$this->identifiersExtractor = $identifiersExtractor;
50+
$this->resourceClass = $resourceClass;
4551
}
4652

4753
/**
@@ -80,6 +86,10 @@ public function enterNode(Node $node)
8086
$this->getGraphQlOperationsNamespaces($this->resourceAnnotation->graphql ?? [])
8187
));
8288

89+
if (true === !($this->resourceAnnotation->attributes['composite_identifier'] ?? true)) {
90+
$namespaces[] = Link::class;
91+
}
92+
8393
foreach ($node->stmts as $k => $stmt) {
8494
if (!$stmt instanceof Node\Stmt\Use_) {
8595
break;
@@ -202,6 +212,40 @@ public function enterNode(Node $node)
202212
continue;
203213
}
204214

215+
if ('compositeIdentifier' === $key) {
216+
if (false !== $value) {
217+
continue;
218+
}
219+
220+
$identifiers = $this->identifiersExtractor->getIdentifiersFromResourceClass($this->resourceClass);
221+
$identifierNodeItems = [];
222+
foreach ($identifiers as $identifier) {
223+
$identifierNodes = [
224+
'compositeIdentifier' => new Node\Expr\ConstFetch(new Node\Name('false')),
225+
'fromClass' => new Node\Expr\ClassConstFetch(
226+
new Node\Name(
227+
'self'
228+
),
229+
'class'
230+
),
231+
'identifiers' => new Node\Expr\Array_(
232+
[
233+
new Node\Expr\ArrayItem(new Node\Scalar\String_($identifier)),
234+
],
235+
['kind' => Node\Expr\Array_::KIND_SHORT]
236+
),
237+
];
238+
239+
$identifierNodeItems[] = new Node\Expr\ArrayItem(
240+
new Node\Expr\New_(new Node\Name('Link'), $this->arrayToArguments($identifierNodes)),
241+
new Node\Scalar\String_($identifier)
242+
);
243+
}
244+
245+
$arguments['uriVariables'] = new Node\Expr\Array_($identifierNodeItems, ['kind' => Node\Expr\Array_::KIND_SHORT]);
246+
continue;
247+
}
248+
205249
$arguments[$key] = $this->valueToNode($value);
206250
}
207251

src/Symfony/Bundle/Resources/config/legacy/upgrade.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<argument type="service" id="api_platform.subresource_operation_factory" />
1414
<argument type="service" id="api_platform.upgrade.subresource_transformer" />
1515
<argument type="service" id="annotations.reader" />
16+
<argument type="service" id="api_platform.identifiers_extractor.legacy" />
1617
</service>
1718
</services>
1819

0 commit comments

Comments
 (0)