Skip to content

Commit

Permalink
Add doctrine/orm 3 support (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored Jul 14, 2024
1 parent 190a9a0 commit b982189
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 174 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"require": {
"php": "^8.0",
"doctrine/collections": "^1.8 || ^2.0",
"doctrine/dbal": "^3.4",
"doctrine/dbal": "^3.6",
"doctrine/event-manager": "^1.2 || ^2.0",
"doctrine/orm": "^2.14",
"doctrine/orm": "^2.14 || ^3.0",
"doctrine/persistence": "^3.0",
"psr/clock": "^1.0",
"symfony/config": "^5.4 || ^6.2 || ^7.0",
Expand All @@ -26,7 +26,7 @@
"doctrine/doctrine-bundle": "^2.7",
"doctrine/doctrine-fixtures-bundle": "^3.4",
"friendsofphp/php-cs-fixer": "^3.4",
"gedmo/doctrine-extensions": "^3.7",
"gedmo/doctrine-extensions": "^3.15",
"matthiasnoback/symfony-dependency-injection-test": "^4.2.1 || ^5.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.11.1@e33492398bd4e5e2ab60e331d445979bd83feecd">
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<!-- NEXT_MAJOR: Remove this file -->
<file src="src/Controller/AuditController.php">
<UndefinedClass occurrences="1">
Expand Down
6 changes: 3 additions & 3 deletions src/AuditConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace SimpleThings\EntityAudit;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use SimpleThings\EntityAudit\Metadata\MetadataFactory;

class AuditConfiguration
Expand Down Expand Up @@ -71,15 +71,15 @@ public static function forEntities(array $classes)
}

/**
* @param ClassMetadataInfo<object> $metadata
* @param ClassMetadata<object> $metadata
*
* @return string
*
* @phpstan-return literal-string
*
* @psalm-suppress MoreSpecificReturnType,LessSpecificReturnStatement https://github.com/vimeo/psalm/issues/10910
*/
public function getTableName(ClassMetadataInfo $metadata)
public function getTableName(ClassMetadata $metadata)
{
/** @var literal-string $tableName */
$tableName = $metadata->getTableName();
Expand Down
108 changes: 44 additions & 64 deletions src/AuditReader.php

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/Collection/AuditedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use SimpleThings\EntityAudit\AuditConfiguration;
use SimpleThings\EntityAudit\AuditReader;
use SimpleThings\EntityAudit\Exception\AuditedCollectionException;
Expand Down Expand Up @@ -58,19 +59,19 @@ class AuditedCollection implements Collection
protected $initialized = false;

/**
* @param string $class
* @param array<string, mixed> $associationDefinition
* @param array<string, mixed> $foreignKeys
* @param string|int $revision
* @param string $class
* @param array<string, mixed>|AssociationMapping $associationDefinition
* @param array<string, mixed> $foreignKeys
* @param string|int $revision
*
* @phpstan-param class-string<T> $class
* @phpstan-param ClassMetadataInfo<T> $metadata
* @phpstan-param ClassMetadata<T> $metadata
*/
public function __construct(
protected AuditReader $auditReader,
protected $class,
protected ClassMetadataInfo $metadata,
protected array $associationDefinition,
protected ClassMetadata $metadata,
protected array|AssociationMapping $associationDefinition,
protected array $foreignKeys,
protected $revision
) {
Expand Down
15 changes: 8 additions & 7 deletions src/DeferredChangedManyToManyEntityRevisionToPersist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@
namespace SimpleThings\EntityAudit;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ManyToManyOwningSideMapping;

/**
* @internal
*/
final class DeferredChangedManyToManyEntityRevisionToPersist
{
/**
* @param array<string, mixed> $assoc
* @param array<string, mixed> $entityData
* @param ClassMetadata<object> $class
* @param ClassMetadata<object> $targetClass
* @param array<string, mixed>|ManyToManyOwningSideMapping $assoc
* @param array<string, mixed> $entityData
* @param ClassMetadata<object> $class
* @param ClassMetadata<object> $targetClass
*/
public function __construct(
private object $entity,
private string $revType,
private array $entityData,
private array $assoc,
private array|ManyToManyOwningSideMapping $assoc,
private ClassMetadata $class,
private ClassMetadata $targetClass
) {
Expand All @@ -55,9 +56,9 @@ public function getEntityData(): array
}

/**
* @return array<string, mixed>
* @return array<string, mixed>|ManyToManyOwningSideMapping
*/
public function getAssoc(): array
public function getAssoc(): array|ManyToManyOwningSideMapping
{
return $this->assoc;
}
Expand Down
22 changes: 13 additions & 9 deletions src/EventListener/CreateSchemaListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
use Doctrine\ORM\Tools\ToolEvents;
use SimpleThings\EntityAudit\AuditConfiguration;
use SimpleThings\EntityAudit\AuditManager;
use SimpleThings\EntityAudit\Metadata\MetadataFactory;
use SimpleThings\EntityAudit\Utils\ORMCompatibilityTrait;

/**
* NEXT_MAJOR: do not implement EventSubscriber interface anymore.
*/
class CreateSchemaListener implements EventSubscriber
{
use ORMCompatibilityTrait;

private AuditConfiguration $config;

private MetadataFactory $metadataFactory;
Expand Down Expand Up @@ -61,6 +64,9 @@ public function getSubscribedEvents()
];
}

/**
* @psalm-suppress TypeDoesNotContainType, NoValue
*/
public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs): void
{
$cm = $eventArgs->getClassMetadata();
Expand Down Expand Up @@ -95,7 +101,7 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
}
$revisionTable->addColumn($this->config->getRevisionFieldName(), $this->config->getRevisionIdFieldType());
$revisionTable->addColumn($this->config->getRevisionTypeFieldName(), Types::STRING, ['length' => 4]);
if (!\in_array($cm->inheritanceType, [ClassMetadataInfo::INHERITANCE_TYPE_NONE, ClassMetadataInfo::INHERITANCE_TYPE_JOINED, ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE], true)) {
if (!\in_array($cm->inheritanceType, [ClassMetadata::INHERITANCE_TYPE_NONE, ClassMetadata::INHERITANCE_TYPE_JOINED, ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE], true)) {
throw new \Exception(sprintf('Inheritance type "%s" is not yet supported', $cm->inheritanceType));
}

Expand All @@ -108,13 +114,11 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
$revisionTable->addIndex([$this->config->getRevisionFieldName()], $revIndexName);

foreach ($cm->associationMappings as $associationMapping) {
if ($associationMapping['isOwningSide'] && isset($associationMapping['joinTable'])) {
if (isset($associationMapping['joinTable']['name'])) {
if ($schema->hasTable($associationMapping['joinTable']['name'])) {
$this->createRevisionJoinTableForJoinTable($schema, $associationMapping['joinTable']['name']);
} else {
$this->defferedJoinTablesToCreate[] = $associationMapping['joinTable']['name'];
}
if (self::isManyToManyOwningSideMapping($associationMapping)) {
if ($schema->hasTable(self::getMappingJoinTableNameValue($associationMapping))) {
$this->createRevisionJoinTableForJoinTable($schema, self::getMappingJoinTableNameValue($associationMapping));
} else {
$this->defferedJoinTablesToCreate[] = self::getMappingJoinTableNameValue($associationMapping);
}
}
}
Expand Down
Loading

0 comments on commit b982189

Please sign in to comment.