Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b98fc0a

Browse filesBrowse files
[Bridge\ProxyManager] Dont call __destruct() on non-instantiated services
1 parent 8f5141d commit b98fc0a
Copy full SHA for b98fc0a

File tree

7 files changed

+122
-2
lines changed
Filter options

7 files changed

+122
-2
lines changed
+31Lines 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+
}
+32Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/ProxyManager/LazyProxy/Instantiator/RuntimeInstantiator.php
+5-1Lines 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
}
4145

4246
/**
+41Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php
-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/ContainerBuilderTest.php
+6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/includes/foo.php
+7Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.