Skip to content

Commit

Permalink
Merge pull request #606 from mabar/initialize-metadata-main
Browse files Browse the repository at this point in the history
[Nette] Initialize metadata on DIC startup - main
  • Loading branch information
hrach authored Feb 8, 2023
2 parents 94d1d44 + 675b276 commit 8c019e6
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/Bridges/NetteDI/OrmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nette\Caching\Cache;
use Nette\DI\CompilerExtension;
use Nette\DI\ContainerBuilder;
use Nette\PhpGenerator\ClassType;
use Nextras\Dbal\IConnection;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\Reflection\IMetadataParserFactory;
Expand All @@ -30,16 +31,19 @@ class OrmExtension extends CompilerExtension
/** @var string */
protected $modelClass;

/** @var array<mixed> */
private $configDefaults = [
'model' => Model::class,
'repositoryFinder' => PhpDocRepositoryFinder::class,
'initializeMetadata' => false,
];


public function loadConfiguration()
{
$this->builder = $this->getContainerBuilder();

$configDefaults = [
'model' => Model::class,
'repositoryFinder' => PhpDocRepositoryFinder::class,
];
$config = $this->validateConfig($configDefaults); // @phpstan-ignore-line
$config = $this->validateConfig($this->configDefaults); // @phpstan-ignore-line
$this->modelClass = $config['model'];

$repositoryFinderClass = $config['repositoryFinder'];
Expand Down Expand Up @@ -76,6 +80,12 @@ public function beforeCompile()
$this->setupDbalMapperDependencies();
}

public function afterCompile(ClassType $class)
{
$config = $this->validateConfig($this->configDefaults); // @phpstan-ignore-line

$this->initializeMetadata($class, $config['initializeMetadata']);
}

protected function setupCache(): void
{
Expand Down Expand Up @@ -185,4 +195,16 @@ protected function setupModel(string $modelClass, array $repositoriesConfig): vo
'metadataStorage' => $this->prefix('@metadataStorage'),
]);
}

protected function initializeMetadata(ClassType $classType, bool $init): void
{
if (!$init) {
return;
}

$initialize = $classType->getMethod('initialize');
$initialize->addBody('$this->getService(?);', [
$this->prefix('metadataStorage'),
]);
}
}

0 comments on commit 8c019e6

Please sign in to comment.