Skip to content

Fix - #17676 (backport #17919 to 2.3) #17947

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
Feb 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"monolog/monolog": "~1.3",
"propel/propel1": "~1.6",
"ircmaxell/password-compat": "~1.0",
"ocramius/proxy-manager": "~0.3.1"
"ocramius/proxy-manager": "~0.3.1|~1.0|~2.0"
},
"autoload": {
"psr-0": { "Symfony\\": "src/" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@ public function getProxyFactoryCode(Definition $definition, $id)
$methodName = 'get'.Container::camelize($id).'Service';
$proxyClass = $this->getProxyClassName($definition);

$generatedClass = $this->generateProxyClass($definition);

$constructorCall = $generatedClass->hasMethod('staticProxyConstructor')
? $proxyClass.'::staticProxyConstructor'
: 'new '.$proxyClass;

return <<<EOF
if (\$lazyLoad) {
\$container = \$this;

$instantiation new $proxyClass(
$instantiation $constructorCall(
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) {
\$wrappedInstance = \$container->$methodName(false);

Expand All @@ -101,11 +107,7 @@ function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy)
*/
public function getProxyCode(Definition $definition)
{
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));

$this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $generatedClass);

return $this->classGenerator->generate($generatedClass);
return $this->classGenerator->generate($this->generateProxyClass($definition));
}

/**
Expand All @@ -119,4 +121,18 @@ private function getProxyClassName(Definition $definition)
{
return str_replace('\\', '', $definition->getClass()).'_'.spl_object_hash($definition).$this->salt;
}

/**
* @param Definition $definition
*
* @return ClassGenerator
*/
private function generateProxyClass(Definition $definition)
{
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));

$this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $generatedClass);

return $generatedClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public function testDumpContainerWithProxyService()
*/
public function testDumpContainerWithProxyServiceWillShareProxies()
{
require_once __DIR__.'/../Fixtures/php/lazy_service.php';
// detecting ProxyManager v2
if (class_exists('ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\StaticProxyConstructor')) {
require_once __DIR__.'/../Fixtures/php/lazy_service_with_hints.php';
} else {
require_once __DIR__.'/../Fixtures/php/lazy_service.php';
}

$container = new \LazyServiceProjectServiceContainer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ProjectServiceContainer extends Container
if ($lazyLoad) {
$container = $this;

return $this->services['foo'] = new stdClass_%s(
return $this->services['foo'] =%sstdClass_%s(
function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {
$wrappedInstance = $container->getFooService(false);

Expand All @@ -23,5 +23,5 @@ class ProjectServiceContainer extends Container
}
}

class stdClass_%s extends \stdClass implements \ProxyManager\Proxy\LazyLoadingInterface, \ProxyManager\Proxy\ValueHolderInterface
class stdClass_%s extends \stdClass implements \ProxyManager\%s
{%a}%A
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* ProjectServiceContainer.
*
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*/
class LazyServiceProjectServiceContainer extends Container
{
/**
* Constructor.
*/
public function __construct()
{
$this->services = array();
}

/**
* Gets the 'foo' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @param bool $lazyLoad whether to try lazy-loading the service with a proxy
*
* @return stdClass A stdClass instance.
*/
public function getFooService($lazyLoad = true)
{
if ($lazyLoad) {
$container = $this;

return $this->services['foo'] = new stdClass_c1d194250ee2e2b7d2eab8b8212368a8(
function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {
$wrappedInstance = $this->getFooService(false);

$proxy->setProxyInitializer(null);

return true;
}
);
}

return new \stdClass();
}
}

class stdClass_c1d194250ee2e2b7d2eab8b8212368a8 extends \stdClass implements \ProxyManager\Proxy\LazyLoadingInterface, \ProxyManager\Proxy\ValueHolderInterface
{
/**
* @var \Closure|null initializer responsible for generating the wrapped object
*/
private $valueHolder5157dd96e88c0 = null;

/**
* @var \Closure|null initializer responsible for generating the wrapped object
*/
private $initializer5157dd96e8924 = null;

/**
* @override constructor for lazy initialization
*
* @param \Closure|null $initializer
*/
public function __construct($initializer)
{
$this->initializer5157dd96e8924 = $initializer;
}

/**
* @param string $name
*/
public function __get($name)
{
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__get', array('name' => $name));

return $this->valueHolder5157dd96e88c0->$name;
}

/**
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__set', array('name' => $name, 'value' => $value));

$this->valueHolder5157dd96e88c0->$name = $value;
}

/**
* @param string $name
*
* @return bool
*/
public function __isset($name)
{
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__isset', array('name' => $name));

return isset($this->valueHolder5157dd96e88c0->$name);
}

/**
* @param string $name
*/
public function __unset($name)
{
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__unset', array('name' => $name));

unset($this->valueHolder5157dd96e88c0->$name);
}

/**
*
*/
public function __clone()
{
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__clone', array());

$this->valueHolder5157dd96e88c0 = clone $this->valueHolder5157dd96e88c0;
}

/**
*
*/
public function __sleep()
{
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__sleep', array());

return array('valueHolder5157dd96e88c0');
}

/**
*
*/
public function __wakeup()
{
}

/**
* {@inheritdoc}
*/
public function setProxyInitializer(\Closure $initializer = null)
{
$this->initializer5157dd96e8924 = $initializer;
}

/**
* {@inheritdoc}
*/
public function getProxyInitializer()
{
return $this->initializer5157dd96e8924;
}

/**
* {@inheritdoc}
*/
public function initializeProxy() : bool
{
return $this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, 'initializeProxy', array());
}

/**
* {@inheritdoc}
*/
public function isProxyInitialized() : bool
{
return null !== $this->valueHolder5157dd96e88c0;
}

/**
* {@inheritdoc}
*/
public function getWrappedValueHolderValue()
{
return $this->valueHolder5157dd96e88c0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testGetProxyFactoryCode()
$code = $this->dumper->getProxyFactoryCode($definition, 'foo');

$this->assertStringMatchesFormat(
'%wif ($lazyLoad) {%w$container = $this;%wreturn $this->services[\'foo\'] = new '
'%wif ($lazyLoad) {%w$container = $this;%wreturn $this->services[\'foo\'] =%s'
.'SymfonyBridgeProxyManagerTestsLazyProxyPhpDumperProxyDumperTest_%s(%wfunction '
.'(&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {'
.'%w$wrappedInstance = $container->getFooService(false);%w$proxy->setProxyInitializer(null);'
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/ProxyManager/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=5.3.3",
"symfony/dependency-injection": "~2.3",
"ocramius/proxy-manager": "~0.3.1"
"ocramius/proxy-manager": "~0.3.1|~1.0|~2.0"
},
"require-dev": {
"symfony/config": "~2.3"
Expand Down