Skip to content

Commit 3cf93f0

Browse files
robwasrippedmartin-georgiev
authored andcommitted
Added event subscriber to clear entity cache on doctrine clear (martin-georgiev#8)
1 parent f7a5954 commit 3cf93f0

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

src/SimpleThings/EntityAudit/AuditManager.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ class AuditManager
1717

1818
private $metadataFactory;
1919

20+
private $entityCache;
21+
2022
/**
2123
* @param AuditConfiguration $config
2224
*/
23-
public function __construct(AuditConfiguration $config)
25+
public function __construct(AuditConfiguration $config, EntityCache $entityCache)
2426
{
2527
$this->config = $config;
2628
$this->metadataFactory = $config->createMetadataFactory();
29+
$this->entityCache = $entityCache;
2730
}
2831

2932
public function getMetadataFactory()
@@ -38,7 +41,7 @@ public function getConfiguration()
3841

3942
public function createAuditReader(EntityManager $em)
4043
{
41-
return new AuditReader($em, $this->config, $this->metadataFactory);
44+
return new AuditReader($em, $this->config, $this->metadataFactory, $this->entityCache);
4245
}
4346

4447
public function registerEvents(EventManager $evm)

src/SimpleThings/EntityAudit/AuditReader.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AuditReader
6969

7070
/**
7171
* Entity cache to prevent circular references
72-
* @var array
72+
* @var EntityCache
7373
*/
7474
private $entityCache;
7575

@@ -166,11 +166,12 @@ public function setLoadNativeEntities($loadNativeEntities)
166166
* @param AuditConfiguration $config
167167
* @param MetadataFactory $factory
168168
*/
169-
public function __construct(EntityManagerInterface $em, AuditConfiguration $config, MetadataFactory $factory)
169+
public function __construct(EntityManagerInterface $em, AuditConfiguration $config, MetadataFactory $factory, EntityCache $entityCache)
170170
{
171171
$this->em = $em;
172172
$this->config = $config;
173173
$this->metadataFactory = $factory;
174+
$this->entityCache = $entityCache;
174175
$this->platform = $this->em->getConnection()->getDatabasePlatform();
175176
$this->quoteStrategy = $this->em->getConfiguration()->getQuoteStrategy();
176177
}
@@ -196,7 +197,7 @@ public function getConfiguration()
196197
*/
197198
public function clearEntityCache()
198199
{
199-
$this->entityCache = array();
200+
$this->entityCache->clear();
200201
}
201202

202203
/**
@@ -363,11 +364,8 @@ private function createEntity($className, array $columnMap, array $data, $revisi
363364

364365
$key = implode(':', $keyParts);
365366

366-
if (isset($this->entityCache[$className]) &&
367-
isset($this->entityCache[$className][$key]) &&
368-
isset($this->entityCache[$className][$key][$revision])
369-
) {
370-
return $this->entityCache[$className][$key][$revision];
367+
if ($this->entityCache->hasEntity($className, $key, $revision)) {
368+
return $this->entityCache->getEntity($className, $key, $revision);
371369
}
372370

373371
if (!$class->isInheritanceTypeNone()) {
@@ -396,7 +394,7 @@ private function createEntity($className, array $columnMap, array $data, $revisi
396394
}
397395

398396
//cache the entity to prevent circular references
399-
$this->entityCache[$className][$key][$revision] = $entity;
397+
$this->entityCache->addEntity($className, $key, $revision, $entity);
400398

401399
foreach ($data as $field => $value) {
402400
if (isset($class->fieldMappings[$field])) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleThings\EntityAudit;
6+
7+
class EntityCache
8+
{
9+
private $entities = [];
10+
11+
public function clear(): void
12+
{
13+
$this->entities = [];
14+
}
15+
16+
public function hasEntity(string $className, string $key, string $revision): bool
17+
{
18+
return isset($this->entities[$className])
19+
&& isset($this->entities[$className][$key])
20+
&& isset($this->entities[$className][$key][$revision]);
21+
}
22+
23+
public function addEntity(string $className, string $key, string $revision, $entity): void
24+
{
25+
$this->entities[$className][$key][$revision] = $entity;
26+
}
27+
28+
public function getEntity(string $className, string $key, string $revision)
29+
{
30+
if($this->hasEntity($className, $key, $revision)) {
31+
return $this->entities[$className][$key][$revision];
32+
}
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
5+
namespace SimpleThings\EntityAudit\EventListener;
6+
7+
use Doctrine\Common\EventSubscriber;
8+
use Doctrine\ORM\Events;
9+
use SimpleThings\EntityAudit\EntityCache;
10+
11+
class CacheListener implements EventSubscriber
12+
{
13+
private $entityCache;
14+
15+
public function __construct(EntityCache $entityCache)
16+
{
17+
$this->entityCache = $entityCache;
18+
}
19+
20+
public function getSubscribedEvents(): array
21+
{
22+
return [
23+
Events::onClear,
24+
];
25+
}
26+
27+
public function onClear(): void
28+
{
29+
$this->entityCache->clear();
30+
}
31+
}

src/SimpleThings/EntityAudit/Resources/config/auditable.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<services>
1919
<service id="simplethings_entityaudit.manager" class="SimpleThings\EntityAudit\AuditManager" public="true">
2020
<argument id="simplethings_entityaudit.config" type="service" />
21+
<argument id="SimpleThings\EntityAudit\EntityCache" type="service" />
2122
</service>
2223

2324
<service id="SimpleThings\EntityAudit\AuditManager" alias="simplethings_entityaudit.manager" public="true" />
@@ -29,6 +30,8 @@
2930

3031
<service id="SimpleThings\EntityAudit\AuditReader" alias="simplethings_entityaudit.reader" />
3132

33+
<service id="SimpleThings\EntityAudit\EntityCache" class="SimpleThings\EntityAudit\EntityCache" public="true" />
34+
3235
<service id="simplethings_entityaudit.log_revisions_listener" class="SimpleThings\EntityAudit\EventListener\LogRevisionsListener">
3336
<argument id="simplethings_entityaudit.manager" type="service" />
3437
<tag name="doctrine.event_subscriber" connection="default" />
@@ -39,6 +42,11 @@
3942
<tag name="doctrine.event_subscriber" connection="default" />
4043
</service>
4144

45+
<service id="simplethings_entityaudit.cache_listener" class="SimpleThings\EntityAudit\EventListener\CacheListener">
46+
<argument id="SimpleThings\EntityAudit\EntityCache" type="service" />
47+
<tag name="doctrine.event_subscriber" connection="default" />
48+
</service>
49+
4250
<service id="simplethings_entityaudit.username_callable.token_storage" class="SimpleThings\EntityAudit\User\TokenStorageUsernameCallable">
4351
<argument id="service_container" type="service" />
4452
</service>

tests/SimpleThings/Tests/EntityAudit/BaseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Gedmo;
3535
use SimpleThings\EntityAudit\AuditConfiguration;
3636
use SimpleThings\EntityAudit\AuditManager;
37+
use SimpleThings\EntityAudit\EntityCache;
3738

3839
abstract class BaseTest extends \PHPUnit_Framework_TestCase
3940
{
@@ -205,7 +206,7 @@ protected function getAuditManager()
205206
return 'beberlei';
206207
});
207208

208-
$auditManager = new AuditManager($auditConfig);
209+
$auditManager = new AuditManager($auditConfig, new EntityCache());
209210
$auditManager->registerEvents($this->_getConnection()->getEventManager());
210211

211212
return $this->auditManager = $auditManager;

0 commit comments

Comments
 (0)