|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Bridge\Doctrine\Orm\State; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator; |
| 17 | +use ApiPlatform\Exception\RuntimeException; |
| 18 | +use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation; |
| 19 | +use ApiPlatform\Metadata\Link; |
| 20 | +use Doctrine\ORM\QueryBuilder; |
| 21 | +use Doctrine\Persistence\Mapping\ClassMetadata; |
| 22 | + |
| 23 | +trait LinksHandlerTrait |
| 24 | +{ |
| 25 | + private function handleLinks(QueryBuilder $queryBuilder, array $identifiers, QueryNameGenerator $queryNameGenerator, array $context, string $resourceClass, ?string $operationName = null): void |
| 26 | + { |
| 27 | + $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($operationName); |
| 28 | + $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
| 29 | + $doctrineClassMetadata = $manager->getClassMetadata($resourceClass); |
| 30 | + $alias = $queryBuilder->getRootAliases()[0]; |
| 31 | + |
| 32 | + $links = $operation instanceof GraphQlOperation ? $operation->getLinks() : $operation->getUriVariables(); |
| 33 | + |
| 34 | + if ($linkClass = $context['linkClass'] ?? false) { |
| 35 | + foreach ($links as $link) { |
| 36 | + if ($linkClass === $link->getFromClass()) { |
| 37 | + foreach ($identifiers as $identifier => $value) { |
| 38 | + $this->applyLink($queryBuilder, $queryNameGenerator, $doctrineClassMetadata, $alias, $link, $identifier, $value); |
| 39 | + } |
| 40 | + |
| 41 | + return; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + $operation = $this->resourceMetadataCollectionFactory->create($linkClass)->getOperation($operationName); |
| 46 | + $links = $operation instanceof GraphQlOperation ? $operation->getLinks() : $operation->getUriVariables(); |
| 47 | + foreach ($links as $link) { |
| 48 | + if ($resourceClass === $link->getFromClass()) { |
| 49 | + $link = $link->withFromProperty($link->getToProperty())->withFromClass($linkClass); |
| 50 | + foreach ($identifiers as $identifier => $value) { |
| 51 | + $this->applyLink($queryBuilder, $queryNameGenerator, $doctrineClassMetadata, $alias, $link, $identifier, $value); |
| 52 | + } |
| 53 | + |
| 54 | + return; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + throw new RuntimeException(sprintf('The class "%s" cannot be retrieved from "%s".', $resourceClass, $linkClass)); |
| 59 | + } |
| 60 | + |
| 61 | + if (!$links) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + foreach ($identifiers as $identifier => $value) { |
| 66 | + $link = $links[$identifier] ?? $links['id']; |
| 67 | + |
| 68 | + $this->applyLink($queryBuilder, $queryNameGenerator, $doctrineClassMetadata, $alias, $link, $identifier, $value); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private function applyLink(QueryBuilder $queryBuilder, QueryNameGenerator $queryNameGenerator, ClassMetadata $doctrineClassMetadata, string $alias, Link $link, string $identifier, $value) |
| 73 | + { |
| 74 | + $placeholder = ':id_'.$identifier; |
| 75 | + if ($fromProperty = $link->getFromProperty()) { |
| 76 | + $propertyIdentifier = $link->getIdentifiers()[0]; |
| 77 | + $joinAlias = $queryNameGenerator->generateJoinAlias($fromProperty); |
| 78 | + |
| 79 | + $queryBuilder->join( |
| 80 | + $link->getFromClass(), |
| 81 | + $joinAlias, |
| 82 | + 'with', |
| 83 | + "$alias.$propertyIdentifier = $joinAlias.$fromProperty" |
| 84 | + ); |
| 85 | + |
| 86 | + $expression = $queryBuilder->expr()->eq( |
| 87 | + "{$joinAlias}.{$propertyIdentifier}", |
| 88 | + $placeholder |
| 89 | + ); |
| 90 | + } elseif ($property = $link->getToProperty()) { |
| 91 | + $propertyIdentifier = $link->getIdentifiers()[0]; |
| 92 | + $joinAlias = $queryNameGenerator->generateJoinAlias($property); |
| 93 | + |
| 94 | + $queryBuilder->join( |
| 95 | + "$alias.$property", |
| 96 | + $joinAlias, |
| 97 | + ); |
| 98 | + |
| 99 | + $expression = $queryBuilder->expr()->eq( |
| 100 | + "{$joinAlias}.{$propertyIdentifier}", |
| 101 | + $placeholder |
| 102 | + ); |
| 103 | + } else { |
| 104 | + $expression = $queryBuilder->expr()->eq( |
| 105 | + "{$alias}.{$identifier}", $placeholder |
| 106 | + ); |
| 107 | + } |
| 108 | + $queryBuilder->andWhere($expression); |
| 109 | + $queryBuilder->setParameter($placeholder, $value, $doctrineClassMetadata->getTypeOfField($identifier)); |
| 110 | + } |
| 111 | +} |
0 commit comments