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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL) : array |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
'@vocab' => $this->urlGenerator->generate('api_hydra_doc', [], UrlGeneratorInterface::ABS_URL).'#', |
58
|
|
|
'hydra' => self::HYDRA_NS, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getHalContext(string $selfLink) : array |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
'_links' => ['self' => ['href' => $selfLink], |
69
|
|
|
'curies' => [ |
70
|
|
|
['name' => 'ap', |
71
|
|
|
'href' => $this->urlGenerator->generate('api_hal_entrypoint').$this->docUri.'#section-{rel}', |
72
|
|
|
'templated' => true, |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
], |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function getEntrypointContext(int $referenceType = UrlGeneratorInterface::ABS_PATH) : array |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$context = $this->getBaseContext($referenceType); |
85
|
|
|
|
86
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
87
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
88
|
|
|
|
89
|
|
|
$resourceName = lcfirst($resourceMetadata->getShortName()); |
90
|
|
|
|
91
|
|
|
$context[$resourceName] = [ |
92
|
|
|
'@id' => 'Entrypoint/'.$resourceName, |
93
|
|
|
'@type' => '@id', |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $context; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : array |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$context = $this->getBaseContext($referenceType, $referenceType); |
|
|
|
|
106
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
107
|
|
|
$prefixedShortName = sprintf('#%s', $resourceMetadata->getShortName()); |
108
|
|
|
|
109
|
|
|
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
110
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
111
|
|
|
|
112
|
|
|
if ($propertyMetadata->isIdentifier() && !$propertyMetadata->isWritable()) { |
|
|
|
|
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName; |
117
|
|
|
|
118
|
|
|
if (!$id = $propertyMetadata->getIri()) { |
119
|
|
|
$id = sprintf('%s/%s', $prefixedShortName, $convertedName); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!$propertyMetadata->isReadableLink()) { |
|
|
|
|
123
|
|
|
$context[$convertedName] = [ |
124
|
|
|
'@id' => $id, |
125
|
|
|
'@type' => '@id', |
126
|
|
|
]; |
127
|
|
|
} else { |
128
|
|
|
$context[$convertedName] = $id; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $context; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getResourceContextUri(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : string |
136
|
|
|
{ |
137
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
138
|
|
|
|
139
|
|
|
return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.