2121/**
2222 * Import functions
2323 */
24- use function DI \factory ;
2524use function key ;
25+ use function reset ;
2626use function sprintf ;
2727use function sys_get_temp_dir ;
2828
@@ -37,6 +37,16 @@ class ManagerRegistry extends AbstractManagerRegistry
3737 */
3838 private $ container ;
3939
40+ /**
41+ * @var \Closure[]
42+ */
43+ private $ factories = [];
44+
45+ /**
46+ * @var object[]
47+ */
48+ private $ services = [];
49+
4050 /**
4151 * Constructor of the class
4252 *
@@ -51,23 +61,29 @@ public function __construct(Container $container)
5161 $ managers = [];
5262 $ types = [];
5363
54- foreach ($ configuration as $ name => $ params ) {
55- $ connections [$ name ] = sprintf ('doctrine.connection.%s ' , $ name );
56- $ managers [$ name ] = sprintf ('doctrine.manager.%s ' , $ name );
64+ foreach ($ configuration as $ serviceName => $ params ) {
65+ $ connectionName = sprintf ('connection:%s ' , $ serviceName );
66+ $ managerName = sprintf ('manager:%s ' , $ serviceName );
67+
68+ $ connections [$ serviceName ] = $ connectionName ;
69+ $ managers [$ serviceName ] = $ managerName ;
5770
58- $ this ->container -> set ( $ connections [ $ name ], factory ( function ($ name ) {
59- return $ this ->getManager ($ name )->getConnection ();
60- })-> parameter ( ' name ' , $ name )) ;
71+ $ this ->factories [ $ connectionName ] = function () use ( $ serviceName ) {
72+ return $ this ->getManager ($ serviceName )->getConnection ();
73+ };
6174
62- $ this ->container -> set ( $ managers [ $ name ], factory ( function ($ params ) {
75+ $ this ->factories [ $ managerName ] = function () use ($ params ) {
6376 return $ this ->createManager ($ params );
64- })-> parameter ( ' params ' , $ params )) ;
77+ };
6578
6679 if (!empty ($ params ['types ' ])) {
6780 $ types += $ params ['types ' ];
6881 }
6982 }
7083
84+ reset ($ connections );
85+ reset ($ managers );
86+
7187 parent ::__construct ('ORM ' , $ connections , $ managers , key ($ connections ), key ($ managers ), Proxy::class);
7288
7389 $ this ->registerUserTypes ($ types );
@@ -161,18 +177,32 @@ public function getHydrator(string $name = null) : ArrayHydrator
161177
162178 /**
163179 * {@inheritDoc}
180+ *
181+ * @throws \RuntimeException If the registry doesn't contain the named service.
164182 */
165183 protected function getService ($ name )
166184 {
167- return $ this ->container ->get ($ name );
185+ if (isset ($ this ->services [$ name ])) {
186+ return $ this ->services [$ name ];
187+ }
188+
189+ if (!isset ($ this ->factories [$ name ])) {
190+ throw new \RuntimeException (
191+ sprintf ('Doctrine Manager registry does not contain named service "%s" ' , $ name )
192+ );
193+ }
194+
195+ $ this ->services [$ name ] = $ this ->factories [$ name ]();
196+
197+ return $ this ->services [$ name ];
168198 }
169199
170200 /**
171201 * {@inheritDoc}
172202 */
173203 protected function resetService ($ name )
174204 {
175- $ this -> container -> set ( $ name , $ this ->container -> make ( $ name) );
205+ unset( $ this ->services [ $ name] );
176206 }
177207
178208 /**
0 commit comments