Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64cc4d8

Browse files
bug #39610 [ProxyManagerBridge] fix PHP notice, switch to "friendsofphp/proxy-manager-lts" (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [ProxyManagerBridge] fix PHP notice, switch to "friendsofphp/proxy-manager-lts" | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #39089 | License | MIT | Doc PR | - I submitted the fix for #39089 on the origin library at Ocramius/ProxyManager#646. Because of the [versioning policy](https://github.com/Ocramius/.github/blob/6d4561515513f6d59fb144d838971d4a7715e3e4/version-support.md#dependency-upgrades) in use at the origin library, this fix won't be available for PHP < 7.4. We usually resort to monkey-patching to workaround the policy and still ship the fix for 4.4 (which supports PHP >= 7.1). This time, and as explained in Ocramius/ProxyManager#630, I propose to delegate the fix to [friendsofphp/proxy-manager-lts](https://github.com/FriendsOfPHP/proxy-manager-lts/). It already embeds the fix and a few others that allow us to remove most of the monkey-patching we had to accumulate over time. Commits ------- 389f530 [ProxyManagerBridge] fix PHP notice, switch to "friendsofphp/proxy-manager-lts"
2 parents 18927fe + 389f530 commit 64cc4d8

File tree

7 files changed

+22
-94
lines changed

7 files changed

+22
-94
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"require": {
1919
"php": ">=7.1.3",
2020
"ext-xml": "*",
21+
"friendsofphp/proxy-manager-lts": "^1.0",
2122
"doctrine/event-manager": "~1.0",
2223
"doctrine/persistence": "^1.3|^2",
2324
"twig/twig": "^1.41|^2.10|^3.0",
@@ -112,7 +113,6 @@
112113
"masterminds/html5": "^2.6",
113114
"monolog/monolog": "^1.25.1",
114115
"nyholm/psr7": "^1.0",
115-
"ocramius/proxy-manager": "^2.1",
116116
"paragonie/sodium_compat": "^1.8",
117117
"php-http/httplug": "^1.0|^2.0",
118118
"predis/predis": "~1.1",

src/Symfony/Bridge/ProxyManager/LazyProxy/Instantiator/RuntimeInstantiator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($realInstantiator
4848
$proxy->setProxyInitializer(null);
4949

5050
return true;
51-
}
51+
},
52+
[
53+
'fluentSafe' => $definition->hasTag('proxy'),
54+
'skipDestructor' => true,
55+
]
5256
);
5357
}
5458
}

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

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,26 @@
1111

1212
namespace Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper;
1313

14+
use Laminas\Code\Generator\ClassGenerator;
1415
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator as BaseGenerator;
1516
use Symfony\Component\DependencyInjection\Definition;
16-
use Zend\Code\Generator\ClassGenerator;
1717

1818
/**
1919
* @internal
2020
*/
2121
class LazyLoadingValueHolderGenerator extends BaseGenerator
2222
{
23-
private $fluentSafe = false;
24-
25-
public function setFluentSafe(bool $fluentSafe)
26-
{
27-
$this->fluentSafe = $fluentSafe;
28-
}
29-
3023
/**
3124
* {@inheritdoc}
3225
*/
33-
public function generate(\ReflectionClass $originalClass, ClassGenerator $classGenerator): void
26+
public function generate(\ReflectionClass $originalClass, ClassGenerator $classGenerator, array $proxyOptions = []): void
3427
{
35-
parent::generate($originalClass, $classGenerator);
28+
parent::generate($originalClass, $classGenerator, $proxyOptions);
3629

3730
foreach ($classGenerator->getMethods() as $method) {
38-
$body = preg_replace(
39-
'/(\$this->initializer[0-9a-f]++) && \1->__invoke\(\$this->(valueHolder[0-9a-f]++), (.*?), \1\);/',
40-
'$1 && ($1->__invoke(\$$2, $3, $1) || 1) && $this->$2 = \$$2;',
41-
$method->getBody()
42-
);
43-
$body = str_replace('(new \ReflectionClass(get_class()))', '$reflection', $body);
44-
$body = str_replace('$reflection = $reflection ?: ', '$reflection = $reflection ?? ', $body);
45-
$body = str_replace('$reflection ?? $reflection = ', '$reflection ?? ', $body);
46-
47-
if ($originalClass->isInterface()) {
48-
$body = str_replace('get_parent_class($this)', var_export($originalClass->name, true), $body);
49-
$body = preg_replace_callback('/\n\n\$realInstanceReflection = [^{]++\{([^}]++)\}\n\n.*/s', function ($m) {
50-
$r = '';
51-
foreach (explode("\n", $m[1]) as $line) {
52-
$r .= "\n".substr($line, 4);
53-
if (0 === strpos($line, ' return ')) {
54-
break;
55-
}
56-
}
57-
58-
return $r;
59-
}, $body);
60-
}
61-
62-
if ($this->fluentSafe) {
63-
$indent = $method->getIndentation();
64-
$method->setIndentation('');
65-
$code = $method->generate();
66-
if (null !== $docBlock = $method->getDocBlock()) {
67-
$code = substr($code, \strlen($docBlock->generate()));
68-
}
69-
$refAmp = (strpos($code, '&') ?: \PHP_INT_MAX) <= strpos($code, '(') ? '&' : '';
70-
$body = preg_replace(
71-
'/\nreturn (\$this->valueHolder[0-9a-f]++)(->[^;]++);$/',
72-
"\nif ($1 === \$returnValue = {$refAmp}$1$2) {\n \$returnValue = \$this;\n}\n\nreturn \$returnValue;",
73-
$body
74-
);
75-
$method->setIndentation($indent);
76-
}
77-
7831
if (0 === strpos($originalClass->getFilename(), __FILE__)) {
79-
$body = str_replace(var_export($originalClass->name, true), '__CLASS__', $body);
80-
}
81-
82-
$method->setBody($body);
83-
}
84-
85-
if ($classGenerator->hasMethod('__destruct')) {
86-
$destructor = $classGenerator->getMethod('__destruct');
87-
$body = $destructor->getBody();
88-
$newBody = preg_replace('/^(\$this->initializer[a-zA-Z0-9]++) && .*;\n\nreturn (\$this->valueHolder)/', '$1 || $2', $body);
89-
90-
if ($body === $newBody) {
91-
throw new \UnexpectedValueException(sprintf('Unexpected lazy-proxy format generated for method "%s::__destruct()".', $originalClass->name));
32+
$method->setBody(str_replace(var_export($originalClass->name, true), '__CLASS__', $method->getBody()));
9233
}
93-
94-
$destructor->setBody($newBody);
9534
}
9635

9736
if (0 === strpos($originalClass->getFilename(), __FILE__)) {

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
namespace Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper;
1313

14-
use ProxyManager\Exception\ExceptionInterface;
1514
use ProxyManager\Generator\ClassGenerator;
16-
use ProxyManager\Generator\MethodGenerator;
1715
use ProxyManager\GeneratorStrategy\BaseGeneratorStrategy;
1816
use Symfony\Component\DependencyInjection\Definition;
1917
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface;
@@ -88,18 +86,6 @@ public function getProxyCode(Definition $definition): string
8886
$code = $this->classGenerator->generate($this->generateProxyClass($definition));
8987
$code = preg_replace('/^(class [^ ]++ extends )([^\\\\])/', '$1\\\\$2', $code);
9088

91-
if (!method_exists(MethodGenerator::class, 'fromReflectionWithoutBodyAndDocBlock')) { // proxy-manager < 2.2
92-
$code = preg_replace(
93-
'/((?:\$(?:this|initializer|instance)->)?(?:publicProperties|initializer|valueHolder))[0-9a-f]++/',
94-
'${1}'.$this->getIdentifierSuffix($definition),
95-
$code
96-
);
97-
}
98-
99-
if (!is_subclass_of(ExceptionInterface::class, 'Throwable')) { // proxy-manager < 2.5
100-
$code = preg_replace('/ \\\\Closure::bind\(function ((?:& )?\(\$instance(?:, \$value)?\))/', ' \Closure::bind(static function \1', $code);
101-
}
102-
10389
return $code;
10490
}
10591

@@ -118,8 +104,10 @@ private function generateProxyClass(Definition $definition): ClassGenerator
118104
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));
119105
$class = $this->proxyGenerator->getProxifiedClass($definition);
120106

121-
$this->proxyGenerator->setFluentSafe($definition->hasTag('proxy'));
122-
$this->proxyGenerator->generate(new \ReflectionClass($class), $generatedClass);
107+
$this->proxyGenerator->generate(new \ReflectionClass($class), $generatedClass, [
108+
'fluentSafe' => $definition->hasTag('proxy'),
109+
'skipDestructor' => true,
110+
]);
123111

124112
return $generatedClass;
125113
}

src/Symfony/Bridge/ProxyManager/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ Resources
1111
[send Pull Requests](https://github.com/symfony/symfony/pulls)
1212
in the [main Symfony repository](https://github.com/symfony/symfony)
1313

14-
[1]: https://github.com/Ocramius/ProxyManager
14+
[1]: https://github.com/FriendsOfPHP/proxy-manager-lts

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/Fixtures/proxy-implem.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function dummy()
1616
$this->initializer%s && ($this->initializer%s->__invoke($valueHolder%s, $this, 'dummy', array(), $this->initializer%s) || 1) && $this->valueHolder%s = $valueHolder%s;
1717

1818
if ($this->valueHolder%s === $returnValue = $this->valueHolder%s->dummy()) {
19-
$returnValue = $this;
19+
return $this;
2020
}
2121

2222
return $returnValue;
@@ -26,8 +26,8 @@ public function & dummyRef()
2626
{
2727
$this->initializer%s && ($this->initializer%s->__invoke($valueHolder%s, $this, 'dummyRef', array(), $this->initializer%s) || 1) && $this->valueHolder%s = $valueHolder%s;
2828

29-
if ($this->valueHolder%s === $returnValue = &$this->valueHolder%s->dummyRef()) {
30-
$returnValue = $this;
29+
if ($this->valueHolder%s === $returnValue = & $this->valueHolder%s->dummyRef()) {
30+
return $this;
3131
}
3232

3333
return $returnValue;
@@ -38,7 +38,7 @@ public function sunny()
3838
$this->initializer%s && ($this->initializer%s->__invoke($valueHolder%s, $this, 'sunny', array(), $this->initializer%s) || 1) && $this->valueHolder%s = $valueHolder%s;
3939

4040
if ($this->valueHolder%s === $returnValue = $this->valueHolder%s->sunny()) {
41-
$returnValue = $this;
41+
return $this;
4242
}
4343

4444
return $returnValue;
@@ -96,7 +96,7 @@ public function __set($name, $value)
9696

9797
$targetObject = $this->valueHolder%s;
9898

99-
return $targetObject->$name = $value;
99+
$targetObject->$name = $value; return $targetObject->$name;
100100
}
101101

102102
public function __isset($name)

src/Symfony/Bridge/ProxyManager/composer.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
"require": {
1919
"php": ">=7.1.3",
2020
"composer/package-versions-deprecated": "^1.8",
21-
"symfony/dependency-injection": "^4.0|^5.0",
22-
"ocramius/proxy-manager": "~2.1"
21+
"friendsofphp/proxy-manager-lts": "^1.0",
22+
"symfony/dependency-injection": "^4.0|^5.0"
2323
},
2424
"require-dev": {
2525
"symfony/config": "^3.4|^4.0|^5.0"
2626
},
27-
"conflict": {
28-
"zendframework/zend-eventmanager": "2.6.0"
29-
},
3027
"autoload": {
3128
"psr-4": { "Symfony\\Bridge\\ProxyManager\\": "" },
3229
"exclude-from-classmap": [

0 commit comments

Comments
 (0)