Skip to content

Commit

Permalink
Provide a polyfill for proxy-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse committed Aug 6, 2019
1 parent 42d6d3a commit b98bbb3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -123,7 +123,8 @@
"Symfony\\Component\\": "src/Symfony/Component/"
},
"classmap": [
"src/Symfony/Component/Intl/Resources/stubs"
"src/Symfony/Component/Intl/Resources/stubs",
"src/Symfony/Bridge/ProxyManager/Legacy/ProxiedMethodReturnExpression.php"
],
"exclude-from-classmap": [
"**/Tests/"
Expand Down
Expand Up @@ -112,6 +112,10 @@ public function getProxyCode(Definition $definition)
);
}

if (version_compare(self::getProxyManagerVersion(), '2.5', '<')) {
$code = str_replace('\Closure::bind(function ', '\Closure::bind(static function ', $code);
}

return $code;
}

Expand Down
@@ -0,0 +1,69 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ProxyManager\Generator\Util;

use Composer\Autoload\ClassLoader;
use ReflectionMethod;
use Symfony\Component\Debug\DebugClassLoader;

if (class_exists(\ProxyManager\Version::class) && \version_compare(\ProxyManager\Version::getVersion(), '2.5', '<')) {
/**
* Utility class to generate return expressions in method, given a method signature.
*
* This is required since return expressions may be forbidden by the method signature (void).
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
final class ProxiedMethodReturnExpression
{
public static function generate(string $returnedValueExpression, ?ReflectionMethod $originalMethod) : string
{
$originalReturnType = $originalMethod === null
? null
: $originalMethod->getReturnType();
$originalReturnTypeName = $originalReturnType === null
? null
: $originalReturnType->getName();
if ($originalReturnTypeName === 'void') {
return $returnedValueExpression . ";\nreturn;";
}
return 'return ' . $returnedValueExpression . ';';
}
}
} else {
// Fallback to the original class by unregistering this file from composer class loader
$getComposerClassLoader = static function($functionLoader) use (&$getComposerClassLoader) {
if (\is_array($functionLoader)) {
$functionLoader = $functionLoader[0];
}
if (!\is_object($functionLoader)) {
return;
}
if ($functionLoader instanceof ClassLoader) {
return $functionLoader;
}
if ($functionLoader instanceof DebugClassLoader) {
return $getComposerClassLoader($functionLoader->getClassLoader());
}
};

$classLoader = null;
while($classLoader === null && count($functions = spl_autoload_functions()) > 0) {
$classLoader = $getComposerClassLoader(\array_shift($functions));
}

if (null === $classLoader) {
throw new \RuntimeException('Unable to find a class loader for "%s"');
}

$classLoader->addClassMap([ProxiedMethodReturnExpression::class => null]);
$classLoader->loadClass(ProxiedMethodReturnExpression::class);
}
Expand Up @@ -63,6 +63,16 @@ public function testGetProxyCode()
);
}

public function testGetProxyMagicCode()
{
$definition = new Definition(__CLASS__);
$definition->setLazy(true);

$code = $this->dumper->getProxyCode($definition);

$this->assertStringContainsString('\Closure::bind(static function (\PHPUnit\Framework\TestCase $instance) {', $code);
}

public function testDeterministicProxyCode()
{
$definition = new Definition(__CLASS__);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/ProxyManager/composer.json
Expand Up @@ -25,7 +25,7 @@
},
"autoload": {
"psr-4": { "Symfony\\Bridge\\ProxyManager\\": "" },
"classmap": [ "Legacy/Debug.php" ],
"classmap": [ "Legacy/ProxiedMethodReturnExpression.php" ],
"exclude-from-classmap": [
"/Tests/"
]
Expand Down

0 comments on commit b98bbb3

Please sign in to comment.