55use Doctrine \Common \Annotations \Reader ;
66use Doctrine \ORM \EntityRepository ;
77use Doctrine \ORM \Event \LifecycleEventArgs ;
8+ use Symfony \Component \DependencyInjection \ContainerInterface ;
89use Vivait \StringGeneratorBundle \Annotation \GeneratorAnnotation ;
910use Vivait \StringGeneratorBundle \Model \GeneratorInterface ;
1011use Vivait \StringGeneratorBundle \Registry \Registry ;
@@ -23,21 +24,32 @@ class GeneratorListener
2324 * @var GeneratorInterface
2425 */
2526 private $ generator ;
27+
2628 /**
2729 * @var Registry
2830 */
2931 private $ registry ;
3032
33+ private $ container ;
34+
3135 /**
36+ * Registry has been made nullable as injecting it causes circular references. Instead, the container is injected
37+ * via a setter, and the registry is fetched from there instead.
38+ *
3239 * @param Reader $reader
3340 * @param Registry $registry
3441 */
35- public function __construct (Reader $ reader , Registry $ registry )
42+ public function __construct (Reader $ reader , Registry $ registry = null )
3643 {
3744 $ this ->reader = $ reader ;
3845 $ this ->registry = $ registry ;
3946 }
4047
48+ public function setContainer (ContainerInterface $ container )
49+ {
50+ $ this ->container = $ container ;
51+ }
52+
4153 /**
4254 * @param LifecycleEventArgs $args
4355 */
@@ -75,7 +87,7 @@ public function prePersist(LifecycleEventArgs $args)
7587 private function generateString ($ property , GeneratorAnnotation $ annotation , $ object )
7688 {
7789 /** @var GeneratorInterface $generator */
78- $ generator = $ this ->registry ->get ($ annotation ->generator );
90+ $ generator = $ this ->getRegistry () ->get ($ annotation ->generator );
7991 $ generator ->setLength ($ annotation ->length );
8092
8193 if (!empty ($ annotation ->callbacks )){
@@ -129,4 +141,15 @@ private function isMethod($class, $callback)
129141 {
130142 return method_exists ($ class , $ callback ) && is_callable ([$ class , $ callback ]);
131143 }
144+
145+ /**
146+ * @return Registry
147+ */
148+ private function getRegistry ()
149+ {
150+ if ($ this ->registry ){
151+ return $ this ->registry ;
152+ }
153+ return $ this ->container ->get ('vivait_generator.registry ' );
154+ }
132155}
0 commit comments