Skip to content

Commit

Permalink
Annotations lazy loading (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper authored Feb 27, 2021
1 parent 9c47c9e commit 297c37a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
30 changes: 27 additions & 3 deletions src/Provider/Doctrine/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
namespace DH\Auditor\Provider\Doctrine;

use DH\Auditor\Provider\ConfigurationInterface;
use DH\Auditor\Provider\Doctrine\Service\AuditingService;
use Symfony\Component\OptionsResolver\OptionsResolver;

class Configuration implements ConfigurationInterface
{
/**
* @var DoctrineProvider
*/
private $provider;

/**
* @var string
*/
Expand All @@ -23,9 +29,9 @@ class Configuration implements ConfigurationInterface
private $ignoredColumns;

/**
* @var array
* @var null|array
*/
private $entities = [];
private $entities;

/**
* @var array
Expand Down Expand Up @@ -173,7 +179,15 @@ public function getIgnoredColumns(): array
*/
public function getEntities(): array
{
return $this->entities;
if (null === $this->entities && null !== $this->provider) {
/** @var AuditingService[] $auditingServices */
$auditingServices = $this->provider->getAuditingServices();
foreach ($auditingServices as $auditingService) {
$this->provider->loadAnnotations($auditingService->getEntityManager(), []);
}
}

return null === $this->entities ? [] : $this->entities;
}

/**
Expand Down Expand Up @@ -219,4 +233,14 @@ public function getStorageMapper(): ?callable
{
return $this->storageMapper;
}

public function getProvider(): DoctrineProvider
{
return $this->provider;
}

public function setProvider(DoctrineProvider $provider): void
{
$this->provider = $provider;
}
}
9 changes: 5 additions & 4 deletions src/Provider/Doctrine/DoctrineProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function __construct(ConfigurationInterface $configuration)
{
$this->configuration = $configuration;
$this->transactionManager = new TransactionManager($this);

\assert($this->configuration instanceof Configuration); // helps PHPStan
$this->configuration->setProvider($this);
}

public function registerAuditingService(AuditingServiceInterface $service): ProviderInterface
Expand All @@ -45,8 +48,6 @@ public function registerAuditingService(AuditingServiceInterface $service): Prov
$evm->addEventSubscriber(new CreateSchemaListener($this));
$evm->addEventSubscriber(new DoctrineSubscriber($this->transactionManager));

$this->loadAnnotations($entityManager);

return $this;
}

Expand Down Expand Up @@ -233,12 +234,12 @@ public function setStorageMapper(callable $storageMapper): void
$this->configuration->setStorageMapper($storageMapper);
}

private function loadAnnotations(EntityManagerInterface $entityManager): self
public function loadAnnotations(EntityManagerInterface $entityManager, ?array $entities = null): self
{
\assert($this->configuration instanceof Configuration); // helps PHPStan
$annotationLoader = new AnnotationLoader($entityManager);
$this->configuration->setEntities(array_merge(
$this->configuration->getEntities(),
null === $entities ? $this->configuration->getEntities() : $entities,
$annotationLoader->load()
));

Expand Down

0 comments on commit 297c37a

Please sign in to comment.