Skip to content

Commit

Permalink
Make the Annotations package optional in the builder API
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Aug 1, 2023
1 parent d436996 commit 50a8d22
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
],
"require": {
"php": "^7.2||^8.0",
"doctrine/annotations": "^1.13 || ^2.0",
"doctrine/instantiator": "^1.0.3 || ^2.0",
"doctrine/lexer": "^2.0 || ^3.0",
"jms/metadata": "^2.6",
Expand All @@ -31,6 +30,7 @@
},
"require-dev": {
"ext-pdo_sqlite": "*",
"doctrine/annotations": "^1.13 || ^2.0",
"doctrine/coding-standard": "^8.1",
"doctrine/orm": "~2.1",
"doctrine/persistence": "^1.3.3|^2.0|^3.0",
Expand All @@ -51,6 +51,9 @@
"symfony/yaml": "^3.3|^4.0|^5.0|^6.0",
"twig/twig": "~1.34|~2.4|^3.0"
},
"conflict": {
"doctrine/annotations": "<1.13 || >=3.0"
},
"autoload": {
"psr-4": {
"JMS\\Serializer\\": "src/"
Expand Down
6 changes: 5 additions & 1 deletion src/Builder/CallbackDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ final class CallbackDriverFactory implements DriverFactoryInterface
{
/**
* @var callable
* @phpstan-var callable(array $metadataDirs, Reader|null $reader): DriverInterface
*/
private $callback;

/**
* @phpstan-param callable(array $metadataDirs, Reader|null $reader): DriverInterface $callable
*/
public function __construct(callable $callable)
{
$this->callback = $callable;
}

public function createDriver(array $metadataDirs, Reader $reader): DriverInterface
public function createDriver(array $metadataDirs, ?Reader $reader = null): DriverInterface
{
$driver = \call_user_func($this->callback, $metadataDirs, $reader);
if (!$driver instanceof DriverInterface) {
Expand Down
15 changes: 11 additions & 4 deletions src/Builder/DefaultDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace JMS\Serializer\Builder;

use Doctrine\Common\Annotations\Reader;
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
use JMS\Serializer\Metadata\Driver\AnnotationOrAttributeDriver;
use JMS\Serializer\Metadata\Driver\DefaultValuePropertyDriver;
Expand Down Expand Up @@ -54,11 +55,17 @@ public function enableEnumSupport(bool $enableEnumSupport = true): void
$this->enableEnumSupport = $enableEnumSupport;
}

public function createDriver(array $metadataDirs, Reader $annotationReader): DriverInterface
public function createDriver(array $metadataDirs, ?Reader $annotationReader = null): DriverInterface
{
$driver = new DriverChain([
new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader),
]);
if (empty($metadataDirs) && !interface_exists(Reader::class) && PHP_VERSION_ID < 80000) {
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));
}

$driver = new DriverChain();

if (PHP_VERSION_ID >= 80000 || $annotationReader instanceof Reader) {
$driver->addDriver(new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader));
}

if (!empty($metadataDirs)) {
$fileLocator = new FileLocator($metadataDirs);
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/DocBlockDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(DriverFactoryInterface $driverFactoryToDecorate, ?Pa
$this->typeParser = $typeParser;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/Builder/DriverFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface DriverFactoryInterface
{
public function createDriver(array $metadataDirs, Reader $annotationReader): DriverInterface;
public function createDriver(array $metadataDirs, ?Reader $annotationReader = null): DriverInterface;
}
5 changes: 2 additions & 3 deletions src/SerializerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,8 @@ public function setDocBlockTypeResolver(bool $docBlockTypeResolver): self
public function build(): Serializer
{
$annotationReader = $this->annotationReader;
if (null === $annotationReader) {
$annotationReader = new AnnotationReader();
$annotationReader = $this->decorateAnnotationReader($annotationReader);
if (null === $annotationReader && class_exists(AnnotationReader::class)) {
$annotationReader = $this->decorateAnnotationReader(new AnnotationReader());
}

if (null === $this->driverFactory) {
Expand Down

0 comments on commit 50a8d22

Please sign in to comment.