@@ -61,13 +61,12 @@ class DataVerify
6161 private ValidationContext $ context ;
6262 private FieldCollection $ fields ;
6363 private ErrorCollection $ errors ;
64- private TranslationManager $ translationManager ;
64+ private ? TranslationManager $ translationManager = null ;
6565 private bool $ hasVerified = false ;
6666
6767 // Engines
6868 private ValidationOrchestrator $ orchestrator ;
6969 private ConditionalEngine $ conditionalEngine ;
70- private ErrorManager $ errorManager ;
7170 private DataTraverser $ dataTraverser ;
7271 private LazyValidationRegistry $ lazyRegistry ;
7372
@@ -77,7 +76,6 @@ public function __construct(array|object $data)
7776 $ this ->context = new ValidationContext ();
7877 $ this ->fields = new FieldCollection ();
7978 $ this ->errors = new ErrorCollection ();
80- $ this ->translationManager = new TranslationManager ();
8179 $ this ->lazyRegistry = LazyValidationRegistry::instance ();
8280
8381 $ this ->initializeEngines ();
@@ -87,12 +85,10 @@ private function initializeEngines(): void
8785 {
8886 $ this ->dataTraverser = new DataTraverser ($ this ->data );
8987 $ this ->conditionalEngine = new ConditionalEngine ($ this ->dataTraverser );
90- $ this ->errorManager = new ErrorManager ($ this ->errors , $ this ->translationManager , $ this ->lazyRegistry );
9188
9289 $ this ->orchestrator = new ValidationOrchestrator (
9390 $ this ->fields ,
9491 $ this ->errors ,
95- $ this ->errorManager ,
9692 $ this ->dataTraverser ,
9793 $ this ->conditionalEngine ,
9894 new ValidationRegistry (),
@@ -261,27 +257,44 @@ public function alias(string $name): self
261257 return $ this ;
262258 }
263259
260+ /**
261+ * Get or create translation manager lazily
262+ */
263+ private function getTranslationManager (): TranslationManager
264+ {
265+ if ($ this ->translationManager === null ) {
266+ $ this ->translationManager = new TranslationManager ();
267+ }
268+
269+ // Injecter dans l'ErrorManager de l'Orchestrator (qui pourrait être lazy aussi)
270+ // On crée l'ErrorManager maintenant si on configure les traductions
271+ $ errorManager = new ErrorManager ($ this ->errors , $ this ->lazyRegistry , $ this ->translationManager );
272+ $ this ->orchestrator ->setErrorManager ($ errorManager );
273+
274+ return $ this ->translationManager ;
275+ }
276+
264277 public function setTranslator (TranslatorInterface $ translator ): self
265278 {
266- $ this ->translationManager ->setTranslator ($ translator );
279+ $ this ->getTranslationManager () ->setTranslator ($ translator );
267280 return $ this ;
268281 }
269282
270283 public function setLocale (string $ locale ): self
271284 {
272- $ this ->translationManager ->setLocale ($ locale );
285+ $ this ->getTranslationManager () ->setLocale ($ locale );
273286 return $ this ;
274287 }
275288
276289 public function addTranslations (array $ translations , string $ locale = 'en ' ): self
277290 {
278- $ this ->translationManager ->addTranslations ($ translations , $ locale );
291+ $ this ->getTranslationManager () ->addTranslations ($ translations , $ locale );
279292 return $ this ;
280293 }
281294
282295 public function loadLocale (string $ locale , ?string $ filePath = null ): self
283296 {
284- $ this ->translationManager ->loadLocale ($ locale , $ filePath );
297+ $ this ->getTranslationManager () ->loadLocale ($ locale , $ filePath );
285298 return $ this ;
286299 }
287300
@@ -423,4 +436,4 @@ public static function enableIdeHelper(?string $outputPath = null): void
423436 {
424437 \Gravity \Documentation \IdeHelperManager::instance ()->enable ($ outputPath );
425438 }
426- }
439+ }
0 commit comments