|
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
|
|
|
namespace ApiPlatform\Core\Hypermedia; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Api\UrlGeneratorInterface; |
|
15
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
|
16
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
|
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
|
18
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
|
19
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
* |
|
24
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class ContextBuilder implements ContextBuilderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private $resourceNameCollectionFactory; |
|
29
|
|
|
private $resourceMetadataFactory; |
|
30
|
|
|
private $propertyNameCollectionFactory; |
|
31
|
|
|
private $propertyMetadataFactory; |
|
32
|
|
|
private $urlGenerator; |
|
33
|
|
|
private $docUri; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var NameConverterInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $nameConverter; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, UrlGeneratorInterface $urlGenerator, string $docUri = '', NameConverterInterface $nameConverter = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
|
43
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
|
44
|
|
|
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
|
45
|
|
|
$this->propertyMetadataFactory = $propertyMetadataFactory; |
|
46
|
|
|
$this->urlGenerator = $urlGenerator; |
|
47
|
|
|
$this->nameConverter = $nameConverter; |
|
48
|
|
|
$this->docUri = $docUri; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL) : array |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
'@vocab' => $this->urlGenerator->generate('api_hydra_doc', [], $referenceType).'#', |
|
58
|
|
|
'hydra' => self::HYDRA_NS, |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getHalContext(string $selfLink) : array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
'_links' => |
|
69
|
|
|
['self' => |
|
70
|
|
|
['href' => $selfLink] |
|
71
|
|
|
, |
|
72
|
|
|
'curies' => [ |
|
73
|
|
|
['name' => 'ap', |
|
74
|
|
|
'href' => $this->urlGenerator->generate('api_hal_entrypoint').$this->docUri.'#section-{rel}', |
|
75
|
|
|
'templated' => true |
|
76
|
|
|
], |
|
77
|
|
|
] |
|
78
|
|
|
] |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getEntrypointContext(int $referenceType = UrlGeneratorInterface::ABS_PATH) : array |
|
85
|
|
|
{ |
|
86
|
|
|
$context = $this->getBaseContext($referenceType); |
|
87
|
|
|
|
|
88
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
|
89
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
|
90
|
|
|
|
|
91
|
|
|
$resourceName = lcfirst($resourceMetadata->getShortName()); |
|
92
|
|
|
|
|
93
|
|
|
$context[$resourceName] = [ |
|
94
|
|
|
'@id' => 'Entrypoint/'.$resourceName, |
|
95
|
|
|
'@type' => '@id', |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $context; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : array |
|
106
|
|
|
{ |
|
107
|
|
|
$context = $this->getBaseContext($referenceType, $referenceType); |
|
|
|
|
|
|
108
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
|
109
|
|
|
$prefixedShortName = sprintf('#%s', $resourceMetadata->getShortName()); |
|
110
|
|
|
|
|
111
|
|
|
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
|
112
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
|
113
|
|
|
|
|
114
|
|
|
if ($propertyMetadata->isIdentifier() && !$propertyMetadata->isWritable()) { |
|
|
|
|
|
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName; |
|
119
|
|
|
|
|
120
|
|
|
if (!$id = $propertyMetadata->getIri()) { |
|
121
|
|
|
$id = sprintf('%s/%s', $prefixedShortName, $convertedName); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (!$propertyMetadata->isReadableLink()) { |
|
|
|
|
|
|
125
|
|
|
$context[$convertedName] = [ |
|
126
|
|
|
'@id' => $id, |
|
127
|
|
|
'@type' => '@id', |
|
128
|
|
|
]; |
|
129
|
|
|
} else { |
|
130
|
|
|
$context[$convertedName] = $id; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $context; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getResourceContextUri(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : string |
|
138
|
|
|
{ |
|
139
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
|
140
|
|
|
|
|
141
|
|
|
return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()]); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.