Skip to content

Commit bbccc03

Browse files
tobias-93bobvandevijver
authored andcommitted
Allow higher Doctrine and Symfony dependencies
1 parent 07ebbf7 commit bbccc03

8 files changed

+51
-46
lines changed

Configuration/HistoryConfiguration.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Bobv\EntityHistoryBundle\Configuration;
44

5+
use Exception;
6+
use LogicException;
57
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
68

79
/**
@@ -80,8 +82,8 @@ public function getDeletedByValue(): mixed {
8082
$method = $this->deletedByMethod;
8183
try {
8284
return $this->authorizationChecker->$method();
83-
} catch (\Exception $e) {
84-
throw new \LogicException(sprintf('The method "%s" could not be called on "%s" to generate the deleted by value', $method, get_class($this->authorizationChecker)));
85+
} catch (Exception) {
86+
throw new LogicException(sprintf('The method "%s" could not be called on "%s" to generate the deleted by value', $method, get_class($this->authorizationChecker)));
8587
}
8688
}
8789

DependencyInjection/BobvEntityHistoryExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function load(array $configs, ContainerBuilder $container): void {
2525
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
2626
$loader->load('services.yml');
2727

28-
$configurables = array(
28+
$configurables = [
2929
'table_prefix',
3030
'table_suffix',
3131
'revision_field_name',
@@ -34,7 +34,7 @@ public function load(array $configs, ContainerBuilder $container): void {
3434
'deleted_at_field',
3535
'deleted_by_field',
3636
'deleted_by_method',
37-
);
37+
];
3838

3939
foreach ($configurables as $key) {
4040
$container->setParameter("bobv.entityhistory." . $key, $config[$key]);

DependencyInjection/Configuration.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public function getConfigTreeBuilder(): TreeBuilder
2323
->children()
2424
->arrayNode('entities')
2525
->prototype('scalar')->end()
26-
->defaultValue(array())
26+
->defaultValue([])
2727
->end()
2828
->arrayNode('connections')
2929
->prototype('scalar')->end()
30-
->defaultValue(array('default'))
30+
->defaultValue(['default'])
3131
->end()
3232
->scalarNode('table_prefix')
3333
->defaultValue('_HISTORY_')

EventSubscriber/CreateSchemaSubscriber.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Bobv\EntityHistoryBundle\Configuration\HistoryConfiguration;
66
use Doctrine\DBAL\Schema\Column;
7+
use Doctrine\DBAL\Types\Type;
78
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
89

910
/**
@@ -38,25 +39,23 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
3839

3940
// Get id column (if any)
4041
if ($entityTable->hasColumn('id')) {
41-
/* @var $column Column */
4242
$column = $entityTable->getColumn('id');
43-
$revisionTable->addColumn($column->getName(), $column->getType()->getName(), array_merge(
43+
$revisionTable->addColumn($column->getName(), Type::lookupName($column->getType()), array_merge(
4444
$this->getColumnOptions($column),
45-
array('notnull' => false, 'autoincrement' => false)
45+
['notnull' => false, 'autoincrement' => false]
4646
));
4747
}
4848

4949
// Add revision info
5050
$revisionTable->addColumn($this->configuration->getRevisionFieldName(), 'integer');
51-
$revisionTable->addColumn($this->configuration->getRevisionTypeFieldName(), 'string', array('length' => 4));
51+
$revisionTable->addColumn($this->configuration->getRevisionTypeFieldName(), 'string', ['length' => 4]);
5252

5353
// Get each column (except id) and add it to the table
5454
foreach ($entityTable->getColumns() AS $column) {
5555
if ($column->getName() == 'id') continue;
56-
/* @var $column Column */
57-
$revisionTable->addColumn($column->getName(), $column->getType()->getName(), array_merge(
56+
$revisionTable->addColumn($column->getName(), Type::lookupName($column->getType()), array_merge(
5857
$this->getColumnOptions($column),
59-
array('notnull' => false, 'autoincrement' => false)
58+
['notnull' => false, 'autoincrement' => false]
6059
));
6160
}
6261

EventSubscriber/LogHistorySubscriber.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Bobv\EntityHistoryBundle\EventSubscriber;
44

55
use Bobv\EntityHistoryBundle\Configuration\HistoryConfiguration;
6+
use DateTime;
67
use Doctrine\DBAL\Connection;
78
use Doctrine\DBAL\Platforms\AbstractPlatform;
89
use Doctrine\DBAL\Types\Type;
@@ -12,6 +13,9 @@
1213
use Doctrine\ORM\Event\PostUpdateEventArgs;
1314
use Doctrine\ORM\Mapping\ClassMetadata;
1415
use Doctrine\ORM\UnitOfWork;
16+
use Doctrine\ORM\Utility\PersisterHelper;
17+
use LogicException;
18+
use PDO;
1519

1620
/**
1721
* Based on the work of
@@ -65,7 +69,7 @@ public function onFlush(OnFlushEventArgs $eventArgs): void {
6569
// Check if there is a deletedAt field configured which we can set
6670
if (null !== ($deletedAtField = $this->config->getDeletedAtField())) {
6771
$deletedAtValue = [];
68-
$deletedAtValue[$deletedAtField] = new \DateTime();
72+
$deletedAtValue[$deletedAtField] = new DateTime();
6973
$entityData = array_merge($entityData, $deletedAtValue);
7074
}
7175

@@ -102,7 +106,7 @@ public function postUpdate(PostUpdateEventArgs $eventArgs): void {
102106
return;
103107
}
104108

105-
$entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity));
109+
$entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity));
106110
// Check if the deleted field was set before, if so, it if and REVERT
107111
if ($this->config->isReverted($class->name, $entityData['id'])) {
108112
$this->saveRevisionEntityData($class, $entityData, 'REV');
@@ -113,13 +117,13 @@ public function postUpdate(PostUpdateEventArgs $eventArgs): void {
113117

114118
private function getInsertRevisionSQL(ClassMetadata $class): string {
115119
if (!isset($this->insertRevisionSQL[$class->name])) {
116-
$placeholders = array('?', '?');
120+
$placeholders = ['?', '?'];
117121
$tableName = $this->config->getTableName($class->table['name']);
118122

119123
$sql = "INSERT INTO " . $tableName . " (" .
120124
$this->config->getRevisionFieldName() . ", " . $this->config->getRevisionTypeFieldName();
121125

122-
$fields = array();
126+
$fields = [];
123127

124128
// Find associations and copy the data
125129
foreach ($class->associationMappings AS $assoc) {
@@ -178,7 +182,7 @@ private function getRevisionId(ClassMetadata $class, $entityData, $revType): int
178182

179183
// Get identifier info and use it in the select query
180184
$count = 1;
181-
$where = " WHERE ";
185+
$where = ' WHERE ';
182186
foreach ($identifiers as $identifier) {
183187
if ($count > 1) {
184188
$where .= ' AND ';
@@ -198,17 +202,17 @@ private function getRevisionId(ClassMetadata $class, $entityData, $revType): int
198202
if ($result->rowCount() == 1) {
199203
return $result->fetchAssociative()[$this->config->getRevisionFieldName()] + 1;
200204
} elseif ($result->rowCount() > 1) {
201-
throw new \LogicException('Error while selecting new rev number');
205+
throw new LogicException('Error while selecting new rev number');
202206
} else {
203207
return 1;
204208
}
205209
}
206210

207211
private function saveRevisionEntityData(ClassMetadata $class, array $entityData, string $revType): void {
208-
$params = array($this->getRevisionId($class, $entityData, $revType), $revType);
209-
$types = array(\PDO::PARAM_INT, \PDO::PARAM_STR);
212+
$params = [$this->getRevisionId($class, $entityData, $revType), $revType];
213+
$types = [PDO::PARAM_INT, PDO::PARAM_STR];
210214

211-
$fields = array();
215+
$fields = [];
212216

213217
foreach ($class->associationMappings AS $field => $assoc) {
214218
if (($assoc['type'] & ClassMetadata::TO_ONE) > 0 && $assoc['isOwningSide']) {
@@ -224,10 +228,10 @@ private function saveRevisionEntityData(ClassMetadata $class, array $entityData,
224228
$fields[$sourceColumn] = true;
225229
if ($entityData[$field] === NULL) {
226230
$params[] = NULL;
227-
$types[] = \PDO::PARAM_STR;
231+
$types[] = PDO::PARAM_STR;
228232
} else {
229233
$params[] = $relatedId[$targetClass->fieldNames[$targetColumn]];
230-
$types[] = $targetClass->getTypeOfColumn($targetColumn);
234+
$types[] = PersisterHelper::getTypeOfColumn($targetColumn, $targetClass, $this->em);
231235
}
232236
}
233237
}

Reader/HistoryCollection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class HistoryCollection
1414
* order, as otherwise you will have to sort them.
1515
*/
1616
public function addRevision(HistoryRevision $revision): self {
17-
if(!isset($this->revisions[$revision->getEntityId()])){
17+
if (!isset($this->revisions[$revision->getEntityId()])) {
1818
$this->revisions[$revision->getEntityId()] = [];
1919
}
2020

@@ -36,7 +36,7 @@ public function getAllRevisions(): array {
3636
}
3737

3838
public function getRevisionCount($entityId = null): int {
39-
if($entityId === null){
39+
if ($entityId === null) {
4040
$count = 0;
4141
foreach ($this->revisions as $revisions) {
4242
$count += count($revisions);

Reader/HistoryReader.php

+15-17
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use Doctrine\DBAL\Types\Type;
1313
use Doctrine\ORM\EntityManager;
1414
use Doctrine\ORM\Mapping\ClassMetadata;
15-
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1615
use Doctrine\ORM\PersistentCollection;
1716
use Doctrine\ORM\Persisters\Entity\EntityPersister;
17+
use RuntimeException;
1818

1919
/**
2020
* Based on the work of
@@ -80,23 +80,23 @@ public function findRevisionsByCriteria(string $className, array $criteria): His
8080

8181
// Check if the given criteria are indeed available in the object
8282
// and create the actual where query
83-
$whereSql = "";
83+
$whereSql = '';
8484
foreach ($criteria as $criterium => $data) {
8585
if ($whereSql) {
86-
$whereSql .= " AND ";
86+
$whereSql .= ' AND ';
8787
}
8888
if ($metadata->hasField($criterium)) {
89-
$whereSql .= "h." . $metadata->getFieldMapping($criterium)['columnName'] . " = ?";
89+
$whereSql .= 'h.' . $metadata->getFieldMapping($criterium)['columnName'] . ' = ?';
9090
} else if ($metadata->hasAssociation($criterium)) {
91-
$whereSql .= "h." . $metadata->getAssociationMapping($criterium)['joinColumns'][0]['name'] . " = ?";
91+
$whereSql .= 'h.' . $metadata->getAssociationMapping($criterium)['joinColumns'][0]['name'] . ' = ?';
9292
} else {
9393
throw new IncorrectCriteriaException($criterium, $className);
9494
}
9595
}
9696

9797
// Create the query with the where statement
9898
$tableName = $this->config->getTableName($metadata->getTableName());
99-
$query = 'SELECT * FROM ' . $tableName . ' h WHERE ' . $whereSql . " ORDER BY h.id DESC";
99+
$query = 'SELECT * FROM ' . $tableName . ' h WHERE ' . $whereSql . ' ORDER BY h.id DESC';
100100

101101
// Execute query
102102
$revisions = $this->em->getConnection()->fetchAllAssociative($query, array_values($criteria));
@@ -131,7 +131,7 @@ public function restoreObject(string $className, &$dbObject, HistoryRevision $re
131131
if ($oldValue != $newValue) {
132132
$metadata->setFieldValue($dbObject, $associationName, $newValue);
133133
$uow->propertyChanged($dbObject, $associationName, $oldValue, $newValue);
134-
$changeset[$associationName] = array($oldValue, $newValue);
134+
$changeset[$associationName] = [$oldValue, $newValue];
135135
}
136136
}
137137

@@ -143,15 +143,15 @@ public function restoreObject(string $className, &$dbObject, HistoryRevision $re
143143
$oldValue = $metadata->getFieldValue($dbObject, $this->config->getDeletedAtField());
144144
$metadata->setFieldValue($dbObject, $this->config->getDeletedAtField(), null);
145145
$uow->propertyChanged($dbObject, $this->config->getDeletedAtField(), $oldValue, null);
146-
$changeset[$this->config->getDeletedAtField()] = array($oldValue, null);
146+
$changeset[$this->config->getDeletedAtField()] = [$oldValue, null];
147147
}
148148

149149
// Check if there is a deletedBy field configured which we can clear
150150
if (null !== ($deletedByField = $this->config->getDeletedByField())) {
151151
$oldValue = $metadata->getFieldValue($dbObject, $this->config->getDeletedByField());
152152
$metadata->setFieldValue($dbObject, $this->config->getDeletedByField(), null);
153153
$uow->propertyChanged($dbObject, $this->config->getDeletedByField(), $oldValue, null);
154-
$changeset[$this->config->getDeletedByField()] = array($oldValue, null);
154+
$changeset[$this->config->getDeletedByField()] = [$oldValue, null];
155155
}
156156
}
157157

@@ -201,7 +201,6 @@ private function getMetadata(string $className): false|ClassMetadata {
201201
* Simplified and stolen code from UnitOfWork::createEntity.
202202
*/
203203
private function createEntity(string $className, array $data, $revision): object {
204-
/** @var ClassMetadataInfo|ClassMetadata $class */
205204
$class = $this->em->getClassMetadata($className);
206205
//lookup revisioned entity cache
207206
$keyParts = [];
@@ -215,22 +214,22 @@ private function createEntity(string $className, array $data, $revision): object
215214

216215
if (!$class->isInheritanceTypeNone()) {
217216
if (!isset($data[$class->discriminatorColumn['name']])) {
218-
throw new \RuntimeException('Expecting discriminator value in data set.');
217+
throw new RuntimeException('Expecting discriminator value in data set.');
219218
}
220219
$discriminator = $data[$class->discriminatorColumn['name']];
221220
if (!isset($class->discriminatorMap[$discriminator])) {
222-
throw new \RuntimeException("No mapping found for [{$discriminator}].");
221+
throw new RuntimeException("No mapping found for [{$discriminator}].");
223222
}
224223
if ($class->discriminatorValue) {
225224
$entity = $this->em->getClassMetadata($class->discriminatorMap[$discriminator])->newInstance();
226225
} else {
227226
//a complex case when ToOne binding is against AbstractEntity having no discriminator
228-
$pk = array();
227+
$pk = [];
229228
foreach ($class->identifier as $field) {
230229
$pk[$class->getColumnName($field)] = $data[$field];
231230
}
232231
// return $this->find($class->discriminatorMap[$discriminator], $pk, $revision);
233-
throw new \RuntimeException("This is not supported");
232+
throw new RuntimeException("This is not supported");
234233
}
235234
} else {
236235
$entity = $class->newInstance();
@@ -249,9 +248,8 @@ private function createEntity(string $className, array $data, $revision): object
249248
if (isset($hints['fetched'][$className][$field])) {
250249
continue;
251250
}
252-
/** @var ClassMetadataInfo|ClassMetadata $targetClass */
253251
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
254-
if ($assoc['type'] & ClassMetadataInfo::TO_ONE) {
252+
if ($assoc['type'] & ClassMetadata::TO_ONE) {
255253
if ($assoc['isOwningSide']) {
256254
$associatedId = array();
257255
foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) {
@@ -272,7 +270,7 @@ private function createEntity(string $className, array $data, $revision): object
272270
$class->reflFields[$field]->setValue($entity, $this->getEntityPersister($assoc['targetEntity'])
273271
->loadOneToOneEntity($assoc, $entity));
274272
}
275-
} elseif ($assoc['type'] & ClassMetadataInfo::ONE_TO_MANY) {
273+
} elseif ($assoc['type'] & ClassMetadata::ONE_TO_MANY) {
276274
$collection = new PersistentCollection($this->em, $targetClass, new ArrayCollection());
277275
$this->getEntityPersister($assoc['targetEntity'])
278276
->loadOneToManyCollection($assoc, $entity, $collection);

composer.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
],
1414
"require": {
1515
"php": "^8.1",
16-
"doctrine/orm": "^2.16",
17-
"symfony/framework-bundle": "^5.4|^6.4",
18-
"symfony/security-core": "^5.4|^6.4"
16+
"ext-pdo": "*",
17+
"doctrine/orm": "^2.16|^3.0",
18+
"doctrine/dbal": "^3.0|^4.0",
19+
"symfony/framework-bundle": "^5.4|^6.4|^7.0",
20+
"symfony/security-core": "^5.4|^6.4|^7.0"
1921
},
2022
"autoload": {
2123
"psr-4": {

0 commit comments

Comments
 (0)