10000 [Bridge/Doctrine] Reset the EM lazy-proxy instead of the EM service · symfony/symfony@358a6bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 358a6bf

Browse files
[Bridge/Doctrine] Reset the EM lazy-proxy instead of the EM service
1 parent 76791fe commit 358a6bf

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/Symfony/Bridge/Doctrine/ManagerRegistry.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bridge\Doctrine;
1313

14+
use ProxyManager\Proxy\LazyLoadingInterface;
15+
use Symfony\Component\DependencyInjection\Container;
1416
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1517
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1618
use Doctrine\Common\Persistence\AbstractManagerRegistry;
@@ -37,6 +39,32 @@ protected function getService($name)
3739
*/
3840
protected function resetService($name)
3941
{
40-
$this->container->set($name, null);
42+
if (!$this->container->initialized($name)) {
43+
return;
44+
}
45+
$manager = $this->container->get($name);
46+
47+
if (!$manager instanceof LazyLoadingInterface) {
48+
@trigger_error(sprintf('Resetting a non-lazy manager service is deprecated since Symfony 3.2 and will thrown an exception in version 4.0. Set the "%s" service as lazy and require "symfony/proxy-manager-bridge" in your composer.json file instead.', $name));
49+
50+
$this->container->set($name, null);
51+
52+
return;
53+
}
54+
$manager->setProxyInitializer(\Closure::bind(
55+
function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
56+
if (isset($this->aliases[$name = strtolower($name)])) {
57+
$name = $this->aliases[$name];
58+
}
59+
$method = 'get'.strtr($name, $this->underscoreMap).'Service';
60+
$wrappedInstance = $this->{$method}(false);
61+
62+
$manager->setProxyInitializer(null);
63+
64+
return true;
65+
},
66+
$this->container,
67+
Container::class
68+
));
4169
}
4270
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests;
13+
14+
use Symfony\Bridge\Doctrine\ManagerRegistry;
15+
use Symfony\Bridge\ProxyManager\Tests\LazyProxy\Dumper\PhpDumperTest;
16+
17+
class ManagerRegistryTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public static function setUpBeforeClass()
20+
{
21+
$test = new PhpDumperTest();
22+
$test->testDumpContainerWithProxyServiceWillShareProxies();
23+
}
24+
25+
public function testLegacyResetService()
26+
{
27+
$container = new \LazyServiceProjectServiceContainer();
28+
29+
$registry = new TestManagerRegistry('name', array(), array('defaultManager' => 'foo'), 'defaultConnection', 'defaultManager', 'proxyInterfaceName');
30+
$registry->setContainer($container);
31+
32+
$foo = $container->get('foo');
33+
$foo->bar = 123;
34+
$this->assertTrue(isset($foo->bar));
35+
36+
$registry->resetManager();
37+
38+
$this->assertSame($foo, $container->get('foo'));
39+
$this->assertFalse(isset($foo->bar));
40+
}
41+
}
42+
43+
class TestManagerRegistry extends ManagerRegistry
44+
{
45+
public function getAliasNamespace($alias)
46+
{
47+
return 'Foo';
48+
}
49+
}

src/Symfony/Bridge/Doctrine/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"symfony/http-kernel": "~2.8|~3.0",
2828
"symfony/property-access": "~2.8|~3.0",
2929
"symfony/property-info": "~2.8|3.0",
30+
"symfony/proxy-manager-bridge": "~2.8|~3.0",
3031
"symfony/security": "~2.8|~3.0",
3132
"symfony/expression-language": "~2.8|~3.0",
3233
"symfony/validator": "~2.8|~3.0",

0 commit comments

Comments
 (0)
0