8000 [Bridge\ProxyManager] Dont call __destruct() on non-instantiated services by nicolas-grekas · Pull Request #23729 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Bridge\ProxyManager] Dont call __destruct() on non-instantiated services #23729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
8000 Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\ProxyManager\LazyProxy\Instantiator;

use ProxyManager\Factory\LazyLoadingValueHolderFactory as BaseFactory;
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\LazyLoadingValueHolderGenerator;

/**
* @internal
*/
class LazyLoadingValueHolderFactoryV1 extends BaseFactory
{
private $generatorV1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does it have a different name here ? Except for the property name, the code is the same in v1 and V2.

Copy link
Member Author
@nicolas-grekas nicolas-grekas Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in v0.4, the property exists and is protected, thus can't be made private...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could just use a name available in both versions


/**
* {@inheritdoc}
*/
protected function getGenerator()
{
return $this->generatorV1 ?: $this->generatorV1 = new LazyLoadingValueHolderGenerator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\ProxyManager\LazyProxy\Instantiator;

use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
use ProxyManager\Factory\LazyLoadingValueHolderFactory as BaseFactory;
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\LazyLoadingValueHolderGenerator;

/**
* @internal
*/
class LazyLoadingValueHolderFactoryV2 extends BaseFactory
{
private $generator;

/**
* {@inheritdoc}
*/
protected function getGenerator(): ProxyGeneratorInterface
{
return $this->generator ?: $this->generator = new LazyLoadingValueHolderGenerator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public function __construct()
$config = new Configuration();
$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());

$this->factory = new LazyLoadingValueHolderFactory($config);
if (method_exists('ProxyManager\Version', 'getVersion')) {
$this->factory = new LazyLoadingValueHolderFactoryV2($config);
} else {
$this->factory = new LazyLoadingValueHolderFactoryV1($config);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper;

use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator as BaseGenerator;
use Zend\Code\Generator\ClassGenerator;

/**
* @internal
*/
class LazyLoadingValueHolderGenerator extends BaseGenerator
{
/**
* {@inheritdoc}
*/
public function generate(\ReflectionClass $originalClass, ClassGenerator $classGenerator)
{
parent::generate($originalClass, $classGenerator);

if ($classGenerator->hasMethod('__destruct')) {
$destructor = $classGenerator->getMethod('__destruct');
$body = $destructor->getBody();
$newBody = preg_replace('/^(\$this->initializer[a-zA-Z0-9]++) && .*;\n\nreturn (\$this->valueHolder)/', '$1 || $2', $body);

if ($body === $newBody) {
throw new \UnexpectedValueException(sprintf('Unexpected lazy-proxy format generated for method %s::__destruct()', $originalClass->name));
}

$destructor->setBody($newBody);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use ProxyManager\Generator\ClassGenerator;
use ProxyManager\GeneratorStrategy\BaseGeneratorStrategy;
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function testCreateProxyServiceWithRuntimeInstantiator()
/* @var $foo1 \ProxyManager\Proxy\LazyLoadingInterface|\ProxyManager\Proxy\ValueHolderInterface */
$foo1 = $builder->get('foo1');

$foo1->__destruct();
$this->assertSame(0, $foo1::$destructorCount);

$this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
$this->assertInstanceOf('\ProxyManagerBridgeFooClass', $foo1);
$this->assertInstanceOf('\ProxyManager\Proxy\LazyLoadingInterface', $foo1);
Expand All @@ -50,5 +53,8 @@ public function testCreateProxyServiceWithRuntimeInstantiator()
$this->assertTrue($foo1->isProxyInitialized());
$this->assertInstanceOf('\ProxyManagerBridgeFooClass', $foo1->getWrappedValueHolderValue());
$this->assertNotInstanceOf('\ProxyManager\Proxy\LazyLoadingInterface', $foo1->getWrappedValueHolderValue());

$foo1->__destruct();
$this->assertSame(1, $foo1::$destructorCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class ProxyManagerBridgeFooClass
{
public static $destructorCount = 0;

public $foo;
public $moo;

Expand Down Expand Up @@ -38,4 +40,9 @@ public function setBar($value = null)
{
$this->bar = $value;
}

public function __destruct()
{
++self::$destructorCount;
}
}
0