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

Skip to content

Commit 51d7c0c

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

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
11+
namespace ProxyManager\Generator\Util;
12+
13+
use Composer\Autoload\ClassLoader;
14+
use ProxyManager\Version;
15+
16+
if (class_exists(Version::class) && version_compare(\defined(Version::class.'::VERSION') ? Version::VERSION : 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+
* @see https://github.com/Ocramius/ProxyManager
26+
*/
27+
final class ProxiedMethodReturnExpression
28+
{
29+
public static function generate(string $returnedValueExpression, ?\ReflectionMethod $originalMethod): string
30+
{
31+
$originalReturnType = null === $originalMethod ? null : $originalMethod->getReturnType();
32+
33+
$originalReturnTypeName = null === $originalReturnType ? null : $originalReturnType->getName();
34+
35+
if ('void' === $originalReturnTypeName) {
36+
return $returnedValueExpression.";\nreturn;";
37+
}
38+
39+
return 'return '.$returnedValueExpression.';';
40+
}
41+
}
42+
} else {
43+
// Fallback to the original class by unregistering this file from composer class loader
44+
$getComposerClassLoader = static function ($functionLoader) use (&$getComposerClassLoader) {
45+
if (\is_array($functionLoader)) {
46+
$functionLoader = $functionLoader[0];
47+
}
48+
if (!\is_object($functionLoader)) {
49+
return;
50+
}
51+
if ($functionLoader instanceof ClassLoader) {
52+
return $functionLoader;
53+
}
54+
if ($functionLoader instanceof \Symfony\Component\Debug\DebugClassLoader) {
55+
return $getComposerClassLoader($functionLoader->getClassLoader());
56+
}
57+
if ($functionLoader instanceof \Symfony\Component\ErrorHandler\DebugClassLoader) {
58+
return $getComposerClassLoader($functionLoader->getClassLoader());
59+
}
60+
};
61+
62+
$classLoader = null;
63+
$functions = spl_autoload_functions();
64+
while (null === $classLoader && $functions) {
65+
$classLoader = $getComposerClassLoader(array_shift($functions));
66+
}
67+
$getComposerClassLoader = null;
68+
69+
if (null !== $classLoader) {
70+
$classLoader->addClassMap([ProxiedMethodReturnExpression::class => null]);
71+
$classLoader->loadClass(ProxiedMethodReturnExpression::class);
72+
}
73+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\ProxyManager\Tests\LazyProxy\PhpDumper;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use ProxyManager\Version;
1516
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Definition;
@@ -63,6 +64,20 @@ public function testGetProxyCode()
6364
);
6465
}
6566

67+
public function testStaticBinding()
68+
{
69+
if (!class_exists(Version::class) || version_compare(\defined(Version::class.'::VERSION') ? Version::VERSION : Version::getVersion(), '2.1', '<')) {
70+
$this->markTestSkipped('ProxyManager prior to version 2.1 does not support static binding');
71+
}
72+
73+
$definition = new Definition(__CLASS__);
74+
$definition->setLazy(true);
75+
76+
$code = $this->dumper->getProxyCode($definition);
77+
78+
$this->assertStringContainsString('\Closure::bind(static function (\PHPUnit\Framework\TestCase $instance) {', $code);
79+
}
80+
6681
public function testDeterministicProxyCode()
6782
{
6883
$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)