forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHydraLinkProcessor.php
More file actions
60 lines (49 loc) · 1.95 KB
/
Copy pathHydraLinkProcessor.php
File metadata and controls
60 lines (49 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Hydra\State;
use ApiPlatform\JsonLd\ContextBuilder;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\Util\CorsTrait;
use Symfony\Component\WebLink\GenericLinkProvider;
use Symfony\Component\WebLink\Link;
/**
* @template T1
* @template T2
*
* @implements ProcessorInterface<T1, T2>
*/
final class HydraLinkProcessor implements ProcessorInterface
{
use CorsTrait;
/**
* @param ProcessorInterface<T1, T2> $decorated
*/
public function __construct(private readonly ProcessorInterface $decorated, private readonly UrlGeneratorInterface $urlGenerator)
{
}
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
{
if (!($request = $context['request'] ?? null) || !$operation instanceof HttpOperation || $this->isPreflightRequest($request)) {
return $this->decorated->process($data, $operation, $uriVariables, $context);
}
$apiDocUrl = $this->urlGenerator->generate('api_doc', ['_format' => 'jsonld'], UrlGeneratorInterface::ABS_PATH);
$linkProvider = $request->attributes->get('_api_platform_links') ?? new GenericLinkProvider();
foreach ($operation->getLinks() ?? [] as $link) {
$linkProvider = $linkProvider->withLink($link);
}
$link = new Link(ContextBuilder::HYDRA_NS.'apiDocumentation', $apiDocUrl);
$request->attributes->set('_api_platform_links', $linkProvider->withLink($link));
return $this->decorated->process($data, $operation, $uriVariables, $context);
}
}