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

api-platform / core / 10943429050

19 Sep 2024 02:48PM UTC coverage: 7.647% (-0.03%) from 7.675%
10943429050

push

github

web-flow
feat: api-platform/json-hal component (#6621)

* feat: add hal support for laravel

* feat: quick review

* fix: typo & cs-fixer

* fix: typo in composer.json

* fix: cs-fixer & phpstan

* fix: forgot about hal item normalizer, therefore there's no more createbook nor updatebook test as Hal is a readonly format

0 of 94 new or added lines in 2 files covered. (0.0%)

9082 existing lines in 291 files now uncovered.

12629 of 165144 relevant lines covered (7.65%)

22.89 hits per line

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

72.41
/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
    ) {
UNCOV
48
    }
18✔
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
    {
UNCOV
59
        if (!$shortName) {
18✔
60
            $shortName = 'Entrypoint';
×
61
        }
62

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

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

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

81
        return $context;
×
82
    }
83

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

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

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

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

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

112
        return null;
×
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