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 authored and Marcin Czarnecki committed Feb 18, 2024
1 parent 9e15e63 commit 2ccf709
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
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
12 changes: 10 additions & 2 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 @@ -55,8 +56,11 @@ 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
{
if (PHP_VERSION_ID < 80000 && empty($metadataDirs) && !interface_exists(Reader::class)) {

Check failure on line 61 in src/Builder/DefaultDriverFactory.php

View workflow job for this annotation

GitHub Actions / Coding Standards (7.4)

Expected 1 line after "if", found 0.
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));
}
/*
* Build the sorted list of metadata drivers based on the environment. The final order should be:
*
Expand All @@ -65,7 +69,11 @@ public function createDriver(array $metadataDirs, Reader $annotationReader): Dri
* - Annotations/Attributes Driver
* - Null (Fallback) Driver
*/
$metadataDrivers = [new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader)];
$metadataDrivers = [];

if (PHP_VERSION_ID >= 80000 || $annotationReader instanceof Reader) {
$metadataDrivers[] = 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 2ccf709

Please sign in to comment.