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

Skip to content

Commit 2d79ffa

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

File tree

7 files changed

+122
-2
lines changed

7 files changed

+122
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Factory\LazyLoadingValueHolderFactory as BaseFactory;
15+
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\LazyLoadingValueHolderGenerator;
16+
17+
/**
18+
* @internal
19+
*/
20+
class LazyLoadingValueHolderFactoryV1 extends BaseFactory
21+
{
22+
private $generatorV1;
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected function getGenerator()
28+
{
29+
return $this->generatorV1 ?: $this->generatorV1 = new LazyLoadingValueHolderGenerator();
30+
}
31+
}
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 LazyLoadingValueHolderFactoryV2 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public function __construct()
3636
$config = new Configuration();
3737
$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
3838

39-
$this->factory = new LazyLoadingValueHolderFactory($config);
39+
if (method_exists('ProxyManager\Version', 'getVersion')) {
40+
$this->factory = new LazyLoadingValueHolderFactoryV2($config);
41+
} else {
42+
$this->factory = new LazyLoadingValueHolderFactoryV1($config);
43+
}
4044< 57AE /code>
}
4145

4246
/**
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[a-zA-Z0-9]++) && .*;\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
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