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

Skip to content

Commit 7444ccc

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

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/Symfony/Bridge/Doctrine/ManagerRegistry.php

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

1212
namespace Symfony\Bridge\Doctrine;
1313

14+
use ProxyManager\Proxy\LazyLoadingInterface;
15+
use ProxyManager\Proxy\ValueHolderInterface;
16+
use Symfony\Component\DependencyInjection\Container;
1417
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1518
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1619
use Doctrine\Common\Persistence\AbstractManagerRegistry;
@@ -37,6 +40,32 @@ protected function getService($name)
3740
*/
3841
protected function resetService($name)
3942
{
40-
$this->container->set($name, null);
43+
if (!$this->container->initialized($name)) {
44+
return;
45+
}
46+
$manager = $this->container->get($name);
47+
48+
if (!$manager instanceof LazyLoadingInterface) {
49+
@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));
50+
51+
$this->container->set($name, null);
52+
53+
return;
54+
}
55+
$manager->setProxyInitializer(\Closure::bind(
56+
function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
57+
if (isset($this->aliases[$name = strtolower($name)])) {
58+
$name = $this->aliases[$name];
59+
}
60+
$method = 'get'.strtr($name, $this->underscoreMap).'Service';
61+
$wrappedInstance = $this->{$method}(false);
62+
63+
$manager->setProxyInitializer(null);
64+
65+
return true;
66+
},
67+
$this->container,
68+
Container::class
69+
));
4170
}
4271
}
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