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

api-platform / core / 21031030191

15 Jan 2026 12:18PM UTC coverage: 29.582% (+0.5%) from 29.097%
21031030191

Pull #7595

github

web-flow
Merge cc5f55cf5 into 73402fc61
Pull Request #7595: feat: mcp bundle tool integration

407 of 486 new or added lines in 16 files covered. (83.74%)

424 existing lines in 19 files now uncovered.

17417 of 58878 relevant lines covered (29.58%)

81.74 hits per line

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

97.06
/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.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\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\McpResource;
21
use ApiPlatform\Metadata\McpTool;
22
use ApiPlatform\Metadata\Metadata;
23
use ApiPlatform\Metadata\Operations;
24
use ApiPlatform\Metadata\Parameter;
25
use ApiPlatform\Metadata\Parameters;
26
use ApiPlatform\Metadata\Util\CamelCaseToSnakeCaseNameConverter;
27
use Psr\Log\LoggerInterface;
28
use Psr\Log\NullLogger;
29

30
/**
31
 * @internal
32
 *
33
 * This trait shares the common logic between attributes and Laravel concerns factories
34
 *
35
 * @author Antoine Bluchet <soyuka@gmail.com>
36
 * @author Kévin Dunglas <kevin@dunglas.dev>
37
 */
38
trait MetadataCollectionFactoryTrait
39
{
40
    use OperationDefaultsTrait;
41

42
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, ?LoggerInterface $logger = null, array $defaults = [], private readonly bool $graphQlEnabled = false)
43
    {
44
        $this->logger = $logger ?? new NullLogger();
2,498✔
45
        $this->defaults = $defaults;
2,498✔
46
        $this->camelCaseToSnakeCaseNameConverter = new CamelCaseToSnakeCaseNameConverter();
2,498✔
47
    }
48

49
    private function isResourceMetadata(string $name): bool
50
    {
51
        return is_a($name, ApiResource::class, true) || is_subclass_of($name, HttpOperation::class) || is_subclass_of($name, GraphQlOperation::class) || is_a($name, Parameter::class, true) || is_a($name, McpTool::class, true) || is_a($name, McpResource::class, true);
218✔
52
    }
53

54
    /**
55
     * Builds resource operations to support:
56
     * Resource
57
     * Get
58
     * Post
59
     * Resource
60
     * Get
61
     * In the future, we will be able to use nested attributes (https://wiki.php.net/rfc/new_in_initializers).
62
     *
63
     * @param array<Metadata|Parameter> $metadataCollection
64
     *
65
     * @return ApiResource[]
66
     */
67
    private function buildResourceOperations(array $metadataCollection, string $resourceClass, array $resources = []): array
68
    {
69
        $shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
219✔
70
        $index = -1;
219✔
71
        $operationPriority = 0;
219✔
72
        $hasApiResource = false;
219✔
73
        $globalParameters = new Parameters();
219✔
74

75
        foreach ($metadataCollection as $metadata) {
219✔
76
            if ($metadata instanceof Parameter) {
211✔
77
                if (!$k = $metadata->getKey()) {
6✔
78
                    throw new RuntimeException('Parameter "key" is mandatory when used on a class.');
×
79
                }
80
                $globalParameters->add($k, $metadata);
6✔
81
                continue;
6✔
82
            }
83

84
            if ($metadata instanceof ApiResource) {
211✔
85
                $hasApiResource = true;
167✔
86
                $resource = $this->getResourceWithDefaults($resourceClass, $shortName, $metadata);
167✔
87
                $operations = [];
167✔
88
                foreach ($resource->getOperations() ?? new Operations() as $operation) {
167✔
89
                    [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
102✔
90
                    $operations[$key] = $operation;
102✔
91
                }
92
                if ($operations) {
167✔
93
                    $resource = $resource->withOperations(new Operations($operations));
102✔
94
                }
95

96
                if ($mcp = $resource->getMcp()) {
167✔
97
                    $processedMcp = [];
4✔
98
                    foreach ($mcp as $key => $mcpOperation) {
4✔
99
                        if (null === $mcpOperation->getName()) {
4✔
100
                            $mcpOperation = $mcpOperation->withName($key);
4✔
101
                        }
102

103
                        [, $mcpOperation] = $this->getOperationWithDefaults($resource, $mcpOperation);
4✔
104
                        $processedMcp[$key] = $mcpOperation;
4✔
105
                    }
106
                    $resource = $resource->withMcp($processedMcp);
4✔
107
                }
108

109
                $resources[++$index] = $resource;
167✔
110
                continue;
167✔
111
            }
112

113
            if ($metadata instanceof GraphQlOperation) {
99✔
114
                [$key, $operation] = $this->getOperationWithDefaults($resources[$index], $metadata);
4✔
115
                $graphQlOperations = $resources[$index]->getGraphQlOperations();
4✔
116
                $graphQlOperations[$key] = $operation;
4✔
117
                $resources[$index] = $resources[$index]->withGraphQlOperations($graphQlOperations);
4✔
118
                continue;
4✔
119
            }
120

121
            if ($metadata instanceof McpTool || $metadata instanceof McpResource) {
99✔
122
                if (-1 === $index) {
4✔
123
                    $resources[++$index] = $this->getResourceWithDefaults($resourceClass, $shortName, new ApiResource());
4✔
124
                }
125
                [$key, $operation] = $this->getOperationWithDefaults($resources[$index], $metadata);
4✔
126
                $mcp = $resources[$index]->getMcp() ?? [];
4✔
127
                $mcp[$key] = $operation;
4✔
128
                $resources[$index] = $resources[$index]->withMcp($mcp);
4✔
129
                continue;
4✔
130
            }
131

132
            if (!is_subclass_of($metadata, HttpOperation::class) && !is_subclass_of($metadata, GraphQlOperation::class)) {
97✔
NEW
133
                continue;
×
134
            }
135

136
            if (-1 === $index || $this->hasSameOperation($resources[$index], $metadata::class, $metadata)) {
97✔
137
                $resources[++$index] = $this->getResourceWithDefaults($resourceClass, $shortName, new ApiResource());
69✔
138
            }
139

140
            [$key, $operation] = $this->getOperationWithDefaults($resources[$index], $metadata);
97✔
141
            if (null === $operation->getPriority()) {
97✔
142
                $operation = $operation->withPriority(++$operationPriority);
97✔
143
            }
144
            $operations = $resources[$index]->getOperations() ?? new Operations();
97✔
145
            $resources[$index] = $resources[$index]->withOperations($operations->add($key, $operation));
97✔
146
        }
147

148
        // Loop again and set default operations if none where found
149
        foreach ($resources as $index => $resource) {
219✔
150
            if (\count($globalParameters) > 0) {
211✔
151
                $resources[$index] = $resource = $this->mergeOperationParameters($resource, $globalParameters);
6✔
152
            }
153

154
            if (null === $resource->getOperations()) {
211✔
155
                $operations = [];
76✔
156
                foreach ($this->getDefaultHttpOperations($resource) as $operation) {
76✔
157
                    [$key, $operation] = $this->getOperationWithDefaults($resource, $operation, true);
76✔
158
                    $operations[$key] = $operation;
76✔
159
                }
160
                $resources[$index] = $resource = $resource->withOperations(new Operations($operations));
76✔
161
            }
162

163
            if ($parameters = $resource->getParameters()) {
211✔
164
                $operations = [];
8✔
165
                foreach ($resource->getOperations() ?? [] as $i => $operation) {
8✔
166
                    $operations[$i] = $this->mergeOperationParameters($operation, $parameters);
8✔
167
                }
168
                $resources[$index] = $resource = $resource->withOperations(new Operations($operations)); // @phpstan-ignore-line
8✔
169
            }
170

171
            if (!$this->graphQlEnabled) {
211✔
172
                continue;
6✔
173
            }
174

175
            $graphQlOperations = $resource->getGraphQlOperations();
205✔
176
            if (null === $graphQlOperations) {
205✔
177
                if (!$hasApiResource) {
190✔
178
                    $resources[$index] = $resources[$index]->withGraphQlOperations([]);
69✔
179
                    continue;
69✔
180
                }
181

182
                // Add default GraphQL operations on the first resource
183
                if (0 === $index) {
146✔
184
                    $resources[$index] = $this->addDefaultGraphQlOperations($resources[$index]);
139✔
185
                }
186
                continue;
146✔
187
            }
188

189
            $resources[$index] = $this->completeGraphQlOperations($resources[$index]);
35✔
190
            $graphQlOperations = $resources[$index]->getGraphQlOperations();
35✔
191

192
            $graphQlOperationsWithDefaults = [];
35✔
193
            foreach ($graphQlOperations as $operation) {
35✔
194
                [$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
35✔
195
                if ($parameters) {
35✔
196
                    $operation = $this->mergeOperationParameters($operation, $parameters);
4✔
197
                }
198

199
                $graphQlOperationsWithDefaults[$key] = $operation;
35✔
200
            }
201

202
            $resources[$index] = $resources[$index]->withGraphQlOperations($graphQlOperationsWithDefaults);
35✔
203
        }
204

205
        return $resources;
219✔
206
    }
207

208
    /**
209
     * Does the resource already have an operation of the $operationClass type?
210
     * Useful to determine if we need to create a new ApiResource when the class has only operation attributes, for example:.
211
     *
212
     * #[Get]
213
     * #[Get(uriTemplate: '/alternate')]
214
     * class Example {}
215
     */
216
    private function hasSameOperation(ApiResource $resource, string $operationClass, HttpOperation $operation): bool
217
    {
218
        foreach ($resource->getOperations() ?? [] as $o) {
51✔
219
            if ($o instanceof $operationClass && $operation->getUriTemplate() === $o->getUriTemplate() && $operation->getName() === $o->getName() && $operation->getRouteName() === $o->getRouteName()) {
31✔
220
                return true;
×
221
            }
222
        }
223

224
        return false;
51✔
225
    }
226

227
    /**
228
     * @template T of Metadata
229
     *
230
     * @param T $resource
231
     *
232
     * @return T
233
     */
234
    private function mergeOperationParameters(Metadata $resource, Parameters $globalParameters): Metadata
235
    {
236
        $parameters = $resource->getParameters() ?? new Parameters();
8✔
237
        foreach ($globalParameters as $parameterName => $parameter) {
8✔
238
            if ($key = $parameter->getKey()) {
8✔
239
                $parameterName = $key;
6✔
240
            }
241

242
            if (!$parameters->has($parameterName, $parameter::class)) {
8✔
243
                $parameters->add($parameterName, $parameter);
6✔
244
            }
245
        }
246

247
        return $resource->withParameters($parameters);
8✔
248
    }
249
}
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