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

Skip to content

Commit bcfdac7

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

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/Symfony/Bridge/Doctrine/ManagerRegistry.php

Lines changed: 32 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 ProxyManager\Proxy\ValueHolderInterface;
1416
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1517
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1618
use Doctrine\Common\Persistence\AbstractManagerRegistry;
@@ -37,6 +39,35 @@ 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 won\'t be supported in version 4.0. Set the "%s" service as lazy instead.', $name));
49+
50+
$this->container->set($name, null);
51+
52+
return;
53+
}
54+
$manager->setProxyInitializer(
55+
function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
56+
$this->container->set($name, null);
57+
$proxy = $this->container->get($name);
58+
59+
if ($proxy instanceof LazyLoadingInterface && !$proxy->isProxyInitialized() && $proxy instanceof ValueHolderInterface) {
60+
$proxy->initializeProxy();
61+
$wrappedInstance = $proxy->getWrappedValueHolderValue();
62+
} else {
63+
$wrappedInstance = $proxy;
64+
}
65+
66+
$manager->setProxyInitializer(null);
67+
$this->container->set($name, $manager);
68+
69+
return true;
70+
}
71+
);
4172
}
4273
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
use Symfony\Component\DependencyInjection\Container;
17+
18+
class ManagerRegistryTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public static function setUpBeforeClass()
21+
{
22+
$test = new PhpDumperTest();
23+
$test->testDumpContainerWithProxyServiceWillShareProxies();
24+
}
25+
26+
public function testLegacyResetService()
27+
{
28+
$container = new \LazyServiceProjectServiceContainer();
29+
30+
$registry = new TestManagerRegistry('name', array(), array('defaultManager' => 'foo'), 'defaultConnection', 'defaultManager', 'proxyInterfaceName');
31+
$registry->setContainer($container);
32+
33+
$foo = $container->get('foo');
34+
$foo->bar = 123;
35+
$this->assertTrue(isset($foo->bar));
36+
37+
$registry->resetManager();
38+
39+
$this->assertSame($foo, $container->get('foo'));
40+
$this->assertFalse(isset($foo->bar));
41+
}
42+
}
43+
44+
class TestManagerRegistry extends ManagerRegistry
45+
{
46+
public function getAliasNamespace($alias)
47+
{
48+
return 'Foo';
49+
}
50+
}

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