Skip to content

Commit cb42f25

Browse files
committed
Make annotation reader optional
1 parent db088fd commit cb42f25

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

Request/ParamReader.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
*/
2424
final class ParamReader implements ParamReaderInterface
2525
{
26+
/**
27+
* @var Reader|null
28+
*/
2629
private $annotationReader;
2730

28-
public function __construct(Reader $annotationReader)
31+
public function __construct(?Reader $annotationReader = null)
2932
{
3033
$this->annotationReader = $annotationReader;
3134
}
@@ -55,10 +58,12 @@ public function getParamsFromMethod(\ReflectionMethod $method): array
5558
$annotations = $this->getParamsFromAttributes($method);
5659
}
5760

58-
$annotations = array_merge(
59-
$annotations,
60-
$this->annotationReader->getMethodAnnotations($method) ?? []
61-
);
61+
if (null !== $this->annotationReader) {
62+
$annotations = array_merge(
63+
$annotations,
64+
$this->annotationReader->getMethodAnnotations($method) ?? []
65+
);
66+
}
6267

6368
return $this->getParamsFromAnnotationArray($annotations);
6469
}
@@ -73,10 +78,12 @@ public function getParamsFromClass(\ReflectionClass $class): array
7378
$annotations = $this->getParamsFromAttributes($class);
7479
}
7580

76-
$annotations = array_merge(
77-
$annotations,
78-
$this->annotationReader->getClassAnnotations($class) ?? []
79-
);
81+
if (null !== $this->annotationReader) {
82+
$annotations = array_merge(
83+
$annotations,
84+
$this->annotationReader->getClassAnnotations($class) ?? []
85+
);
86+
}
8087

8188
return $this->getParamsFromAnnotationArray($annotations);
8289
}

Resources/config/request.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<service id="FOS\RestBundle\Request\ParamFetcherInterface" alias="fos_rest.request.param_fetcher" />
1717

1818
<service id="fos_rest.request.param_fetcher.reader" class="FOS\RestBundle\Request\ParamReader" public="false">
19-
<argument type="service" id="annotation_reader"/>
19+
<argument type="service" id="annotation_reader" on-invalid="null"/>
2020
</service>
2121

2222
</services>

Tests/Request/ParamReaderTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ public function testReadsOnlyParamAnnotations()
9898
*/
9999
public function testReadsAttributes()
100100
{
101-
$annotationReader = $this->getMockBuilder(AnnotationReader::class)->getMock();
102-
$paramReader = new ParamReader($annotationReader);
101+
$paramReader = new ParamReader();
103102
$params = $paramReader->read(new \ReflectionClass(ParamsAnnotatedController::class), 'getArticlesAttributesAction');
104103

105104
$this->assertCount(6, $params);

0 commit comments

Comments
 (0)