8000 bug #17947 Fix - #17676 (backport #17919 to 2.3) (Ocramius) · dunglas/symfony@fe4362c · GitHub
[go: up one dir, main page]

Skip to content

Commit fe4362c

Browse files
committed
bug symfony#17947 Fix - symfony#17676 (backport symfony#17919 to 2.3) (Ocramius)
This PR was merged into the 2.3 branch. Discussion ---------- Fix - symfony#17676 (backport symfony#17919 to 2.3) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#17676 | License | MIT | Doc PR | This is a backport of symfony#17919 Commits ------- 0c6400a symfony#17676 - making the proxy instantiation compatible with ProxyManager 2.x by detecting proxy features
2 parents 85c4b06 + 0c6400a commit fe4362c

File tree

7 files changed

+222
-12
lines changed

7 files changed

+222
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"monolog/monolog": "~1.3",
7373
"propel/propel1": "~1.6",
7474
"ircmaxell/password-compat": "~1.0",
75-
"ocramius/proxy-manager": "~0.3.1"
75+
"ocramius/proxy-manager": "~0.3.1|~1.0|~2.0"
7676
},
7777
"autoload": {
7878
"psr-0": { "Symfony\\": "src/" },

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

Lines changed: 22 additions & 6 deletions

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,17 @@ public function getProxyFactoryCode(Definition $definition, $id)
7777
$methodName = 'get'.Container::camelize($id).'Service';
7878
$proxyClass = $this->getProxyClassName($definition);
7979

80+
$generatedClass = $this->generateProxyClass($definition);
81+
82+
$constructorCall = $generatedClass->hasMethod('staticProxyConstructor')
83+
? $proxyClass.'::staticProxyConstructor'
84+
: 'new '.$proxyClass;
85+
8086
return <<<EOF
8187
if (\$lazyLoad) {
8288
\$container = \$this;
8389
84-
$instantiation new $proxyClass(
90+
$instantiation $constructorCall(
8591
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) {
8692
\$wrappedInstance = \$container->$methodName(false);
8793
@@ -101,11 +107,7 @@ function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy)
101107
*/
102108
public function getProxyCode(Definition $definition)
103109
{
104-
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));
105-
106-
$this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $generatedClass);
107-
108-
return $this->classGenerator->generate($generatedClass);
110+
return $this->classGenerator->generate($this->generateProxyClass($definition));
109111
}
110112

111113
6D40 /**
@@ -119,4 +121,18 @@ private function getProxyClassName(Definition $definition)
119121
{
120122
return str_replace('\\', '', $definition->getClass()).'_'.spl_object_hash($definition).$this->salt;
121123
}
124+
125+
/**
126+
* @param Definition $definition
127+
*
128+
* @return ClassGenerator
129+
*/
130+
private function generateProxyClass(Definition $definition)
131+
{
132+
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));
133+
134+
$this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $generatedClass);
135+
136+
return $generatedClass;
137+
}
122138
}
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ public function testDumpContainerWithProxyService()
4949
*/
5050
public function testDumpContainerWithProxyServiceWillShareProxies()
5151
{
52-
require_once __DIR__.'/../Fixtures/php/lazy_service.php';
52+
// detecting ProxyManager v2
53+
if (class_exists('ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\StaticProxyConstructor')) {
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ProjectServiceContainer extends Container
88
if ($lazyLoad) {
99
$container = $this;
1010

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

@@ -23,5 +23,5 @@ class ProjectServiceContainer extends Container
2323
}
2424
}
2525

26-
class stdClass_%s extends \stdClass implements \ProxyManager\Proxy\LazyLoadingInterface, \ProxyManager\Proxy\ValueHolderInterface
26+
class stdClass_%s extends \stdClass implements \ProxyManager\%s
2727
{%a}%A
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
$container = $this;
42+
43+
return $this->services['foo'] = new stdClass_c1d194250ee2e2b7d2eab8b8212368a8(
44+
function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {
45+
$wrappedInstance = $this->getFooService(false);
46+
47+
$proxy->setProxyInitializer(null);
48+
49+
return true;
50+
}
51+
);
52+
}
53+
54+
return new \stdClass();
55+
}
56+
}
57+
58+
class stdClass_c1d194250ee2e2b7d2eab8b8212368a8 extends \stdClass implements \ProxyManager\Proxy\LazyLoadingInterface, \ProxyManager\Proxy\ValueHolderInterface
59+
{
60+
/**
61+
* @var \Closure|null initializer responsible for generating the wrapped object
62+
*/
63+
private $valueHolder5157dd96e88c0 = null;
64+
65+
/**
66+
* @var \Closure|null initializer responsible for generating the wrapped object
67+
*/
68+
private $initializer5157dd96e8924 = null;
69+
70+
/**
71+
* @override constructor for lazy initialization
72+
*
73+
* F987 @param \Closure|null $initializer
74+
*/
75+
public function __construct($initializer)
76+
{
77+
$this->initializer5157dd96e8924 = $initializer;
78+
}
79+
80+
/**
81+
* @param string $name
82+
*/
83+
public function __get($name)
84+
{
85+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__get', array('name' => $name));
86+
87+
return $this->valueHolder5157dd96e88c0->$name;
88+
}
89+
90+
/**
91+
* @param string $name
92+
* @param mixed $value
93+
*/
94+
public function __set($name, $value)
95+
{
96+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__set', array('name' => $name, 'value' => $value));
97+
98+
$this->valueHolder5157dd96e88c0->$name = $value;
99+
}
100+
101+
/**
102+
* @param string $name
103+
*
104+
* @return bool
105+
*/
106+
public function __isset($name)
107+
{
108+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__isset', array('name' => $name));
109+
110+
return isset($this->valueHolder5157dd96e88c0->$name);
111+
}
112+
113+
/**
114+
* @param string $name
115+
*/
116+
public function __unset($name)
117+
{
118+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__unset', array('name' => $name));
119+
120+
unset($this->valueHolder5157dd96e88c0->$name);
121+
}
122+
123+
/**
124+
*
125+
*/
126+
public function __clone()
127+
{
128+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__clone', array());
129+
130+
$this->valueHolder5157dd96e88c0 = clone $this->valueHolder5157dd96e88c0;
131+
}
132+
133+
/**
134+
*
135+
*/
136+
public function __sleep()
137+
{
138+
$this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, '__sleep', array());
139+
140+
return array('valueHolder5157dd96e88c0');
141+
}
142+
143+
/**
144+
*
145+
*/
146+
public function __wakeup()
147+
{
148+
}
149+
150+
/**
151+
* {@inheritdoc}
152+
*/
153+
public function setProxyInitializer(\Closure $initializer = null)
154+
{
155+
$this->initializer5157dd96e8924 = $initializer;
156+
}
157+
158+
/**
159+
* {@inheritdoc}
160+
*/
161+
public function getProxyInitializer()
162+
{
163+
return $this->initializer5157dd96e8924;
164+
}
165+
166+
/**
167+
* {@inheritdoc}
168+
*/
169+
public function initializeProxy() : bool
170+
{
171+
return $this->initializer5157dd96e8924 && $this->initializer5157dd96e8924->__invoke($this->valueHolder5157dd96e88c0, $this, 'initializeProxy', array());
172+
}
173+
174+
/**
175+
* {@inheritdoc}
176+
*/
177+
public function isProxyInitialized() : bool
178+
{
179+
return null !== $this->valueHolder5157dd96e88c0;
180+
}
181+
182+
/**
183+
* {@inheritdoc}
184+
*/
185+
public function getWrappedValueHolderValue()
186+
{
187+
return $this->valueHolder5157dd96e88c0;
188+
}
189+
}

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

src/Symfony/Bridge/ProxyManager/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=5.3.3",
2020
"symfony/dependency-injection": "~2.3",
21-
"ocramius/proxy-manager": "~0.3.1"
21+
"ocramius/proxy-manager": "~0.3.1|~1.0|~2.0"
2222
},
2323
"require-dev": {
2424
"symfony/config": "~2.3"

0 commit comments

Comments
 (0)
0