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

api-platform / core / 9710726294

25 Jun 2024 02:01PM UTC coverage: 62.122% (-0.5%) from 62.637%
9710726294

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.

10862 of 17485 relevant lines covered (62.12%)

59.64 hits per line

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

92.86
/src/Documentation/Action/DocumentationAction.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\Documentation\Action;
15

16
use ApiPlatform\Documentation\Documentation;
17
use ApiPlatform\Documentation\DocumentationInterface;
18
use ApiPlatform\Metadata\Get;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
21
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
22
use ApiPlatform\OpenApi\OpenApi;
23
use ApiPlatform\OpenApi\Serializer\ApiGatewayNormalizer;
24
use ApiPlatform\OpenApi\Serializer\LegacyOpenApiNormalizer;
25
use ApiPlatform\OpenApi\Serializer\OpenApiNormalizer;
26
use ApiPlatform\State\ProcessorInterface;
27
use ApiPlatform\State\ProviderInterface;
28
use Negotiation\Negotiator;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\Response;
31

32
/**
33
 * Generates the API documentation.
34
 *
35
 * @deprecated use ApiPlatform\Symfony\DocumentationAction instead
36
 *
37
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
38
 */
39
final class DocumentationAction
40
{
41
    use ContentNegotiationTrait;
42

43
    public function __construct(
44
        private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory,
45
        private readonly string $title = '',
46
        private readonly string $description = '',
47
        private readonly string $version = '',
48
        private readonly ?OpenApiFactoryInterface $openApiFactory = null,
49
        private readonly ?ProviderInterface $provider = null,
50
        private readonly ?ProcessorInterface $processor = null,
51
        ?Negotiator $negotiator = null,
52
        private readonly array $documentationFormats = [OpenApiNormalizer::JSON_FORMAT => ['application/vnd.openapi+json'], OpenApiNormalizer::FORMAT => ['application/json']]
53
    ) {
54
        $this->negotiator = $negotiator ?? new Negotiator();
20✔
55
    }
56

57
    /**
58
     * @return DocumentationInterface|OpenApi|Response
59
     */
60
    public function __invoke(?Request $request = null)
61
    {
62
        if (null === $request) {
20✔
63
            return new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version);
×
64
        }
65

66
        $context = [
20✔
67
            'api_gateway' => $request->query->getBoolean(ApiGatewayNormalizer::API_GATEWAY),
20✔
68
            'base_url' => $request->getBaseUrl(),
20✔
69
            'spec_version' => (string) $request->query->get(LegacyOpenApiNormalizer::SPEC_VERSION),
20✔
70
        ];
20✔
71
        $request->attributes->set('_api_normalization_context', $request->attributes->get('_api_normalization_context', []) + $context);
20✔
72
        $format = $this->getRequestFormat($request, $this->documentationFormats);
20✔
73

74
        if (null !== $this->openApiFactory && ('html' === $format || OpenApiNormalizer::FORMAT === $format || OpenApiNormalizer::JSON_FORMAT === $format || OpenApiNormalizer::YAML_FORMAT === $format)) {
20✔
75
            return $this->getOpenApiDocumentation($context, $format, $request);
12✔
76
        }
77

78
        return $this->getHydraDocumentation($context, $request);
8✔
79
    }
80

81
    /**
82
     * @param array<string,mixed> $context
83
     */
84
    private function getOpenApiDocumentation(array $context, string $format, Request $request): OpenApi|Response
85
    {
86
        if ($this->provider && $this->processor) {
12✔
87
            $context['request'] = $request;
8✔
88
            $operation = new Get(
8✔
89
                class: OpenApi::class,
8✔
90
                read: true,
8✔
91
                serialize: true,
8✔
92
                provider: fn () => $this->openApiFactory->__invoke($context),
8✔
93
                normalizationContext: [
8✔
94
                    ApiGatewayNormalizer::API_GATEWAY => $context['api_gateway'] ?? null,
8✔
95
                    LegacyOpenApiNormalizer::SPEC_VERSION => $context['spec_version'] ?? null,
8✔
96
                ],
8✔
97
                outputFormats: $this->documentationFormats
8✔
98
            );
8✔
99

100
            if ('html' === $format) {
8✔
101
                // TODO: support laravel this bounds Documentation with Symfony so it's not perfect
UNCOV
102
                $operation = $operation->withProcessor('api_platform.swagger_ui.processor')->withWrite(true);
×
103
            }
104
            if ('json' === $format) {
8✔
105
                trigger_deprecation('api-platform/core', '3.2', 'The "json" format is too broad, use "jsonopenapi" instead.');
×
106
            }
107

108
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
8✔
109
        }
110

111
        return $this->openApiFactory->__invoke($context);
4✔
112
    }
113

114
    /**
115
     * TODO: the logic behind the Hydra Documentation is done in a ApiPlatform\Hydra\Serializer\DocumentationNormalizer.
116
     * We should transform this to a provider, it'd improve performances also by a bit.
117
     *
118
     * @param array<string,mixed> $context
119
     */
120
    private function getHydraDocumentation(array $context, Request $request): DocumentationInterface|Response
121
    {
122
        if ($this->provider && $this->processor) {
8✔
123
            $context['request'] = $request;
4✔
124
            $operation = new Get(
4✔
125
                class: Documentation::class,
4✔
126
                read: true,
4✔
127
                serialize: true,
4✔
128
                provider: fn () => new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version)
4✔
129
            );
4✔
130

131
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
4✔
132
        }
133

134
        return new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version);
4✔
135
    }
136
}
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