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

api-platform / core / 9710673650

25 Jun 2024 02:01PM UTC coverage: 61.693% (-0.9%) from 62.637%
9710673650

push

github

web-flow
feat(laravel): laravel component (#5882)

* feat(laravel): laravel component

* try to skip laravel

* feat(jsonapi): component

* feat(laravel): json api support (needs review)

* work on relations

* relations (needs toMany) + skolem + IRI to resource

* links handler

* ulid

* validation

* slug post

* remove deprecations

* move classes

* fix tests

* fix tests metadata

* phpstan

* missing class

* fix laravel tests

* fix stan

33 of 77 new or added lines in 20 files covered. (42.86%)

140 existing lines in 15 files now uncovered.

10787 of 17485 relevant lines covered (61.69%)

59.64 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
    }
28✔
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) {
28✔
NEW
60
            $shortName = 'Entrypoint';
×
61
        }
62

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

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

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

81
        return $context;
12✔
82
    }
83

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

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

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

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

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

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

© 2025 Coveralls, Inc