Skip to content

Commit c624300

Browse files
committed
xml/yaml
1 parent 402d0c4 commit c624300

13 files changed

+94
-11
lines changed

src/Metadata/Extractor/XmlResourceExtractor.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private function buildExtendedBase(\SimpleXMLElement $resource): array
9696
'queryParameterValidationEnabled' => $this->phpize($resource, 'queryParameterValidationEnabled', 'bool'),
9797
'stateOptions' => $this->buildStateOptions($resource),
9898
'links' => $this->buildLinks($resource),
99+
'headers' => $this->buildHeaders($resource),
99100
]);
100101
}
101102

@@ -465,7 +466,6 @@ private function buildStateOptions(\SimpleXMLElement $resource): ?OptionsInterfa
465466
*/
466467
private function buildLinks(\SimpleXMLElement $resource): ?array
467468
{
468-
$links = $resource->links ?? null;
469469
if (!$resource->links) {
470470
return null;
471471
}
@@ -477,4 +477,21 @@ private function buildLinks(\SimpleXMLElement $resource): ?array
477477

478478
return $links;
479479
}
480+
481+
/**
482+
* @return array<string, string>
483+
*/
484+
private function buildHeaders(\SimpleXMLElement $resource): ?array
485+
{
486+
if (!$resource->headers) {
487+
return null;
488+
}
489+
490+
$headers = [];
491+
foreach ($resource->headers as $header) {
492+
$headers[(string) $header->header->attributes()->key] = (string) $header->header->attributes()->value;
493+
}
494+
495+
return $headers;
496+
}
480497
}

src/Metadata/Extractor/YamlResourceExtractor.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private function buildExtendedBase(array $resource): array
123123
'outputFormats' => $this->buildArrayValue($resource, 'outputFormats'),
124124
'stateOptions' => $this->buildStateOptions($resource),
125125
'links' => $this->buildLinks($resource),
126+
'headers' => $this->buildHeaders($resource),
126127
]);
127128
}
128129

@@ -432,4 +433,21 @@ private function buildLinks(array $resource): ?array
432433

433434
return $links;
434435
}
436+
437+
/**
438+
* @return array<string, string>
439+
*/
440+
private function buildHeaders(array $resource): ?array
441+
{
442+
if (!isset($resource['headers']) || !\is_array($resource['headers'])) {
443+
return null;
444+
}
445+
446+
$headers = [];
447+
foreach ($resource['headers'] as $key => $value) {
448+
$headers[$key] = $value;
449+
}
450+
451+
return $headers;
452+
}
435453
}

src/Metadata/Extractor/schema/resources.xsd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,19 @@
407407
</xsd:sequence>
408408
</xsd:complexType>
409409

410+
<xsd:element name="header">
411+
<xsd:complexType>
412+
<xsd:attribute name="key" type="xsd:string"/>
413+
<xsd:attribute name="value" type="xsd:string"/>
414+
</xsd:complexType>
415+
</xsd:element>
416+
417+
<xsd:complexType name="headers">
418+
<xsd:sequence>
419+
<xsd:element ref="header"/>
420+
</xsd:sequence>
421+
</xsd:complexType>
422+
410423
<xsd:group name="base">
411424
<xsd:sequence>
412425
<xsd:element name="denormalizationContext" minOccurs="0" type="sequenceWithValues"/>
@@ -439,6 +452,7 @@
439452
<xsd:element name="types" minOccurs="0" type="types"/>
440453
<xsd:element name="uriVariables" minOccurs="0" type="uriVariables"/>
441454
<xsd:element name="links" minOccurs="0" type="links"/>
455+
<xsd:element name="headers" minOccurs="0" type="headers"/>
442456
</xsd:sequence>
443457
</xsd:group>
444458

src/Metadata/Metadata.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ abstract class Metadata
2424
* @param string|null $deprecationReason https://api-platform.com/docs/core/deprecations/#deprecating-resource-classes-operations-and-properties
2525
* @param string|null $security https://api-platform.com/docs/core/security
2626
* @param string|null $securityPostDenormalize https://api-platform.com/docs/core/security/#executing-access-control-rules-after-denormalization
27-
* @param array<string, string> $headers
2827
* @param mixed|null $mercure
2928
* @param mixed|null $messenger
3029
* @param mixed|null $input

src/Metadata/Tests/Extractor/Adapter/XmlResourceAdapter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,20 @@ private function buildLinks(\SimpleXMLElement $resource, array $values = null):
507507
$childNode->addAttribute('href', $values[0]['href']);
508508
}
509509

510+
private function buildHeaders(\SimpleXMLElement $resource, array $values = null): void
511+
{
512+
if (!$values) {
513+
return;
514+
}
515+
516+
$node = $resource->addChild('headers');
517+
foreach ($values as $key => $value) {
518+
$childNode = $node->addChild('header');
519+
$childNode->addAttribute('key', $key);
520+
$childNode->addAttribute('value', $value);
521+
}
522+
}
523+
510524
private function parse($value): ?string
511525
{
512526
if (null === $value) {

src/Metadata/Tests/Extractor/Adapter/resources.xml

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/Metadata/Tests/Extractor/Adapter/resources.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ resources:
4444
host: api-platform.com
4545
schemes:
4646
- https
47+
headers:
48+
key: value
4749
condition: "request.headers.has('Accept')"
4850
controller: App\Controller\CustomController
4951
class: ApiPlatform\Metadata\GetCollection
@@ -165,6 +167,7 @@ resources:
165167
class: null
166168
urlGenerationStrategy: 1
167169
deprecationReason: 'This resource is deprecated'
170+
headers: null
168171
cacheHeaders:
169172
max_age: 60
170173
shared_max_age: 120

src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ final class ResourceMetadataCompatibilityTest extends TestCase
341341
'status' => 204,
342342
'host' => 'api-platform.com',
343343
'schemes' => ['https'],
344+
'headers' => ['key' => 'value'],
344345
'condition' => 'request.headers.has(\'Accept\')',
345346
'controller' => 'App\Controller\CustomController',
346347
'class' => GetCollection::class,
@@ -506,6 +507,7 @@ final class ResourceMetadataCompatibilityTest extends TestCase
506507
'paginationViaCursor',
507508
'stateOptions',
508509
'links',
510+
'headers',
509511
];
510512

511513
/**

src/Metadata/Tests/Extractor/XmlExtractorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function testValidXML(): void
101101
'write' => null,
102102
'stateOptions' => null,
103103
'links' => null,
104+
'headers' => null,
104105
],
105106
[
106107
'uriTemplate' => '/users/{author}/comments{._format}',
@@ -273,6 +274,7 @@ public function testValidXML(): void
273274
'itemUriTemplate' => null,
274275
'stateOptions' => null,
275276
'links' => null,
277+
'headers' => ['hello' => 'world'],
276278
],
277279
[
278280
'name' => null,
@@ -373,6 +375,7 @@ public function testValidXML(): void
373375
'provider' => null,
374376
'stateOptions' => null,
375377
'links' => null,
378+
'headers' => ['hello' => 'world'],
376379
],
377380
],
378381
'graphQlOperations' => null,
@@ -383,6 +386,7 @@ public function testValidXML(): void
383386
'write' => null,
384387
'stateOptions' => null,
385388
'links' => null,
389+
'headers' => ['hello' => 'world'],
386390
],
387391
],
388392
], $extractor->getResources());

src/Metadata/Tests/Extractor/YamlExtractorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function testValidYaml(): void
101101
'write' => null,
102102
'stateOptions' => null,
103103
'links' => null,
104+
'headers' => null,
104105
],
105106
],
106107
Program::class => [
@@ -172,6 +173,7 @@ public function testValidYaml(): void
172173
'write' => null,
173174
'stateOptions' => null,
174175
'links' => null,
176+
'headers' => null,
175177
],
176178
[
177179
'uriTemplate' => '/users/{author}/programs{._format}',
@@ -314,6 +316,7 @@ public function testValidYaml(): void
314316
'itemUriTemplate' => null,
315317
'stateOptions' => null,
316318
'links' => null,
319+
'headers' => ['hello' => 'world'],
317320
],
318321
[
319322
'name' => null,
@@ -397,6 +400,7 @@ public function testValidYaml(): void
397400
'provider' => null,
398401
'stateOptions' => null,
399402
'links' => null,
403+
'headers' => ['hello' => 'world'],
400404
],
401405
],
402406
'graphQlOperations' => null,
@@ -406,6 +410,7 @@ public function testValidYaml(): void
406410
'write' => null,
407411
'stateOptions' => null,
408412
'links' => null,
413+
'headers' => ['hello' => 'world'],
409414
],
410415
],
411416
SingleFileConfigDummy::class => [
@@ -477,6 +482,7 @@ public function testValidYaml(): void
477482
'write' => null,
478483
'stateOptions' => null,
479484
'links' => null,
485+
'headers' => null,
480486
],
481487
],
482488
], $extractor->getResources());

src/Metadata/Tests/Extractor/xml/valid.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
</cacheHeader>
4646
</cacheHeaders>
4747

48+
<headers>
49+
<header key="hello" value="world" />
50+
</headers>
51+
4852
<normalizationContext>
4953
<values>
5054
<value name="groups">

src/Metadata/Tests/Extractor/yaml/valid.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ resources:
77
uriVariables: ['author']
88
types: ['someirischema']
99
description: User programs
10+
headers:
11+
hello: 'world'
1012
normalizationContext:
1113
groups: ['foo', 'bar']
1214
enabled: false

src/Metadata/composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"doctrine/inflector": "^2.0",
3232
"psr/cache": "^3.0",
3333
"psr/log": "^1.0 || ^2.0 || ^3.0",
34-
"symfony/property-info": "^6.1",
35-
"symfony/string": "^6.1"
34+
"symfony/property-info": "^6.1 || ^7.0",
35+
"symfony/string": "^6.1 || ^7.0"
3636
},
3737
"require-dev": {
3838
"api-platform/json-schema": "*@dev || ^3.1",
@@ -41,12 +41,12 @@
4141
"phpspec/prophecy-phpunit": "^2.0",
4242
"phpstan/phpdoc-parser": "^1.16",
4343
"sebastian/comparator": "<5.0",
44-
"symfony/config": "^6.1",
45-
"symfony/phpunit-bridge": "^6.1",
46-
"symfony/routing": "^6.1",
47-
"symfony/var-dumper": "^6.3",
48-
"symfony/web-link": "^6.3",
49-
"symfony/yaml": "^6.1"
44+
"symfony/config": "^6.1 || ^7.0",
45+
"symfony/phpunit-bridge": "^6.1 || ^7.0",
46+
"symfony/routing": "^6.1 || ^7.0",
47+
"symfony/var-dumper": "^6.3 || ^7.0",
48+
"symfony/web-link": "^6.3 || ^7.0",
49+
"symfony/yaml": "^6.1 || ^7.0"
5050
},
5151
"suggest": {
5252
"phpstan/phpdoc-parser": "For PHP documentation support.",

0 commit comments

Comments
 (0)