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

api-platform / core / 8658338405

12 Apr 2024 07:07AM UTC coverage: 58.893% (-0.004%) from 58.897%
8658338405

push

github

soyuka
chore: doctrine deprecations (#6237)

0 of 1 new or added line in 1 file covered. (0.0%)

6 existing lines in 3 files now uncovered.

10900 of 18508 relevant lines covered (58.89%)

19.84 hits per line

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

54.83
/src/Metadata/Extractor/XmlResourceExtractor.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\Extractor;
15

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use ApiPlatform\Metadata\GetCollection;
18
use ApiPlatform\Metadata\Post;
19
use ApiPlatform\Elasticsearch\State\Options;
20
use ApiPlatform\OpenApi\Model\ExternalDocumentation;
21
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
22
use ApiPlatform\OpenApi\Model\Parameter;
23
use ApiPlatform\OpenApi\Model\RequestBody;
24
use ApiPlatform\State\OptionsInterface;
25
use Symfony\Component\Config\Util\XmlUtils;
26

27
/**
28
 * Extracts an array of metadata from a list of XML files.
29
 *
30
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
31
 */
32
final class XmlResourceExtractor extends AbstractResourceExtractor
33
{
34
    use ResourceExtractorTrait;
35

36
    public const SCHEMA = __DIR__.'/schema/resources.xsd';
37

38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function extractPath(string $path): void
42
    {
43
        try {
44
            /** @var \SimpleXMLElement $xml */
45
            $xml = simplexml_import_dom(XmlUtils::loadFile($path, self::SCHEMA));
8✔
46
        } catch (\InvalidArgumentException $e) {
8✔
47
            // Ensure it's not a resource
48
            try {
49
                simplexml_import_dom(XmlUtils::loadFile($path, XmlPropertyExtractor::SCHEMA));
8✔
50
            } catch (\InvalidArgumentException) {
×
51
                throw new InvalidArgumentException(sprintf('Error while parsing %s: %s', $path, $e->getMessage()), $e->getCode(), $e);
×
52
            }
53

54
            // It's a property: ignore error
55
            return;
8✔
56
        }
57

58
        foreach ($xml->resource as $resource) {
8✔
59
            $base = $this->buildExtendedBase($resource);
8✔
60
            $this->resources[$this->resolve((string) $resource['class'])][] = array_merge($base, [
8✔
61
                'class' => $this->phpize($resource, 'class', 'string'),
8✔
62
                'operations' => $this->buildOperations($resource, $base),
8✔
63
                'graphQlOperations' => $this->buildGraphQlOperations($resource, $base),
8✔
64
            ]);
8✔
65
        }
66
    }
67

68
    private function buildExtendedBase(\SimpleXMLElement $resource): array
69
    {
70
        return array_merge($this->buildBase($resource), [
8✔
71
            'uriTemplate' => $this->phpize($resource, 'uriTemplate', 'string'),
8✔
72
            'routePrefix' => $this->phpize($resource, 'routePrefix', 'string'),
8✔
73
            'stateless' => $this->phpize($resource, 'stateless', 'bool'),
8✔
74
            'sunset' => $this->phpize($resource, 'sunset', 'string'),
8✔
75
            'acceptPatch' => $this->phpize($resource, 'acceptPatch', 'string'),
8✔
76
            'status' => $this->phpize($resource, 'status', 'integer'),
8✔
77
            'host' => $this->phpize($resource, 'host', 'string'),
8✔
78
            'condition' => $this->phpize($resource, 'condition', 'string'),
8✔
79
            'controller' => $this->phpize($resource, 'controller', 'string'),
8✔
80
            'types' => $this->buildArrayValue($resource, 'type'),
8✔
81
            'formats' => $this->buildFormats($resource, 'formats'),
8✔
82
            'inputFormats' => $this->buildFormats($resource, 'inputFormats'),
8✔
83
            'outputFormats' => $this->buildFormats($resource, 'outputFormats'),
8✔
84
            'uriVariables' => $this->buildUriVariables($resource),
8✔
85
            'defaults' => isset($resource->defaults->values) ? $this->buildValues($resource->defaults->values) : null,
8✔
86
            'requirements' => $this->buildRequirements($resource),
8✔
87
            'options' => isset($resource->options->values) ? $this->buildValues($resource->options->values) : null,
8✔
88
            'schemes' => $this->buildArrayValue($resource, 'scheme'),
8✔
89
            'cacheHeaders' => $this->buildCacheHeaders($resource),
8✔
90
            'hydraContext' => isset($resource->hydraContext->values) ? $this->buildValues($resource->hydraContext->values) : null,
8✔
91
            'openapiContext' => isset($resource->openapiContext->values) ? $this->buildValues($resource->openapiContext->values) : null, // TODO Remove in 4.0
8✔
92
            'openapi' => $this->buildOpenapi($resource),
8✔
93
            'paginationViaCursor' => $this->buildPaginationViaCursor($resource),
8✔
94
            'exceptionToStatus' => $this->buildExceptionToStatus($resource),
8✔
95
            'queryParameterValidationEnabled' => $this->phpize($resource, 'queryParameterValidationEnabled', 'bool'),
8✔
96
            'stateOptions' => $this->buildStateOptions($resource),
8✔
97
        ]);
8✔
98
    }
99

100
    private function buildBase(\SimpleXMLElement $resource): array
101
    {
102
        return [
8✔
103
            'shortName' => $this->phpize($resource, 'shortName', 'string'),
8✔
104
            'description' => $this->phpize($resource, 'description', 'string'),
8✔
105
            'urlGenerationStrategy' => $this->phpize($resource, 'urlGenerationStrategy', 'integer'),
8✔
106
            'deprecationReason' => $this->phpize($resource, 'deprecationReason', 'string'),
8✔
107
            'elasticsearch' => $this->phpize($resource, 'elasticsearch', 'bool'),
8✔
108
            'messenger' => $this->phpize($resource, 'messenger', 'bool|string'),
8✔
109
            'mercure' => $this->buildMercure($resource),
8✔
110
            'input' => $this->phpize($resource, 'input', 'bool|string'),
8✔
111
            'output' => $this->phpize($resource, 'output', 'bool|string'),
8✔
112
            'fetchPartial' => $this->phpize($resource, 'fetchPartial', 'bool'),
8✔
113
            'forceEager' => $this->phpize($resource, 'forceEager', 'bool'),
8✔
114
            'paginationClientEnabled' => $this->phpize($resource, 'paginationClientEnabled', 'bool'),
8✔
115
            'paginationClientItemsPerPage' => $this->phpize($resource, 'paginationClientItemsPerPage', 'bool'),
8✔
116
            'paginationClientPartial' => $this->phpize($resource, 'paginationClientPartial', 'bool'),
8✔
117
            'paginationEnabled' => $this->phpize($resource, 'paginationEnabled', 'bool'),
8✔
118
            'paginationFetchJoinCollection' => $this->phpize($resource, 'paginationFetchJoinCollection', 'bool'),
8✔
119
            'paginationUseOutputWalkers' => $this->phpize($resource, 'paginationUseOutputWalkers', 'bool'),
8✔
120
            'paginationItemsPerPage' => $this->phpize($resource, 'paginationItemsPerPage', 'integer'),
8✔
121
            'paginationMaximumItemsPerPage' => $this->phpize($resource, 'paginationMaximumItemsPerPage', 'integer'),
8✔
122
            'paginationPartial' => $this->phpize($resource, 'paginationPartial', 'bool'),
8✔
123
            'paginationType' => $this->phpize($resource, 'paginationType', 'string'),
8✔
124
            'processor' => $this->phpize($resource, 'processor', 'string'),
8✔
125
            'provider' => $this->phpize($resource, 'provider', 'string'),
8✔
126
            'security' => $this->phpize($resource, 'security', 'string'),
8✔
127
            'securityMessage' => $this->phpize($resource, 'securityMessage', 'string'),
8✔
128
            'securityPostDenormalize' => $this->phpize($resource, 'securityPostDenormalize', 'string'),
8✔
129
            'securityPostDenormalizeMessage' => $this->phpize($resource, 'securityPostDenormalizeMessage', 'string'),
8✔
130
            'securityPostValidation' => $this->phpize($resource, 'securityPostValidation', 'string'),
8✔
131
            'securityPostValidationMessage' => $this->phpize($resource, 'securityPostValidationMessage', 'string'),
8✔
132
            'normalizationContext' => isset($resource->normalizationContext->values) ? $this->buildValues($resource->normalizationContext->values) : null,
8✔
133
            'denormalizationContext' => isset($resource->denormalizationContext->values) ? $this->buildValues($resource->denormalizationContext->values) : null,
8✔
134
            'collectDenormalizationErrors' => $this->phpize($resource, 'collectDenormalizationErrors', 'bool'),
8✔
135
            'validationContext' => isset($resource->validationContext->values) ? $this->buildValues($resource->validationContext->values) : null,
8✔
136
            'filters' => $this->buildArrayValue($resource, 'filter'),
8✔
137
            'order' => isset($resource->order->values) ? $this->buildValues($resource->order->values) : null,
8✔
138
            'extraProperties' => $this->buildExtraProperties($resource, 'extraProperties'),
8✔
139
            'read' => $this->phpize($resource, 'read', 'bool'),
8✔
140
            'write' => $this->phpize($resource, 'write', 'bool'),
8✔
141
        ];
8✔
142
    }
143

144
    private function buildFormats(\SimpleXMLElement $resource, string $key): ?array
145
    {
146
        if (!isset($resource->{$key}->format)) {
8✔
147
            return null;
8✔
148
        }
149

150
        $data = [];
×
151
        foreach ($resource->{$key}->format as $format) {
×
152
            if (isset($format['name'])) {
×
153
                $data[(string) $format['name']] = (string) $format;
×
154
                continue;
×
155
            }
156

157
            $data[] = (string) $format;
×
158
        }
159

160
        return $data;
×
161
    }
162

163
    private function buildOpenapi(\SimpleXMLElement $resource): bool|OpenApiOperation|null
164
    {
165
        if (!isset($resource->openapi) && !isset($resource['openapi'])) {
8✔
166
            return null;
8✔
167
        }
168

169
        if (isset($resource['openapi']) && \in_array((string) $resource['openapi'], ['1', '0', 'true', 'false'], true)) {
×
170
            return $this->phpize($resource, 'openapi', 'bool');
×
171
        }
172

173
        $openapi = $resource->openapi;
×
174
        $data = [];
×
175
        $attributes = $openapi->attributes();
×
176
        foreach ($attributes as $attribute) {
×
177
            $data[$attribute->getName()] = $this->phpize($attributes, 'deprecated', 'deprecated' === $attribute->getName() ? 'bool' : 'string');
×
178
        }
179

180
        $data['tags'] = $this->buildArrayValue($resource, 'tag');
×
181

182
        if (isset($openapi->responses->response)) {
×
183
            foreach ($openapi->responses->response as $response) {
×
184
                $data['responses'][(string) $response->attributes()->status] = [
×
185
                    'description' => $this->phpize($response, 'description', 'string'),
×
186
                    'content' => isset($response->content->values) ? $this->buildValues($response->content->values) : null,
×
187
                    'headers' => isset($response->headers->values) ? $this->buildValues($response->headers->values) : null,
×
188
                    'links' => isset($response->links->values) ? $this->buildValues($response->links->values) : null,
×
189
                ];
×
190
            }
191
        }
192

193
        $data['externalDocs'] = isset($openapi->externalDocs) ? new ExternalDocumentation(
×
194
            description: $this->phpize($resource, 'description', 'string'),
×
195
            url: $this->phpize($resource, 'url', 'string'),
×
196
        ) : null;
×
197

198
        if (isset($openapi->parameters->parameter)) {
×
199
            foreach ($openapi->parameters->parameter as $parameter) {
×
200
                $data['parameters'][(string) $parameter->attributes()->name] = new Parameter(
×
201
                    name: $this->phpize($parameter, 'name', 'string'),
×
202
                    in: $this->phpize($parameter, 'in', 'string'),
×
203
                    description: $this->phpize($parameter, 'description', 'string'),
×
204
                    required: $this->phpize($parameter, 'required', 'bool'),
×
205
                    deprecated: $this->phpize($parameter, 'deprecated', 'bool'),
×
206
                    allowEmptyValue: $this->phpize($parameter, 'allowEmptyValue', 'bool'),
×
207
                    schema: isset($parameter->schema->values) ? $this->buildValues($parameter->schema->values) : null,
×
208
                    style: $this->phpize($parameter, 'style', 'string'),
×
209
                    explode: $this->phpize($parameter, 'explode', 'bool'),
×
210
                    allowReserved: $this->phpize($parameter, 'allowReserved', 'bool'),
×
211
                    example: $this->phpize($parameter, 'example', 'string'),
×
212
                    examples: isset($parameter->examples->values) ? new \ArrayObject($this->buildValues($parameter->examples->values)) : null,
×
213
                    content: isset($parameter->content->values) ? new \ArrayObject($this->buildValues($parameter->content->values)) : null,
×
214
                );
×
215
            }
216
        }
217
        $data['requestBody'] = isset($openapi->requestBody) ? new RequestBody(
×
218
            description: $this->phpize($openapi->requestBody, 'description', 'string'),
×
219
            content: isset($openapi->requestBody->content->values) ? new \ArrayObject($this->buildValues($openapi->requestBody->content->values)) : null,
×
220
            required: $this->phpize($openapi->requestBody, 'required', 'bool'),
×
221
        ) : null;
×
222

223
        $data['callbacks'] = isset($openapi->callbacks->values) ? new \ArrayObject($this->buildValues($openapi->callbacks->values)) : null;
×
224

225
        $data['security'] = isset($openapi->security->values) ? $this->buildValues($openapi->security->values) : null;
×
226

227
        if (isset($openapi->servers->server)) {
×
228
            foreach ($openapi->servers->server as $server) {
×
229
                $data['servers'][] = [
×
230
                    'description' => $this->phpize($server, 'description', 'string'),
×
231
                    'url' => $this->phpize($server, 'url', 'string'),
×
232
                    'variables' => isset($server->variables->values) ? $this->buildValues($server->variables->values) : null,
×
233
                ];
×
234
            }
235
        }
236

237
        $data['extensionProperties'] = isset($openapi->extensionProperties->values) ? $this->buildValues($openapi->extensionProperties->values) : null;
×
238

239
        foreach ($data as $key => $value) {
×
240
            if (null === $value) {
×
241
                unset($data[$key]);
×
242
            }
243
        }
244

245
        return new OpenApiOperation(...$data);
×
246
    }
247

248
    private function buildUriVariables(\SimpleXMLElement $resource): ?array
249
    {
250
        if (!isset($resource->uriVariables->uriVariable)) {
8✔
251
            return null;
8✔
252
        }
253

254
        $uriVariables = [];
8✔
255
        foreach ($resource->uriVariables->uriVariable as $data) {
8✔
256
            $parameterName = (string) $data['parameterName'];
8✔
257
            if (1 === (null === $data->attributes() ? 0 : \count($data->attributes()))) {
8✔
258
                $uriVariables[$parameterName] = $parameterName;
8✔
259
                continue;
8✔
260
            }
261

262
            if ($fromProperty = $this->phpize($data, 'fromProperty', 'string')) {
8✔
263
                $uriVariables[$parameterName]['from_property'] = $fromProperty;
×
264
            }
265
            if ($toProperty = $this->phpize($data, 'toProperty', 'string')) {
8✔
266
                $uriVariables[$parameterName]['to_property'] = $toProperty;
8✔
267
            }
268
            if ($fromClass = $this->phpize($data, 'fromClass', 'string')) {
8✔
269
                $uriVariables[$parameterName]['from_class'] = $fromClass;
8✔
270
            }
271
            if ($toClass = $this->phpize($data, 'toClass', 'string')) {
8✔
272
                $uriVariables[$parameterName]['to_class'] = $toClass;
×
273
            }
274
            if (isset($data->identifiers->values)) {
8✔
275
                $uriVariables[$parameterName]['identifiers'] = $this->buildValues($data->identifiers->values);
×
276
            }
277
            if (null !== ($compositeIdentifier = $this->phpize($data, 'compositeIdentifier', 'bool'))) {
8✔
278
                $uriVariables[$parameterName]['composite_identifier'] = $compositeIdentifier;
×
279
            }
280
        }
281

282
        return $uriVariables;
8✔
283
    }
284

285
    private function buildCacheHeaders(\SimpleXMLElement $resource): ?array
286
    {
287
        if (!isset($resource->cacheHeaders->cacheHeader)) {
8✔
288
            return null;
8✔
289
        }
290

291
        $data = [];
×
292
        foreach ($resource->cacheHeaders->cacheHeader as $cacheHeader) {
×
293
            if (isset($cacheHeader->values->value)) {
×
294
                $data[(string) $cacheHeader['name']] = $this->buildValues($cacheHeader->values);
×
295
                continue;
×
296
            }
297

298
            $data[(string) $cacheHeader['name']] = (string) $cacheHeader;
×
299
        }
300

301
        return $data;
×
302
    }
303

304
    private function buildRequirements(\SimpleXMLElement $resource): ?array
305
    {
306
        if (!isset($resource->requirements->requirement)) {
8✔
307
            return null;
8✔
308
        }
309

310
        $data = [];
×
311
        foreach ($resource->requirements->requirement as $requirement) {
×
312
            $data[(string) $requirement->attributes()->property] = (string) $requirement;
×
313
        }
314

315
        return $data;
×
316
    }
317

318
    private function buildMercure(\SimpleXMLElement $resource): array|bool|null
319
    {
320
        if (!isset($resource->mercure)) {
8✔
321
            return null;
8✔
322
        }
323

324
        if (null !== $resource->mercure->attributes()->private) {
×
325
            return ['private' => $this->phpize($resource->mercure->attributes(), 'private', 'bool')];
×
326
        }
327

328
        return true;
×
329
    }
330

331
    private function buildPaginationViaCursor(\SimpleXMLElement $resource): ?array
332
    {
333
        if (!isset($resource->paginationViaCursor->paginationField)) {
8✔
334
            return null;
8✔
335
        }
336

337
        $data = [];
×
338
        foreach ($resource->paginationViaCursor->paginationField as $paginationField) {
×
339
            $data[(string) $paginationField['field']] = (string) $paginationField['direction'];
×
340
        }
341

342
        return $data;
×
343
    }
344

345
    private function buildExceptionToStatus(\SimpleXMLElement $resource): ?array
346
    {
347
        if (!isset($resource->exceptionToStatus->exception)) {
8✔
348
            return null;
8✔
349
        }
350

351
        $data = [];
×
352
        foreach ($resource->exceptionToStatus->exception as $exception) {
×
353
            $data[(string) $exception['class']] = (int) $exception['statusCode'];
×
354
        }
355

356
        return $data;
×
357
    }
358

359
    private function buildExtraProperties(\SimpleXMLElement $resource, ?string $key = null): ?array
360
    {
361
        if (null !== $key) {
8✔
362
            if (!isset($resource->{$key})) {
8✔
363
                return null;
8✔
364
            }
365

366
            $resource = $resource->{$key};
×
367
        }
368

369
        return $this->buildValues($resource->values);
×
370
    }
371

372
    private function buildOperations(\SimpleXMLElement $resource, array $root): ?array
373
    {
374
        if (!isset($resource->operations->operation)) {
8✔
375
            return null;
8✔
376
        }
377

378
        $data = [];
8✔
379
        foreach ($resource->operations->operation as $operation) {
8✔
380
            $datum = $this->buildExtendedBase($operation);
8✔
381
            foreach ($datum as $key => $value) {
8✔
382
                if (null === $value) {
8✔
383
                    $datum[$key] = $root[$key];
8✔
384
                }
385
            }
386

387
            if (\in_array((string) $operation['class'], [GetCollection::class, Post::class], true)) {
8✔
388
                $datum['itemUriTemplate'] = $this->phpize($operation, 'itemUriTemplate', 'string');
8✔
389
            }
390

391
            $data[] = array_merge($datum, [
8✔
392
                'collection' => $this->phpize($operation, 'collection', 'bool'),
8✔
393
                'class' => (string) $operation['class'],
8✔
394
                'method' => $this->phpize($operation, 'method', 'string'),
8✔
395
                'read' => $this->phpize($operation, 'read', 'bool'),
8✔
396
                'deserialize' => $this->phpize($operation, 'deserialize', 'bool'),
8✔
397
                'validate' => $this->phpize($operation, 'validate', 'bool'),
8✔
398
                'write' => $this->phpize($operation, 'write', 'bool'),
8✔
399
                'serialize' => $this->phpize($operation, 'serialize', 'bool'),
8✔
400
                'queryParameterValidate' => $this->phpize($operation, 'queryParameterValidate', 'bool'),
8✔
401
                'priority' => $this->phpize($operation, 'priority', 'integer'),
8✔
402
                'name' => $this->phpize($operation, 'name', 'string'),
8✔
403
            ]);
8✔
404
        }
405

406
        return $data;
8✔
407
    }
408

409
    private function buildGraphQlOperations(\SimpleXMLElement $resource, array $root): ?array
410
    {
411
        if (!isset($resource->graphQlOperations->graphQlOperation)) {
8✔
412
            return null;
8✔
413
        }
414

415
        $data = [];
×
416
        foreach ($resource->graphQlOperations->graphQlOperation as $operation) {
×
417
            $datum = $this->buildBase($operation);
×
418
            foreach ($datum as $key => $value) {
×
419
                if (null === $value) {
×
420
                    $datum[$key] = $root[$key];
×
421
                }
422
            }
423

424
            $data[] = array_merge($datum, [
×
425
                'resolver' => $this->phpize($operation, 'resolver', 'string'),
×
426
                'args' => $this->buildArgs($operation),
×
427
                'class' => (string) $operation['class'],
×
428
                'read' => $this->phpize($operation, 'read', 'bool'),
×
429
                'deserialize' => $this->phpize($operation, 'deserialize', 'bool'),
×
430
                'validate' => $this->phpize($operation, 'validate', 'bool'),
×
431
                'write' => $this->phpize($operation, 'write', 'bool'),
×
432
                'serialize' => $this->phpize($operation, 'serialize', 'bool'),
×
433
                'priority' => $this->phpize($operation, 'priority', 'integer'),
×
434
                'name' => $this->phpize($operation, 'name', 'string'),
×
435
            ]);
×
436
        }
437

438
        return $data;
×
439
    }
440

441
    private function buildStateOptions(\SimpleXMLElement $resource): ?OptionsInterface
442
    {
443
        $stateOptions = $resource->stateOptions ?? null;
8✔
444
        if (!$stateOptions) {
8✔
445
            return null;
8✔
446
        }
447
        $elasticsearchOptions = $stateOptions->elasticsearchOptions ?? null;
×
448
        if ($elasticsearchOptions) {
×
449
                if (class_exists(Options::class)) {
×
450
                    return new Options(isset($elasticsearchOptions['index']) ? (string) $elasticsearchOptions['index'] : null, isset($elasticsearchOptions['type']) ? (string) $elasticsearchOptions['type'] : null);
×
451
                }
452
        }
453

UNCOV
454
        return null;
×
455
    }
456
}
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