Skip to content

drop annotation support with entities #1126

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

Merged
merged 1 commit into from
May 24, 2022
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
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"sort-packages": true
},
"conflict": {
"doctrine/orm": "<2.10"
"doctrine/orm": "<2.10",
"doctrine/doctrine-bundle": "<2.4",
"symfony/doctrine-bridge": "<5.4"
},
"autoload": {
"psr-4": { "Symfony\\Bundle\\MakerBundle\\": "src/" }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function process(ContainerBuilder $container): void
}

if (null !== $annotatedPrefixes) {
$container->getDefinition('maker.doctrine_helper')->setArgument(4, $annotatedPrefixes);
$container->getDefinition('maker.doctrine_helper')->setArgument(2, $annotatedPrefixes);
}
}
}
2 changes: 0 additions & 2 deletions src/Doctrine/BaseCollectionRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
abstract class BaseCollectionRelation extends BaseRelation
{
abstract public function getOrphanRemoval(): bool;

abstract public function getTargetSetterMethodName(): string;

public function getAdderMethodName(): string
Expand Down
80 changes: 25 additions & 55 deletions src/Doctrine/BaseRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,51 @@
*/
abstract class BaseRelation
{
private $propertyName;
private $targetClassName;
private $targetPropertyName;
private $customReturnType;
private $isSelfReferencing = false;
private $mapInverseRelation = true;
private $avoidSetter = false;

abstract public function isOwning(): bool;
public function __construct(
private string $propertyName,
private string $targetClassName,
private ?string $targetPropertyName = null,
private bool $isSelfReferencing = false,
private bool $mapInverseRelation = true,
private bool $avoidSetter = false,
private bool $isCustomReturnTypeNullable = false,
private ?string $customReturnType = null,
private bool $isOwning = false,
private bool $orphanRemoval = false,
private bool $isNullable = false,
) {
}

public function getPropertyName(): string
{
return $this->propertyName;
}

public function setPropertyName(string $propertyName): self
{
$this->propertyName = $propertyName;

return $this;
}

public function getTargetClassName(): string
{
return $this->targetClassName;
}

public function setTargetClassName(string $targetClassName): self
{
$this->targetClassName = $targetClassName;

return $this;
}

public function getTargetPropertyName(): ?string
{
return $this->targetPropertyName;
}

public function setTargetPropertyName(?string $targetPropertyName): self
{
$this->targetPropertyName = $targetPropertyName;

return $this;
}

public function isSelfReferencing(): bool
{
return $this->isSelfReferencing;
}

public function setIsSelfReferencing(bool $isSelfReferencing): self
{
$this->isSelfReferencing = $isSelfReferencing;

return $this;
}

public function getMapInverseRelation(): bool
{
return $this->mapInverseRelation;
}

public function setMapInverseRelation(bool $mapInverseRelation): self
{
$this->mapInverseRelation = $mapInverseRelation;

return $this;
}

public function shouldAvoidSetter(): bool
{
return $this->avoidSetter;
}

public function avoidSetter(bool $avoidSetter = true): self
{
$this->avoidSetter = $avoidSetter;

return $this;
}

public function getCustomReturnType(): ?string
{
return $this->customReturnType;
Expand All @@ -108,11 +71,18 @@ public function isCustomReturnTypeNullable(): bool
return $this->isCustomReturnTypeNullable;
}

public function setCustomReturnType(string $customReturnType, bool $isNullable)
public function isOwning(): bool
{
$this->customReturnType = $customReturnType;
$this->isCustomReturnTypeNullable = $isNullable;
return $this->isOwning;
}

return $this;
public function getOrphanRemoval(): bool
{
return $this->orphanRemoval;
}

public function isNullable(): bool
{
return $this->isNullable;
}
}
36 changes: 0 additions & 36 deletions src/Doctrine/BaseSingleRelation.php

This file was deleted.

61 changes: 16 additions & 45 deletions src/Doctrine/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\MappingException as ORMMappingException;
use Doctrine\ORM\Mapping\NamingStrategy;
Expand All @@ -25,7 +24,6 @@
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Persistence\Mapping\MappingException as PersistenceMappingException;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;

/**
* @author Fabien Potencier <[email protected]>
Expand All @@ -36,27 +34,12 @@
*/
final class DoctrineHelper
{
/**
* @var string
*/
private $entityNamespace;
private $phpCompatUtil;
private $registry;

/**
* @var array|null
*/
private $mappingDriversByPrefix;

private $attributeMappingSupport;

public function __construct(string $entityNamespace, PhpCompatUtil $phpCompatUtil, ManagerRegistry $registry = null, bool $attributeMappingSupport = false, array $annotatedPrefixes = null)
{
public function __construct(
private string $entityNamespace,
private ?ManagerRegistry $registry = null,
private ?array $mappingDriversByPrefix = null,
) {
$this->entityNamespace = trim($entityNamespace, '\\');
$this->phpCompatUtil = $phpCompatUtil;
$this->registry = $registry;
$this->attributeMappingSupport = $attributeMappingSupport;
$this->mappingDriversByPrefix = $annotatedPrefixes;
}

public function getRegistry(): ManagerRegistry
Expand Down Expand Up @@ -114,7 +97,7 @@ public function doesClassUseDriver(string $className, string $driverClass): bool
}

foreach ($metadataDriver->getDrivers() as $namespace => $driver) {
if (0 === strpos($className, $namespace)) {
if (str_starts_with($className, $namespace)) {
return $this->isInstanceOf($driver, $driverClass);
}
}
Expand All @@ -125,27 +108,22 @@ public function doesClassUseDriver(string $className, string $driverClass): bool
$managerName = array_search($em, $this->getRegistry()->getManagers(), true);

foreach ($this->mappingDriversByPrefix[$managerName] as [$prefix, $prefixDriver]) {
if (0 === strpos($className, $prefix)) {
if (str_starts_with($className, $prefix)) {
return $this->isInstanceOf($prefixDriver, $driverClass);
}
}

return false;
}

public function isClassAnnotated(string $className): bool
{
return $this->doesClassUseDriver($className, AnnotationDriver::class);
}

public function doesClassUsesAttributes(string $className): bool
{
return $this->doesClassUseDriver($className, AttributeDriver::class);
}

public function isDoctrineSupportingAttributes(): bool
Copy link
Member

Choose a reason for hiding this comment

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

After we remove that one if statement we talked about in this PR, is this used anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It won't be when we're all said and done, but make:user && make:reset-password are still calling on this.. they need to be refactored after this PR && the CSM one.

{
return $this->isDoctrineInstalled() && $this->attributeMappingSupport;
return $this->isDoctrineInstalled();
}

public function getEntitiesForAutocomplete(): array
Expand All @@ -168,18 +146,14 @@ public function getEntitiesForAutocomplete(): array

public function getMetadata(string $classOrNamespace = null, bool $disconnected = false): array|ClassMetadata
{
// Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
// Invalidating the cached AttributeDriver::$classNames to find new Entity classes
foreach ($this->mappingDriversByPrefix ?? [] as $managerName => $prefixes) {
foreach ($prefixes as [$prefix, $annotationDriver]) {
if (null !== $annotationDriver) {
if ($annotationDriver instanceof AnnotationDriver) {
$classNames = (new \ReflectionClass(AnnotationDriver::class))->getProperty('classNames');
} else {
$classNames = (new \ReflectionClass(AttributeDriver::class))->getProperty('classNames');
}
foreach ($prefixes as [$prefix, $attributeDriver]) {
if ($attributeDriver instanceof AttributeDriver) {
$classNames = (new \ReflectionClass(AttributeDriver::class))->getProperty('classNames');

$classNames->setAccessible(true);
$classNames->setValue($annotationDriver, null);
$classNames->setValue($attributeDriver, null);
}
}
}
Expand All @@ -205,14 +179,11 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
}

if (null === $this->mappingDriversByPrefix) {
// Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
// Invalidating the cached AttributeDriver::$classNames to find new Entity classes
$metadataDriver = $em->getConfiguration()->getMetadataDriverImpl();

if ($this->isInstanceOf($metadataDriver, MappingDriverChain::class)) {
foreach ($metadataDriver->getDrivers() as $driver) {
if ($this->isInstanceOf($driver, AnnotationDriver::class)) {
$classNames->setValue($driver, null);
}

if ($this->isInstanceOf($driver, AttributeDriver::class)) {
$classNames->setValue($driver, null);
}
Expand All @@ -229,7 +200,7 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
return $m;
}

if (0 === strpos($m->getName(), $classOrNamespace)) {
if (str_starts_with($m->getName(), $classOrNamespace)) {
$metadata[$m->getName()] = $m;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
'broadcast' => $broadcast,
'should_escape_table_name' => $this->doctrineHelper->isKeyword($tableName),
'table_name' => $tableName,
'doctrine_use_attributes' => $this->doctrineHelper->isDoctrineSupportingAttributes() && $this->doctrineHelper->doesClassUsesAttributes($entityClassDetails->getFullName()),
]
);

Expand Down
Loading