66use Nette \Caching \Cache ;
77use Nette \DI \CompilerExtension ;
88use Nette \DI \ContainerBuilder ;
9+ use Nette \DI \Definitions \Statement ;
910use Nette \Schema \Expect ;
1011use Nette \Schema \Schema ;
1112use Nextras \Dbal \IConnection ;
1213use Nextras \Orm \Entity \IEntity ;
13- use Nextras \Orm \Entity \Reflection \IMetadataParserFactory ;
14- use Nextras \Orm \Entity \Reflection \MetadataParser ;
14+ use Nextras \Orm \Entity \Reflection \MetadataParserFactory ;
1515use Nextras \Orm \Exception \InvalidStateException ;
16+ use Nextras \Orm \Extension ;
1617use Nextras \Orm \Mapper \Dbal \DbalMapperCoordinator ;
1718use Nextras \Orm \Model \IModel ;
1819use Nextras \Orm \Model \MetadataStorage ;
@@ -39,6 +40,7 @@ public function getConfigSchema(): Schema
3940 {
4041 return Expect::structure ([
4142 'model ' => Expect::string ()->default (Model::class),
43+ 'extensions ' => Expect::arrayOf ('string|Nette\DI\Definitions\Statement ' )->default ([]),
4244 'repositoryFinder ' => Expect::string ()->default (PhpDocRepositoryFinder::class),
4345 'initializeMetadata ' => Expect::bool ()->default (false ),
4446 'autowiredInternalServices ' => Expect::bool ()->default (true ),
@@ -52,23 +54,28 @@ public function loadConfiguration(): void
5254 $ this ->builder = $ this ->getContainerBuilder ();
5355 $ this ->modelClass = $ this ->config ->model ;
5456
57+ $ extensions = [];
58+ foreach ($ this ->config ->extensions as $ extension ) {
59+ $ extensions [] = is_string ($ extension ) ? new Statement ($ extension ) : $ extension ;
60+ }
61+
5562 $ repositoryFinderClass = $ this ->config ->repositoryFinder ;
5663 if (!is_subclass_of ($ repositoryFinderClass , IRepositoryFinder::class)) {
5764 throw new InvalidStateException ('Repository finder does not implement Nextras\Orm\Bridges\NetteDI\IRepositoryFinder interface. ' );
5865 }
59- $ this ->repositoryFinder = new $ repositoryFinderClass ($ this ->modelClass , $ this ->builder , $ this );
66+ $ this ->repositoryFinder = new $ repositoryFinderClass ($ this ->modelClass , $ extensions , $ this ->builder , $ this );
6067
6168 $ repositories = $ this ->repositoryFinder ->loadConfiguration ();
6269
6370 $ this ->setupCache ();
6471 $ this ->setupDependencyProvider ();
6572 $ this ->setupDbalMapperDependencies ();
66- $ this ->setupMetadataParserFactory ();
73+ $ this ->setupMetadataParserFactory ($ extensions );
6774
6875 if ($ repositories !== null ) {
6976 $ repositoriesConfig = Model::getConfiguration ($ repositories );
7077 $ this ->setupMetadataStorage ($ repositoriesConfig [2 ]);
71- $ this ->setupModel ($ this ->modelClass , $ repositoriesConfig );
78+ $ this ->setupModel ($ this ->modelClass , $ repositoriesConfig, $ extensions );
7279 }
7380
7481 $ this ->initializeMetadata ($ this ->config ->initializeMetadata );
@@ -80,9 +87,14 @@ public function beforeCompile(): void
8087 $ repositories = $ this ->repositoryFinder ->beforeCompile ();
8188
8289 if ($ repositories !== null ) {
90+ $ extensions = [];
91+ foreach ($ this ->config ->extensions as $ extension ) {
92+ $ extensions [] = is_string ($ extension ) ? new Statement ($ extension ) : $ extension ;
93+ }
94+
8395 $ repositoriesConfig = Model::getConfiguration ($ repositories );
8496 $ this ->setupMetadataStorage ($ repositoriesConfig [2 ]);
85- $ this ->setupModel ($ this ->modelClass , $ repositoriesConfig );
97+ $ this ->setupModel ($ this ->modelClass , $ repositoriesConfig, $ extensions );
8698 }
8799
88100 $ this ->setupDbalMapperDependencies ();
@@ -138,18 +150,19 @@ protected function setupDbalMapperDependencies(): void
138150 }
139151
140152
141- protected function setupMetadataParserFactory (): void
153+ /**
154+ * @param list<Extension> $extensions
155+ */
156+ protected function setupMetadataParserFactory (array $ extensions ): void
142157 {
143158 $ factoryName = $ this ->prefix ('metadataParserFactory ' );
144159 if ($ this ->builder ->hasDefinition ($ factoryName )) {
145160 return ;
146161 }
147162
148- $ this ->builder ->addFactoryDefinition ($ factoryName )
149- ->setImplement (IMetadataParserFactory::class)
150- ->getResultDefinition ()
151- ->setType (MetadataParser::class)
152- ->setArguments (['$entityClassesMap ' ])
163+ $ this ->builder ->addDefinition ($ factoryName )
164+ ->setType (MetadataParserFactory::class)
165+ ->setArgument ('extensions ' , $ extensions )
153166 ->setAutowired ($ this ->config ->autowiredInternalServices );
154167 }
155168
@@ -182,8 +195,9 @@ protected function setupMetadataStorage(array $entityClassMap): void
182195 * array<string, class-string<IRepository<IEntity>>>,
183196 * array<class-string<IEntity>, class-string<IRepository<IEntity>>>
184197 * } $repositoriesConfig
198+ * @param list<Statement> $extensions
185199 */
186- protected function setupModel (string $ modelClass , array $ repositoriesConfig ): void
200+ protected function setupModel (string $ modelClass , array $ repositoriesConfig, array $ extensions ): void
187201 {
188202 $ modelName = $ this ->prefix ('model ' );
189203 if ($ this ->builder ->hasDefinition ($ modelName )) {
@@ -196,7 +210,8 @@ protected function setupModel(string $modelClass, array $repositoriesConfig): vo
196210 'configuration ' => $ repositoriesConfig ,
197211 'repositoryLoader ' => $ this ->prefix ('@repositoryLoader ' ),
198212 'metadataStorage ' => $ this ->prefix ('@metadataStorage ' ),
199- ]);
213+ ])
214+ ->addSetup ('foreach (? as $e) { $e->configureModel($service); } ' , [$ extensions ]);
200215 }
201216
202217
0 commit comments