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

Skip to content

Commit b98bbb3

Browse files
committed
Provide a polyfill for proxy-manager
1 parent 42d6d3a commit b98bbb3

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@
123123
"Symfony\\Component\\": "src/Symfony/Component/"
124124
},
125125
"classmap": [
126-
"src/Symfony/Component/Intl/Resources/stubs"
126+
"src/Symfony/Component/Intl/Resources/stubs",
127+
"src/Symfony/Bridge/ProxyManager/Legacy/ProxiedMethodReturnExpression.php"
127128
],
128129
"exclude-from-classmap": [
129130
"**/Tests/"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public function getProxyCode(Definition $definition)
112112
);
113113
}
114114

115+
if (version_compare(self::getProxyManagerVersion(), '2.5', '<')) {
116+
$code = str_replace('\Closure::bind(function ', '\Closure::bind(static function ', $code);
117+
}
118+
115119
return $code;
116120
}
117121

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace ProxyManager\Generator\Util;
11+
12+
use Composer\Autoload\ClassLoader;
13+
use ReflectionMethod;
14+
use Symfony\Component\Debug\DebugClassLoader;
15+
16+
if (class_exists(\ProxyManager\Version::class) && \version_compare(\ProxyManager\Version::getVersion(), '2.5', '<')) {
17+
/**
18+
* Utility class to generate return expressions in method, given a method signature.
19+
*
20+
* This is required since return expressions may be forbidden by the method signature (void).
21+
*
22+
* @author Marco Pivetta <[email protected]>
23+
* @license MIT
24+
*/
25+
final class ProxiedMethodReturnExpression
26+
{
27+
public static function generate(string $returnedValueExpression, ?ReflectionMethod $originalMethod) : string
28+
{
29+
$originalReturnType = $originalMethod === null
30+
? null
31+
: $originalMethod->getReturnType();
32+
$originalReturnTypeName = $originalReturnType === null
33+
? null
34+
: $originalReturnType->getName();
35+
if ($originalReturnTypeName === 'void') {
36+
return $returnedValueExpression . ";\nreturn;";
37+
}
38+
return 'return ' . $returnedValueExpression . ';';
39+
}
40+
}
41+
} else {
42+
// Fallback to the original class by unregistering this file from composer class loader
43+
$getComposerClassLoader = static function($functionLoader) use (&$getComposerClassLoader) {
44+
if (\is_array($functionLoader)) {
45+
$functionLoader = $functionLoader[0];
46+
}
47+
if (!\is_object($functionLoader)) {
48+
return;
49+
}
50+
if ($functionLoader instanceof ClassLoader) {
51+
return $functionLoader;
52+
}
53+
if ($functionLoader instanceof DebugClassLoader) {
54+
return $getComposerClassLoader($functionLoader->getClassLoader());
55+
}
56+
};
57+
58+
$classLoader = null;
59+
while($classLoader === null && count($functions = spl_autoload_functions()) > 0) {
60+
$classLoader = $getComposerClassLoader(\array_shift($functions));
61+
}
62+
63+
if (null === $classLoader) {
64+
throw new \RuntimeException('Unable to find a class loader for "%s"');
65+
}
66+
67+
$classLoader->addClassMap([ProxiedMethodReturnExpression::class => null]);
68+
$classLoader->loadClass(ProxiedMethodReturnExpression::class);
69+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public function testGetProxyCode()
6363
);
6464
}
6565

66+
public function testGetProxyMagicCode()
67+
{
68+
$definition = new Definition(__CLASS__);
69+
$definition->setLazy(true);
70+
71+
$code = $this->dumper->getProxyCode($definition);
72+
73+
$this->assertStringContainsString('\Closure::bind(static function (\PHPUnit\Framework\TestCase $instance) {', $code);
74+
}
75+
6676
public function testDeterministicProxyCode()
6777
{
6878
$definition = new Definition(__CLASS__);

src/Symfony/Bridge/ProxyManager/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"autoload": {
2727
"psr-4": { "Symfony\\Bridge\\ProxyManager\\": "" },
28-
"classmap": [ "Legacy/Debug.php" ],
28+
"classmap": [ "Legacy/ProxiedMethodReturnExpression.php" ],
2929
"exclude-from-classmap": [
3030
"/Tests/"
3131
]

0 commit comments

Comments
 (0)