Skip to content

Commit dab3e9d

Browse files
committed
Make the Annotations package optional in the builder API
1 parent d436996 commit dab3e9d

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

src/Builder/CallbackDriverFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ final class CallbackDriverFactory implements DriverFactoryInterface
1212
{
1313
/**
1414
* @var callable
15+
* @phpstan-var callable(array $metadataDirs, Reader|null $reader): DriverInterface
1516
*/
1617
private $callback;
1718

19+
/**
20+
* @phpstan-param callable(array $metadataDirs, Reader|null $reader): DriverInterface $callable
21+
*/
1822
public function __construct(callable $callable)
1923
{
2024
$this->callback = $callable;
2125
}
2226

23-
public function createDriver(array $metadataDirs, Reader $reader): DriverInterface
27+
public function createDriver(array $metadataDirs, ?Reader $reader = null): DriverInterface
2428
{
2529
$driver = \call_user_func($this->callback, $metadataDirs, $reader);
2630
if (!$driver instanceof DriverInterface) {

src/Builder/DefaultDriverFactory.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace JMS\Serializer\Builder;
66

77
use Doctrine\Common\Annotations\Reader;
8+
use JMS\Serializer\Exception\RuntimeException;
89
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
910
use JMS\Serializer\Metadata\Driver\AnnotationOrAttributeDriver;
1011
use JMS\Serializer\Metadata\Driver\DefaultValuePropertyDriver;
@@ -54,11 +55,17 @@ public function enableEnumSupport(bool $enableEnumSupport = true): void
5455
$this->enableEnumSupport = $enableEnumSupport;
5556
}
5657

57-
public function createDriver(array $metadataDirs, Reader $annotationReader): DriverInterface
58+
public function createDriver(array $metadataDirs, ?Reader $annotationReader = null): DriverInterface
5859
{
59-
$driver = new DriverChain([
60-
new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader),
61-
]);
60+
if (empty($metadataDirs) && !interface_exists(Reader::class) && PHP_VERSION_ID < 80000) {
61+
throw new RuntimeException(sprintf('To use "%s", either a list of metadata directories must be provided, the "doctrine/annotations" package installed, or use PHP 8.0 or later.', self::class));
62+
}
63+
64+
$driver = new DriverChain();
65+
66+
if (PHP_VERSION_ID >= 80000 || $annotationReader instanceof Reader) {
67+
$driver->addDriver(new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader));
68+
}
6269

6370
if (!empty($metadataDirs)) {
6471
$fileLocator = new FileLocator($metadataDirs);

src/Builder/DocBlockDriverFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(DriverFactoryInterface $driverFactoryToDecorate, ?Pa
2626
$this->typeParser = $typeParser;
2727
}
2828

29-
public function createDriver(array $metadataDirs, Reader $annotationReader): DriverInterface
29+
public function createDriver(array $metadataDirs, ?Reader $annotationReader = null): DriverInterface
3030
{
3131
$driver = $this->driverFactoryToDecorate->createDriver($metadataDirs, $annotationReader);
3232

src/Builder/DriverFactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
interface DriverFactoryInterface
1111
{
12-
public function createDriver(array $metadataDirs, Reader $annotationReader): DriverInterface;
12+
public function createDriver(array $metadataDirs, ?Reader $annotationReader = null): DriverInterface;
1313
}

src/SerializerBuilder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,8 @@ public function setDocBlockTypeResolver(bool $docBlockTypeResolver): self
552552
public function build(): Serializer
553553
{
554554
$annotationReader = $this->annotationReader;
555-
if (null === $annotationReader) {
556-
$annotationReader = new AnnotationReader();
557-
$annotationReader = $this->decorateAnnotationReader($annotationReader);
555+
if (null === $annotationReader && class_exists(AnnotationReader::class)) {
556+
$annotationReader = $this->decorateAnnotationReader(new AnnotationReader());
558557
}
559558

560559
if (null === $this->driverFactory) {

0 commit comments

Comments
 (0)