Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[B/C Break] Make the Annotations package optional in the builder API #1471

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
],
"require": {
"php": "^7.4 || ^8.0",
"doctrine/annotations": "^1.14 || ^2.0",
"doctrine/instantiator": "^1.3.1 || ^2.0",
"doctrine/lexer": "^2.0 || ^3.0",
"jms/metadata": "^2.6",
Expand All @@ -37,6 +36,7 @@
},
"require-dev": {
"ext-pdo_sqlite": "*",
"doctrine/annotations": "^1.14 || ^2.0",
"doctrine/coding-standard": "^12.0",
"doctrine/orm": "^2.14 || ^3.0",
"doctrine/persistence": "^2.5.2 || ^3.0",
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
13 changes: 11 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,12 @@ 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)) {
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 +70,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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one line is why there is a B/C break; widening the annotation reader argument to allow null causes compile errors in classes if they don't do the same.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should not be a big issues. JMS Bundle seems to not using those classes, so it will affect only people who is using package without Bundle.

}
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
Loading