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\Core\Bridge\Doctrine\Orm\Metadata\Property; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
18
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
19
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Use Doctrine metadata to populate the identifier property. |
23
|
|
|
* |
24
|
|
|
* @author Kévin Dunglas <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class DoctrineOrmPropertyMetadataFactory implements PropertyMetadataFactoryInterface |
27
|
|
|
{ |
28
|
|
|
private $decorated; |
29
|
|
|
private $managerRegistry; |
30
|
|
|
|
31
|
|
|
public function __construct(ManagerRegistry $managerRegistry, PropertyMetadataFactoryInterface $decorated) |
32
|
|
|
{ |
33
|
|
|
$this->managerRegistry = $managerRegistry; |
34
|
|
|
$this->decorated = $decorated; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata |
41
|
|
|
{ |
42
|
|
|
$propertyMetadata = $this->decorated->create($resourceClass, $property, $options); |
43
|
|
|
|
44
|
|
|
if (null !== $propertyMetadata->isIdentifier()) { |
45
|
|
|
return $propertyMetadata; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$manager = $this->managerRegistry->getManagerForClass($resourceClass); |
49
|
|
|
if (!$manager) { |
50
|
|
|
return $propertyMetadata; |
51
|
|
|
} |
52
|
|
|
$doctrineClassMetadata = $manager->getClassMetadata($resourceClass); |
53
|
|
|
|
54
|
|
|
$identifiers = $doctrineClassMetadata->getIdentifier(); |
55
|
|
|
foreach ($identifiers as $identifier) { |
56
|
|
|
if ($identifier === $property) { |
57
|
|
|
$propertyMetadata = $propertyMetadata->withIdentifier(true); |
58
|
|
|
|
59
|
|
|
if (null !== $propertyMetadata->isWritable()) { |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($doctrineClassMetadata instanceof ClassMetadataInfo) { |
64
|
|
|
$writable = $doctrineClassMetadata->isIdentifierNatural(); |
65
|
|
|
} else { |
66
|
|
|
$writable = false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$propertyMetadata = $propertyMetadata->withWritable($writable); |
70
|
|
|
|
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (null === $propertyMetadata->isIdentifier()) { |
|
|
|
|
76
|
|
|
$propertyMetadata = $propertyMetadata->withIdentifier(false); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $propertyMetadata; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|