Skip to content

Commit

Permalink
Update DoctrineBehaviors for Symfony 7 and Doctrine ORM 3 compatibility
Browse files Browse the repository at this point in the history
This commit upgrades dependencies and adjusts classes to align with Symfony 7 and Doctrine ORM 3 requirements. Key changes include modifying type hints, updating deprecated `ClassMetadataInfo` usage to `ClassMetadata`, and refining mapping logics. This ensures compatibility with the latest frameworks and improves maintainability.
  • Loading branch information
DennisdeBest committed Dec 26, 2024
1 parent cd825fa commit ec2ccd5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 62 deletions.
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
"require": {
"php": ">=8.1",
"doctrine/common": "^3.3",
"doctrine/persistence": "^2.5|^3.0",
"doctrine/persistence": "^3.0",
"doctrine/dbal": "^3.3",
"doctrine/orm": "^2.12",
"doctrine/orm": "^3.3",
"doctrine/doctrine-bundle": "^2.7.2",
"symfony/cache": "^6.1|^7.0",
"symfony/dependency-injection": "^6.1|^7.0",
"symfony/http-kernel": "^6.1|^7.0",
"symfony/security-bundle": "^6.1|^7.0",
"symfony/framework-bundle": "^6.1|^7.0",
"symfony/string": "^6.1|^7.0",
"symfony/translation-contracts": "^2.4|^3.0",
"nette/utils": "^3.2",
"symfony/cache": "^7.0",
"symfony/dependency-injection": "^7.0",
"symfony/http-kernel": "^7.0",
"symfony/security-bundle": "^7.0",
"symfony/framework-bundle": "^7.0",
"symfony/string": "^7.0",
"symfony/translation-contracts": "^3.0",
"nette/utils": "^4.0",
"ramsey/uuid": "^4.2"
},
"require-dev": {
Expand Down
36 changes: 18 additions & 18 deletions src/EventSubscriber/BlameableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Doctrine\ORM\Event\PreRemoveEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\UnitOfWork;
use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
use Knp\DoctrineBehaviors\Contract\Provider\UserProviderInterface;
Expand Down Expand Up @@ -137,12 +137,12 @@ public function preRemove(PreRemoveEventArgs $preRemoveEventArgs): void
->propertyChanged($object, self::DELETED_BY, $oldDeletedBy, $user);
}

private function mapEntity(ClassMetadataInfo $classMetadataInfo): void
private function mapEntity(ClassMetadata $classMetadata): void
{
if ($this->blameableUserEntity !== null && class_exists($this->blameableUserEntity)) {
$this->mapManyToOneUser($classMetadataInfo);
$this->mapManyToOneUser($classMetadata);
} else {
$this->mapStringUser($classMetadataInfo);
$this->mapStringUser($classMetadata);
}
}

Expand All @@ -151,27 +151,27 @@ private function getUnitOfWork(): UnitOfWork
return $this->entityManager->getUnitOfWork();
}

private function mapManyToOneUser(ClassMetadataInfo $classMetadataInfo): void
private function mapManyToOneUser(ClassMetadata $classMetadata): void
{
$this->mapManyToOneWithTargetEntity($classMetadataInfo, self::CREATED_BY);
$this->mapManyToOneWithTargetEntity($classMetadataInfo, self::UPDATED_BY);
$this->mapManyToOneWithTargetEntity($classMetadataInfo, self::DELETED_BY);
$this->mapManyToOneWithTargetEntity($classMetadata, self::CREATED_BY);
$this->mapManyToOneWithTargetEntity($classMetadata, self::UPDATED_BY);
$this->mapManyToOneWithTargetEntity($classMetadata, self::DELETED_BY);
}

private function mapStringUser(ClassMetadataInfo $classMetadataInfo): void
private function mapStringUser(ClassMetadata $classMetadata): void
{
$this->mapStringNullableField($classMetadataInfo, self::CREATED_BY);
$this->mapStringNullableField($classMetadataInfo, self::UPDATED_BY);
$this->mapStringNullableField($classMetadataInfo, self::DELETED_BY);
$this->mapStringNullableField($classMetadata, self::CREATED_BY);
$this->mapStringNullableField($classMetadata, self::UPDATED_BY);
$this->mapStringNullableField($classMetadata, self::DELETED_BY);
}

private function mapManyToOneWithTargetEntity(ClassMetadataInfo $classMetadataInfo, string $fieldName): void
private function mapManyToOneWithTargetEntity(ClassMetadata $classMetadata, string $fieldName): void
{
if ($classMetadataInfo->hasAssociation($fieldName)) {
if ($classMetadata->hasAssociation($fieldName)) {
return;
}

$classMetadataInfo->mapManyToOne([
$classMetadata->mapManyToOne([
'fieldName' => $fieldName,
'targetEntity' => $this->blameableUserEntity,
'joinColumns' => [
Expand All @@ -182,13 +182,13 @@ private function mapManyToOneWithTargetEntity(ClassMetadataInfo $classMetadataIn
]);
}

private function mapStringNullableField(ClassMetadataInfo $classMetadataInfo, string $fieldName): void
private function mapStringNullableField(ClassMetadata $classMetadata, string $fieldName): void
{
if ($classMetadataInfo->hasField($fieldName)) {
if ($classMetadata->hasField($fieldName)) {
return;
}

$classMetadataInfo->mapField([
$classMetadata->mapField([
'fieldName' => $fieldName,
'type' => 'string',
'nullable' => true,
Expand Down
10 changes: 5 additions & 5 deletions src/EventSubscriber/SluggableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
use Knp\DoctrineBehaviors\Repository\DefaultSluggableRepository;

Expand Down Expand Up @@ -54,18 +54,18 @@ public function preUpdate(PreUpdateEventArgs $preUpdateEventArgs): void
$this->processLifecycleEventArgs($preUpdateEventArgs);
}

private function shouldSkip(ClassMetadataInfo $classMetadataInfo): bool
private function shouldSkip(ClassMetadata $classMetadata): bool
{
if (! is_a($classMetadataInfo->getName(), SluggableInterface::class, true)) {
if (! is_a($classMetadata->getName(), SluggableInterface::class, true)) {
return true;
}

return $classMetadataInfo->hasField(self::SLUG);
return $classMetadata->hasField(self::SLUG);
}

private function processLifecycleEventArgs(PrePersistEventArgs|PreUpdateEventArgs $lifecycleEventArgs): void
{
$entity = $lifecycleEventArgs->getEntity();
$entity = $lifecycleEventArgs->getObject();
if (! $entity instanceof SluggableInterface) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/EventSubscriber/SoftDeletableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class SoftDeletableEventSubscriber

public function onFlush(OnFlushEventArgs $onFlushEventArgs): void
{
$entityManager = $onFlushEventArgs->getEntityManager();
$entityManager = $onFlushEventArgs->getObjectManager();
$unitOfWork = $entityManager->getUnitOfWork();

foreach ($unitOfWork->getScheduledEntityDeletions() as $entity) {
Expand Down
50 changes: 25 additions & 25 deletions src/EventSubscriber/TranslatableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Doctrine\ORM\Event\PostLoadEventArgs;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
Expand Down Expand Up @@ -83,51 +83,51 @@ private function convertFetchString(string|int $fetchMode): int
}

if ($fetchMode === 'EAGER') {
return ClassMetadataInfo::FETCH_EAGER;
return ClassMetadata::FETCH_EAGER;
}

if ($fetchMode === 'EXTRA_LAZY') {
return ClassMetadataInfo::FETCH_EXTRA_LAZY;
return ClassMetadata::FETCH_EXTRA_LAZY;
}

return ClassMetadataInfo::FETCH_LAZY;
return ClassMetadata::FETCH_LAZY;
}

private function mapTranslatable(ClassMetadataInfo $classMetadataInfo): void
private function mapTranslatable(ClassMetadata $classMetadata): void
{
if ($classMetadataInfo->hasAssociation('translations')) {
if ($classMetadata->hasAssociation('translations')) {
return;
}

$classMetadataInfo->mapOneToMany([
$classMetadata->mapOneToMany([
'fieldName' => 'translations',
'mappedBy' => 'translatable',
'indexBy' => self::LOCALE,
'cascade' => ['persist', 'merge', 'remove'],
'cascade' => ['persist', 'remove'],
'fetch' => $this->translatableFetchMode,
'targetEntity' => $classMetadataInfo->getReflectionClass()
'targetEntity' => $classMetadata->getReflectionClass()
->getMethod('getTranslationEntityClass')
->invoke(null),
'orphanRemoval' => true,
]);
}

private function mapTranslation(ClassMetadataInfo $classMetadataInfo, ObjectManager $objectManager): void
private function mapTranslation(ClassMetadata $classMetadata, ObjectManager $objectManager): void
{
if (! $classMetadataInfo->hasAssociation('translatable')) {
$targetEntity = $classMetadataInfo->getReflectionClass()
if (! $classMetadata->hasAssociation('translatable')) {
$targetEntity = $classMetadata->getReflectionClass()
->getMethod('getTranslatableEntityClass')
->invoke(null);

/** @var ClassMetadataInfo $classMetadata */
$classMetadata = $objectManager->getClassMetadata($targetEntity);
/** @var ClassMetadata $classPersistenceMetadata */
$classPersistenceMetadata = $objectManager->getClassMetadata($targetEntity);

$singleIdentifierFieldName = $classMetadata->getSingleIdentifierFieldName();
$singleIdentifierFieldName = $classPersistenceMetadata->getSingleIdentifierFieldName();

$classMetadataInfo->mapManyToOne([
$classMetadata->mapManyToOne([
'fieldName' => 'translatable',
'inversedBy' => 'translations',
'cascade' => ['persist', 'merge'],
'cascade' => ['persist'],
'fetch' => $this->translationFetchMode,
'joinColumns' => [[
'name' => 'translatable_id',
Expand All @@ -138,16 +138,16 @@ private function mapTranslation(ClassMetadataInfo $classMetadataInfo, ObjectMana
]);
}

$name = $classMetadataInfo->getTableName() . '_unique_translation';
if (! $this->hasUniqueTranslationConstraint($classMetadataInfo, $name) &&
$classMetadataInfo->getName() === $classMetadataInfo->rootEntityName) {
$classMetadataInfo->table['uniqueConstraints'][$name] = [
$name = $classMetadata->getTableName() . '_unique_translation';
if (! $this->hasUniqueTranslationConstraint($classMetadata, $name) &&
$classMetadata->getName() === $classMetadata->rootEntityName) {
$classMetadata->table['uniqueConstraints'][$name] = [
'columns' => ['translatable_id', self::LOCALE],
];
}

if (! $classMetadataInfo->hasField(self::LOCALE) && ! $classMetadataInfo->hasAssociation(self::LOCALE)) {
$classMetadataInfo->mapField([
if (! $classMetadata->hasField(self::LOCALE) && ! $classMetadata->hasAssociation(self::LOCALE)) {
$classMetadata->mapField([
'fieldName' => self::LOCALE,
'type' => 'string',
'length' => 5,
Expand All @@ -173,8 +173,8 @@ private function setLocales(PostLoadEventArgs|PrePersistEventArgs $lifecycleEven
}
}

private function hasUniqueTranslationConstraint(ClassMetadataInfo $classMetadataInfo, string $name): bool
private function hasUniqueTranslationConstraint(ClassMetadata $classMetadata, string $name): bool
{
return isset($classMetadataInfo->table['uniqueConstraints'][$name]);
return isset($classMetadata->table['uniqueConstraints'][$name]);
}
}
5 changes: 3 additions & 2 deletions src/Model/Translatable/TranslatableMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
trait TranslatableMethodsTrait
{
/**
* @return Collection<string, TranslationInterface>
* @return ArrayCollection|Collection
*/
public function getTranslations()
public function getTranslations(): ArrayCollection|Collection
{
// initialize collection, usually in ctor
if ($this->translations === null) {
Expand All @@ -27,6 +27,7 @@ public function getTranslations()
/**
* @param Collection<string, TranslationInterface> $translations
* @phpstan-param iterable<TranslationInterface> $translations
* @throws TranslatableException
*/
public function setTranslations(iterable $translations): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Translatable/TranslationPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait TranslationPropertiesTrait
protected $locale;

/**
* Will be mapped to translatable entity by TranslatableSubscriber
* Will be mapped to translatable entity by TranslatableEventSubscriber
*
* @var TranslatableInterface
*/
Expand Down

0 comments on commit ec2ccd5

Please sign in to comment.