Skip to content

Commit f85f9d8

Browse files
committed
Make the Annotations package optional in the builder API
1 parent 9c2ac39 commit f85f9d8

File tree

6 files changed

+24
-9
lines changed

6 files changed

+24
-9
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
],
1818
"require": {
1919
"php": "^7.2||^8.0",
20-
"doctrine/annotations": "^1.13 || ^2.0",
2120
"doctrine/instantiator": "^1.0.3 || ^2.0",
2221
"doctrine/lexer": "^2.0 || ^3.0",
2322
"jms/metadata": "^2.6",
@@ -31,6 +30,7 @@
3130
},
3231
"require-dev": {
3332
"ext-pdo_sqlite": "*",
33+
"doctrine/annotations": "^1.13 || ^2.0",
3434
"doctrine/coding-standard": "^12.0",
3535
"doctrine/orm": "~2.1",
3636
"doctrine/persistence": "^1.3.3|^2.0|^3.0",
@@ -51,6 +51,9 @@
5151
"symfony/yaml": "^3.3|^4.0|^5.0|^6.0|^7.0",
5252
"twig/twig": "~1.34|~2.4|^3.0"
5353
},
54+
"conflict": {
55+
"doctrine/annotations": "<1.13 || >=3.0"
56+
},
5457
"autoload": {
5558
"psr-4": {
5659
"JMS\\Serializer\\": "src/"

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 & 2 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;
@@ -55,8 +56,12 @@ public function enableEnumSupport(bool $enableEnumSupport = true): void
5556
$this->enableEnumSupport = $enableEnumSupport;
5657
}
5758

58-
public function createDriver(array $metadataDirs, Reader $annotationReader): DriverInterface
59+
public function createDriver(array $metadataDirs, ?Reader $annotationReader = null): DriverInterface
5960
{
61+
if (empty($metadataDirs) && !interface_exists(Reader::class) && PHP_VERSION_ID < 80000) {
62+
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));
63+
}
64+
6065
/*
6166
* Build the sorted list of metadata drivers based on the environment. The final order should be:
6267
*
@@ -65,7 +70,11 @@ public function createDriver(array $metadataDirs, Reader $annotationReader): Dri
6570
* - Annotations/Attributes Driver
6671
* - Null (Fallback) Driver
6772
*/
68-
$metadataDrivers = [new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader)];
73+
$metadataDrivers = [];
74+
75+
if (PHP_VERSION_ID >= 80000 || $annotationReader instanceof Reader) {
76+
$metadataDrivers[] = new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader);
77+
}
6978

7079
if (!empty($metadataDirs)) {
7180
$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)