|
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\Hal; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
|
15
|
|
|
use ApiPlatform\Core\Api\UrlGeneratorInterface; |
|
16
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
|
17
|
|
|
use ApiPlatform\Core\Hypermedia\ContextBuilderInterface; |
|
18
|
|
|
use ApiPlatform\Core\JsonLd\EntrypointBuilderInterface; |
|
19
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
|
20
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
* |
|
25
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class EntrypointBuilder implements EntrypointBuilderInterface |
|
28
|
|
|
{ |
|
29
|
|
|
private $resourceNameCollectionFactory; |
|
30
|
|
|
private $resourceMetadataFactory; |
|
31
|
|
|
private $iriConverter; |
|
32
|
|
|
private $urlGenerator; |
|
33
|
|
|
private $contextBuilder; |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator, ContextBuilderInterface $contextBuilder) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
|
38
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
|
39
|
|
|
$this->iriConverter = $iriConverter; |
|
40
|
|
|
$this->urlGenerator = $urlGenerator; |
|
41
|
|
|
$this->contextBuilder = $contextBuilder; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getEntrypoint(string $referenceType = UrlGeneratorInterface::ABS_PATH) : array |
|
48
|
|
|
{ |
|
49
|
|
|
$entrypoint = $this->contextBuilder->getBaseContext($referenceType); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
|
52
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
|
53
|
|
|
|
|
54
|
|
|
if (empty($resourceMetadata->getCollectionOperations())) { |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
try { |
|
58
|
|
|
$entrypoint['_links']['ap:'.$resourceMetadata->getShortName()] = ['href' => $this->iriConverter->getIriFromResourceClass($resourceClass)]; |
|
59
|
|
|
} catch (InvalidArgumentException $ex) { |
|
60
|
|
|
// Ignore resources without GET operations |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $entrypoint; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
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.