Skip to content

Commit d4c0f7c

Browse files
Registry improve manager rebinding
1 parent 8d49769 commit d4c0f7c

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/DoctrineServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ protected function registerManagerRegistry()
117117
// Add all managers into the registry
118118
foreach ($app->make('config')->get('doctrine.managers', []) as $manager => $settings) {
119119
$registry->addManager($manager, $settings);
120-
$registry->addConnection($manager);
121120
}
122121

123122
return $registry;

src/IlluminateRegistry.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,25 @@ public function __construct(Container $container, EntityManagerFactory $factory)
7777
*/
7878
public function addManager($manager, array $settings = [])
7979
{
80-
if (!$this->container->bound($this->getManagerBindingName($manager))) {
81-
$this->container->singleton($this->getManagerBindingName($manager), function () use ($settings) {
82-
return $this->factory->create($settings);
83-
});
80+
$this->container->singleton($this->getManagerBindingName($manager), function () use ($settings) {
81+
return $this->factory->create($settings);
82+
});
8483

85-
$this->managers[$manager] = $manager;
86-
}
84+
$this->addConnection($manager);
85+
86+
$this->managers[$manager] = $manager;
8787
}
8888

8989
/**
9090
* @param $connection
9191
*/
9292
public function addConnection($connection)
9393
{
94-
if (!$this->container->bound($this->getConnectionBindingName($connection))) {
95-
$this->container->singleton($this->getConnectionBindingName($connection), function () use ($connection) {
96-
return $this->getManager($connection)->getConnection();
97-
});
94+
$this->container->singleton($this->getConnectionBindingName($connection), function () use ($connection) {
95+
return $this->getManager($connection)->getConnection();
96+
});
9897

99-
$this->connections[$connection] = $connection;
100-
}
98+
$this->connections[$connection] = $connection;
10199
}
102100

103101
/**
@@ -275,7 +273,12 @@ public function resetManager($name = null)
275273
$this->getManagerBindingName($this->managers[$name])
276274
);
277275

276+
$this->resetService(
277+
$this->getConnectionBindingName($this->connections[$name])
278+
);
279+
278280
unset($this->managersMap[$name]);
281+
unset($this->connectionsMap[$name]);
279282
}
280283

281284
/**

tests/IlluminateRegistryTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class IlluminateRegistryTest extends PHPUnit_Framework_TestCase
2929
protected function setUp()
3030
{
3131
$this->container = m::mock(Container::class);
32-
$this->container->shouldReceive('bound')->andReturn(false);
3332
$this->factory = m::mock(EntityManagerFactory::class);
3433

3534
$this->registry = new IlluminateRegistry(
@@ -40,7 +39,7 @@ protected function setUp()
4039

4140
public function test_can_add_manager()
4241
{
43-
$this->container->shouldReceive('singleton')->once();
42+
$this->container->shouldReceive('singleton')->twice();
4443
$this->registry->addManager('default', ['settings']);
4544

4645
$this->assertTrue($this->registry->managerExists('default'));
@@ -70,7 +69,7 @@ public function test_get_default_connection_name()
7069
public function test_get_default_manager_name()
7170
{
7271
// Will return first, when no default name
73-
$this->container->shouldReceive('singleton')->once();
72+
$this->container->shouldReceive('singleton')->times(3);
7473
$this->registry->addManager('custom');
7574
$this->assertEquals('custom', $this->registry->getDefaultManagerName());
7675

@@ -178,7 +177,7 @@ public function test_can_get_all_connections()
178177

179178
public function test_can_get_default_manager()
180179
{
181-
$this->container->shouldReceive('singleton')->once();
180+
$this->container->shouldReceive('singleton')->times(2);
182181
$this->registry->addManager('default');
183182

184183
$this->container->shouldReceive('make')
@@ -191,7 +190,7 @@ public function test_can_get_default_manager()
191190

192191
public function test_can_get_custom_manager()
193192
{
194-
$this->container->shouldReceive('singleton')->once();
193+
$this->container->shouldReceive('singleton')->times(2);
195194
$this->registry->addManager('custom');
196195

197196
$this->container->shouldReceive('make')
@@ -213,7 +212,7 @@ public function test_cannot_non_existing_manager()
213212

214213
public function test_manager_gets_only_resolved_once()
215214
{
216-
$this->container->shouldReceive('singleton')->once();
215+
$this->container->shouldReceive('singleton')->times(2);
217216
$this->registry->addManager('default');
218217

219218
$this->container->shouldReceive('make')
@@ -231,7 +230,7 @@ public function test_manager_gets_only_resolved_once()
231230

232231
public function test_can_check_if_manager_exists()
233232
{
234-
$this->container->shouldReceive('singleton')->once();
233+
$this->container->shouldReceive('singleton')->times(2);
235234
$this->registry->addManager('default');
236235

237236
$this->assertFalse($this->registry->managerExists('non-existing'));
@@ -240,7 +239,7 @@ public function test_can_check_if_manager_exists()
240239

241240
public function test_can_get_manager_names()
242241
{
243-
$this->container->shouldReceive('singleton')->twice();
242+
$this->container->shouldReceive('singleton')->times(4);
244243

245244
$this->registry->addManager('default');
246245
$this->registry->addManager('custom');
@@ -252,7 +251,7 @@ public function test_can_get_manager_names()
252251

253252
public function test_can_get_all_managers()
254253
{
255-
$this->container->shouldReceive('singleton')->twice();
254+
$this->container->shouldReceive('singleton')->times(4);
256255

257256
$this->container->shouldReceive('make')
258257
->with('doctrine.managers.default')
@@ -302,7 +301,7 @@ public function test_cannot_reset_non_existing_managers()
302301
$this->registry->resetManager('non-existing');
303302
}
304303

305-
public function test_get_alias_namespace_from_unkown_namespace()
304+
public function test_get_alias_namespace_from_unknown_namespace()
306305
{
307306
$this->setExpectedException(
308307
ORMException::class,

0 commit comments

Comments
 (0)