Skip to content

Commit

Permalink
Make annotation reader optional
Browse files Browse the repository at this point in the history
  • Loading branch information
W0rma committed Dec 18, 2021
1 parent db088fd commit cb42f25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
25 changes: 16 additions & 9 deletions Request/ParamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
*/
final class ParamReader implements ParamReaderInterface
{
/**
* @var Reader|null
*/
private $annotationReader;

public function __construct(Reader $annotationReader)
public function __construct(?Reader $annotationReader = null)
{
$this->annotationReader = $annotationReader;
}
Expand Down Expand Up @@ -55,10 +58,12 @@ public function getParamsFromMethod(\ReflectionMethod $method): array
$annotations = $this->getParamsFromAttributes($method);
}

$annotations = array_merge(
$annotations,
$this->annotationReader->getMethodAnnotations($method) ?? []
);
if (null !== $this->annotationReader) {
$annotations = array_merge(
$annotations,
$this->annotationReader->getMethodAnnotations($method) ?? []
);
}

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

$annotations = array_merge(
$annotations,
$this->annotationReader->getClassAnnotations($class) ?? []
);
if (null !== $this->annotationReader) {
$annotations = array_merge(
$annotations,
$this->annotationReader->getClassAnnotations($class) ?? []
);
}

return $this->getParamsFromAnnotationArray($annotations);
}
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/request.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<service id="FOS\RestBundle\Request\ParamFetcherInterface" alias="fos_rest.request.param_fetcher" />

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

</services>
Expand Down
3 changes: 1 addition & 2 deletions Tests/Request/ParamReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public function testReadsOnlyParamAnnotations()
*/
public function testReadsAttributes()
{
$annotationReader = $this->getMockBuilder(AnnotationReader::class)->getMock();
$paramReader = new ParamReader($annotationReader);
$paramReader = new ParamReader();
$params = $paramReader->read(new \ReflectionClass(ParamsAnnotatedController::class), 'getArticlesAttributesAction');

$this->assertCount(6, $params);
Expand Down

0 comments on commit cb42f25

Please sign in to comment.