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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

54.07
/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\Metadata\Tests\Fixtures\StateOptions;
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
use Symfony\Component\WebLink\Link;
27

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

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

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

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

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

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

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

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

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

159
            $data[] = (string) $format;
×
160
        }
161

162
        return $data;
×
163
    }
164

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

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

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

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

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

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

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

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

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

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

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

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

247
        return new OpenApiOperation(...$data);
×
248
    }
249

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

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

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

284
        return $uriVariables;
12✔
285
    }
286

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

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

300
            $data[(string) $cacheHeader['name']] = (string) $cacheHeader;
×
301
        }
302

303
        return $data;
×
304
    }
305

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

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

317
        return $data;
×
318
    }
319

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

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

330
        return true;
×
331
    }
332

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

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

344
        return $data;
×
345
    }
346

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

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

358
        return $data;
×
359
    }
360

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

368
            $resource = $resource->{$key};
×
369
        }
370

371
        return $this->buildValues($resource->values);
×
372
    }
373

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

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

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

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

408
        return $data;
12✔
409
    }
410

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

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

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

441
        return $data;
×
442
    }
443

444
    private function buildStateOptions(\SimpleXMLElement $resource): ?OptionsInterface
445
    {
446
        $stateOptions = $resource->stateOptions ?? null;
12✔
447
        if (!$stateOptions) {
12✔
448
            return null;
12✔
449
        }
450
        $elasticsearchOptions = $stateOptions->elasticsearchOptions ?? null;
×
451
        if ($elasticsearchOptions) {
×
452
            return new StateOptions(
×
453
                isset($elasticsearchOptions['index']) ? (string) $elasticsearchOptions['index'] : null,
×
454
                isset($elasticsearchOptions['type']) ? (string) $elasticsearchOptions['type'] : null,
×
455
            );
×
456
        }
457

458
        return null;
×
459
    }
460

461
    /**
462
     * @return Link[]
463
     */
464
    private function buildLinks(\SimpleXMLElement $resource): ?array
465
    {
466
        $links = $resource->links ?? null;
12✔
467
        if (!$resource->links) {
12✔
468
            return null;
12✔
469
        }
470

471
        $links = [];
×
472
        foreach ($resource->links as $link) {
×
473
            $links[] = new Link(rel: (string) $link->link->attributes()->rel, href: (string) $link->link->attributes()->href);
×
474
        }
475

476
        return $links;
×
477
    }
478
}
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