8000 [Bridge\ProxyManager] Dont call __destruct() on non-instantiated serv… · symfony/symfony@6fb67d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6fb67d5

Browse files
[Bridge\ProxyManager] Dont call __destruct() on non-instantiated services
1 parent 8f5141d commit 6fb67d5

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\ProxyManager\LazyProxy\Instantiator;
13+
14+
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
15+
use ProxyManager\Factory\LazyLoadingValueHolderFactory as BaseFactory;
16+
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\LazyLoadingValueHolderGenerator;
17+
18+
/**
19+
* @internal
20+
*/
21+
class LazyLoadingValueHolderFactory extends BaseFactory
22+
{
23+
private $generator;
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function getGenerator() : ProxyGeneratorInterface
29+
{
30+
return $this->generator ?: $this->generator = new LazyLoadingValueHolderGenerator();
31+
}
32+
}

src/Symfony/Bridge/ProxyManager/LazyProxy/Instantiator/RuntimeInstantiator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bridge\ProxyManager\LazyProxy\Instantiator;
1313

1414
use ProxyManager\Configuration;
15-
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
1615
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
1716
use ProxyManager\Proxy\LazyLoadingInterface;
1817
use Symfony\Component\DependencyInjection\ContainerInterface;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\ProxyManager\LazyProxy\PhpDumper;
13+
14+
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator as BaseGenerator;
15+
use Zend\Code\Generator\ClassGenerator;
16+
17+
/**
18+
* @internal
19+
*/
20+
class LazyLoadingValueHolderGenerator extends BaseGenerator
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function generate(\ReflectionClass $originalClass, ClassGenerator $classGenerator)
26+
{
27+
parent::generate($originalClass, $classGenerator);
28+
29+
if ($classGenerator->hasMethod('__destruct')) {
30+
$destructor = $classGenerator->getMethod('__destruct');
31+
$body = $destructor->getBody();
32+
$newBody = preg_replace('/^(\$this->initializer[\d\w]++) && .*;\n\nreturn (\$this->valueHolder)/', '$1 || $2', $body);
33+
34+
if ($body === $newBody) {
35+
throw new \UnexpectedValueException(sprintf('Unexpected lazy-proxy format generated for method %s::__destruct()', $originalClass->name));
36+
}
37+
38+
$destructor->setBody($newBody);
39+
}
40+
}
41+
}

src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php

Lines changed: 0 additions & 1 deletion
< 6D4E thead class="sr-only">
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use ProxyManager\Generator\ClassGenerator;
1515
use ProxyManager\GeneratorStrategy\BaseGeneratorStrategy;
16-
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator;
1716
use Symfony\Component\DependencyInjection\Container;
1817
use Symfony\Component\DependencyInjection\ContainerInterface;
1918
use Symfony\Component\DependencyInjection\Definition;

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/ContainerBuilderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function testCreateProxyServiceWithRuntimeInstantiator()
3939
/* @var $foo1 \ProxyManager\Proxy\LazyLoadingInterface|\ProxyManager\Proxy\ValueHolderInterface */
4040
$foo1 = $builder->get('foo1');
4141

42+
$foo1->__destruct();
43+
$this->assertSame(0, $foo1::$destructorCount);
44+
4245
$this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
4346
$this->assertInstanceOf('\ProxyManagerBridgeFooClass', $foo1);
4447
$this->assertInstanceOf('\ProxyManager\Proxy\LazyLoadingInterface', $foo1);
@@ -50,5 +53,8 @@ public function testCreateProxyServiceWithRuntimeInstantiator()
5053
$this->assertTrue($foo1->isProxyInitialized());
5154
$this->assertInstanceOf('\ProxyManagerBridgeFooClass', $foo1->getWrappedValueHolderValue());
5255
$this->assertNotInstanceOf('\ProxyManager\Proxy\LazyLoadingInterface', $foo1->getWrappedValueHolderValue());
56+
57+
$foo1->__destruct();
58+
$this->assertSame(1, $foo1::$destructorCount);
5359
}
5460
}

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/includes/foo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
class ProxyManagerBridgeFooClass
44
{
5+
public static $destructorCount = 0;
6+
57
public $foo;
68
public $moo;
79

@@ -38,4 +40,9 @@ public function setBar($value = null)
3840
{
3941
$this->bar = $value;
4042
}
43+
44+
public function __destruct()
45+
{
46+
++self::$destructorCount;
47+
}
4148
}

0 commit comments

Comments
 (0)
0