8000 #17676 - making the proxy instantiation compatible with ProxyManager … · Ocramius/symfony@f3d41d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit f3d41d3

Browse files
committed
symfony#17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
1 parent 1b85799 commit f3d41d3

File tree

6 files changed

+215
-10
lines changed

6 files changed

+215
-10
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
"doctrine/orm": "~2.4,>=2.4.5",
8383
"doctrine/doctrine-bundle": "~1.4",
8484
"monolog/monolog": "~1.11",
85-
"ocramius/proxy-manager": "~0.4|~1.0",
8685
"egulias/email-validator": "~1.2",
8786
"symfony/polyfill-apcu": "~1.1",
8887
"symfony/security-acl": "~2.8|~3.0",

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,16 @@ public function getProxyFactoryCode(Definition $definition, $id)
7474
$methodName = 'get'.Container::camelize($id).'Service';
7575
$proxyClass = $this->getProxyClassName($definition);
7676

77+
$generatedClass = $this->generateProxyClass($definition);
78+
79+
$constructorCall = $generatedClass->hasMethod('staticProxyConstructor')
80+
? $proxyClass . '::staticProxyConstructor'
81+
: 'new ' . $proxyClass;
82+
7783
return <<<EOF
7884
if (\$lazyLoad) {
7985
80-
$instantiation new $proxyClass(
86+
$instantiation $constructorCall(
8187
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) {
8288
\$wrappedInstance = \$this->$methodName(false);
8389
@@ -97,11 +103,7 @@ function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy)
97103
*/
98104
public function getProxyCode(Definition $definition)
99105
{
100-
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));
101-
102-
$this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $generatedClass);
103-
104-
return $this->classGenerator->generate($generatedClass);
106+
return $this->classGenerator->generate($this->generateProxyClass($definition));
105107
}
106108

107109
/**
@@ -115,4 +117,16 @@ private function getProxyClassName(Definition $definition)
115117
{
116118
return str_replace('\\', '', $definition->getClass()).'_'.spl_object_hash($definition).$this->salt;
117119
}
120+
121+
/**
122+
* @return ClassGenerator
123+
*/
124+
private function generateProxyClass(Definition $definition)
125+
{
126+
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));
127+
128+
$this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $generatedClass);
129+
130+
return $generatedClass;
131+
}
118132
}

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Dumper/PhpDumperTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\ProxyManager\Tests\LazyProxy\Dumper;
1313

14+
use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\StaticProxyConstructor;
1415
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
@@ -49,7 +50,11 @@ public function testDumpContainerWithProxyService()
4950
*/
5051
public function testDumpContainerWithProxyServiceWillShareProxies()
5152
{
52-
require_once __DIR__.'/../Fixtures/php/lazy_service.php';
53+
if (class_exists(StaticProxyConstructor::class)) { // detecting ProxyManager v2
54+
require_once __DIR__ . '/../Fixtures/php/lazy_service_with_hints.php';
55+
} else {
56+
require_once __DIR__ . '/../Fixtures/php/lazy_service.php';
57+
}
5358

5459
$container = new \LazyServiceProjectServiceContainer();
5560

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/php/lazy_service_structure.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ProjectServiceContainer extends Container
77
{
88
if ($lazyLoad) {
99

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

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerInterface;
4+
use Symfony\Component\DependencyInjection\Container;
5+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
6+
use Symfony\Component\DependencyInjection\Exception\LogicException;
7+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
8+
use Symfony\Component\DependencyInjection\Reference;
9+
use Symfony\Component\DependencyInjection\Parameter;
10+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
11+
12+
/**
13+
* ProjectServiceContainer.
14+
*
15+
* This class has been auto-generated
16+
* by the Symfony Dependency Injection Component.
17+
*/
18+
class LazyServiceProjectServiceContainer extends Container
19+
{
20+
/**
21+
* Constructor.
22+
*/
23+
public function __construct()
24+
{
25+
$this->services = array();
26+
}
27+
28+
/**
29+
* Gets the 'foo' service.
30+
*
31+
* This service is shared.
32+
* This method always returns the same instance of the service.
33+
*
34+
* @param bool $lazyLoad whether to try lazy-loading the service with a proxy
35+
*
36+
* @return stdClass A stdClass instance.
37+
*/
38+
public function getFooService($lazyLoad = true)
39+
{
40+
if ($lazyLoad) {
41+
return $this->services['foo'] = new stdClass_c1d194250ee2e2b7d2eab8b8212368a8(
42+
10000 function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {
43+
$wrappedInstance = $this->getFooService(false);
44+
45+
$proxy->setProxyInitializer(null);
46+
47+
return true;
48+
}
49+
);
50+
}
51+
52+
return new \stdClass();
53+
}
54+
}
55+
56+
class stdClass_c1d194250ee2e2b7d2eab8b8212368a8 extends \stdClass implements \ProxyManager\Proxy\LazyLoadingInterface, \ProxyManager\Proxy\ValueHolderInterface
57+
{
58+
/**
59+
* @var \Closure|null initializer responsible for generating the wrapped object
60+
*/
61+
private $valueHolder5157dd96e88c0 = null;
62+
63+
/**
64+
* @var \Closure|null initializer responsible for generating the wrapped object
65+
*/
66+
private $initializer5157dd96e8924 = null;
67+
68+
/**
69+
* @override constructor for lazy initialization
70+
*
71+
* @param \Closure|null $initializer
72+
*/
73+
public function __construct($initializer)
74+
{
75+
$this->initializer5157dd96e8924 = $initializer;
76+
}
77+
78+
/**
79+
* @param string $name
80+
*/
81+
public function __get($name)
82+
{
83+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__get', array('name' => $name));
84+
85+
return $this->valueHolder5157dd96e88c0->$name;
86+
}
87+
88+
/**
89+
* @param string $name
90+
* @param mixed $value
91+
*/
92+
public function __set($name, $value)
93+
{
94+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__set', array('name' => $name, 'value' => $value));
95+
96+
$this->valueHolder5157dd96e88c0->$name = $value;
97+
}
98+
99+
/**
100+
* @param string $name
101+
*
102+
* @return bool
103+
*/
104+
public function __isset($name)
105+
{
106+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__isset', array('name' => $name));
107+
108+
return isset($this->valueHolder5157dd96e88c0->$name);
109+
}
110+
111+
/**
112+
* @param string $name
113+
*/
114+
public function __unset($name)
115+
{
116+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__unset', array('name' => $name));
117+
118+
unset($this->valueHolder5157dd96e88c0->$name);
119+
}
120+
121+
/**
122+
*
123+
*/
124+
public function __clone()
125+
{
126+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__clone', array());
127+
128+
$this->valueHolder5157dd96e88c0 = clone $this->valueHolder5157dd96e88c0;
129+
}
130+
131+
/**
132+
*
133+
*/
134+
public function __sleep()
135+
{
136+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__sleep', array());
137+
138+
return array('valueHolder5157dd96e88c0');
139+
}
140+
141+
/**
142+
*
143+
*/
144+
public function __wakeup()
145+
{
146+
}
147+
148+
/**
149+
* {@inheritdoc}
150+
*/
151+
public function setProxyInitializer(\Closure $initializer = null)
152+
{
153+
$this->initializer5157dd96e8924 = $initializer;
154+
}
155+
156+
/**
157+
* {@inheritdoc}
158+
*/
159+
public function getProxyInitializer()
160+
{
161+
return $this->initializer5157dd96e8924;
162+
}
163+
164+
/**
165+
* {@inheritdoc}
166+
*/
167+
public function initializeProxy() : bool
168+
{
169+
return $this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, 'initializeProxy', array());
170+
}
171+
172+
/**
173+
* {@inheritdoc}
174+
*/
175+
public function isProxyInitialized() : bool
176+
{
177+
return null !== $this->valueHolder5157dd96e88c0;
178+
}
179+
180+
/**
181+
* {@inheritdoc}
182+
*/
183+
public function getWrappedValueHolderValue()
184+
{
185+
return $this->valueHolder5157dd96e88c0;
186+
}
187+
}

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testGetProxyFactoryCode()
6969
$code = $this->dumper->getProxyFactoryCode($definition, 'foo');
7070

7171
$this->assertStringMatchesFormat(
72-
'%wif ($lazyLoad) {%wreturn $this->services[\'foo\'] = new '
72+
'%wif ($lazyLoad) {%wreturn $this->services[\'foo\'] =%s'
7373
.'SymfonyBridgeProxyManagerTestsLazyProxyPhpDumperProxyDumperTest_%s(%wfunction '
7474
.'(&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {'
7575
.'%w$wrappedInstance = $this->getFooService(false);%w$proxy->setProxyInitializer(null);'

0 commit comments

Comments
 (0)
0