• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.66
/src/JsonLd/Action/ContextAction.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\JsonLd\Action;
15

16
use ApiPlatform\JsonLd\ContextBuilderInterface;
17
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
18
use ApiPlatform\Metadata\Get;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
21
use ApiPlatform\State\ProcessorInterface;
22
use ApiPlatform\State\ProviderInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
use Symfony\Component\Serializer\SerializerInterface;
27

28
/**
29
 * Generates JSON-LD contexts.
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class ContextAction
34
{
35
    public const RESERVED_SHORT_NAMES = [
36
        'ConstraintViolationList' => true,
37
        'Error' => true,
38
    ];
39

40
    public function __construct(
41
        private readonly ContextBuilderInterface $contextBuilder,
42
        private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory,
43
        private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
44
        private readonly ?ProviderInterface $provider = null,
45
        private readonly ?ProcessorInterface $processor = null,
46
        private readonly ?SerializerInterface $serializer = null,
47
    ) {
48
    }
16✔
49

50
    /**
51
     * Generates a context according to the type requested.
52
     *
53
     * @throws NotFoundHttpException
54
     *
55
     * @return array{'@context': array<string, mixed>}|Response
56
     */
57
    public function __invoke(string $shortName = 'Entrypoint', ?Request $request = null): array|Response
58
    {
59
        if (!$shortName) {
16✔
60
            $shortName = 'Entrypoint';
×
61
        }
62

63
        if (null !== $request && $this->provider && $this->processor && $this->serializer) {
16✔
64
            $operation = new Get(
8✔
65
                outputFormats: ['jsonld' => ['application/ld+json']],
8✔
66
                validate: false,
8✔
67
                provider: fn () => $this->getContext($shortName),
8✔
68
                serialize: false,
8✔
69
                read: true
8✔
70
            );
8✔
71
            $context = ['request' => $request];
8✔
72
            $jsonLdContext = $this->provider->provide($operation, [], $context);
8✔
73

74
            return $this->processor->process($this->serializer->serialize($jsonLdContext, 'json'), $operation, [], $context);
8✔
75
        }
76

77
        if (!$context = $this->getContext($shortName)) {
8✔
78
            throw new NotFoundHttpException();
2✔
79
        }
80

81
        return $context;
6✔
82
    }
83

84
    /**
85
     * @return array{'@context': array<string, mixed>}|null
86
     */
87
    private function getContext(string $shortName): ?array
88
    {
89
        if ('Entrypoint' === $shortName) {
14✔
90
            return ['@context' => $this->contextBuilder->getEntrypointContext()];
2✔
91
        }
92

93
        // TODO: remove this, exceptions are resources since 3.2
94
        if (isset(self::RESERVED_SHORT_NAMES[$shortName])) {
12✔
95
            return ['@context' => $this->contextBuilder->getBaseContext()];
2✔
96
        }
97

98
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
10✔
99
            $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
10✔
100

101
            try {
102
                $resourceMetadataCollection = $resourceMetadataCollection->getOperation();
10✔
103
            } catch (OperationNotFoundException) {
×
104
                continue;
×
105
            }
106

107
            if ($shortName === $resourceMetadataCollection->getShortName()) {
10✔
108
                return ['@context' => $this->contextBuilder->getResourceContext($resourceClass)];
8✔
109
            }
110
        }
111

112
        return null;
2✔
113
    }
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc