Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Retro-fit proxy code to make it deterministic for older proxy manager implementations #26176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ProxyManager\Generator\ClassGenerator;
use ProxyManager\GeneratorStrategy\BaseGeneratorStrategy;
use ProxyManager\Version;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface;
Expand Down Expand Up @@ -94,11 +95,35 @@ public function getProxyFactoryCode(Definition $definition, $id, $factoryCode =
*/
public function getProxyCode(Definition $definition)
{
return preg_replace(
$code = $this->classGenerator->generate($this->generateProxyClass($definition));

$code = preg_replace(
'/(\$this->initializer[0-9a-f]++) && \1->__invoke\(\$this->(valueHolder[0-9a-f]++), (.*?), \1\);/',
'$1 && ($1->__invoke(\$$2, $3, $1) || 1) && $this->$2 = \$$2;',
$this->classGenerator->generate($this->generateProxyClass($definition))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could even keep this code here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could but it’s less symmetric

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas is this replacement related to backporting some ProxyManager too, or would be also keep it after dropping support for ProxyManager 2.1 and older ?

If it is meant to stay, I would not try to make it symmetric with the feature backporting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is Symfony-only feature, has been refused in ProxyManager (at least not merged yet), so not part of any versions of it

$code
);

if (version_compare(self::getProxyManagerVersion(), '2.2', '<')) {
$code = preg_replace(
'/((?:\$(?:this|initializer|instance)->)?(?:publicProperties|initializer|valueHolder))[0-9a-f]++/',
'${1}'.$this->getIdentifierSuffix($definition),
$code
);
}

return $code;
}

/**
* @return string
*/
private static function getProxyManagerVersion()
{
if (!\class_exists(Version::class)) {
return '0.0.1';
}

return defined(Version::class.'::VERSION') ? Version::VERSION : Version::getVersion();
}

/**
Expand All @@ -108,7 +133,7 @@ public function getProxyCode(Definition $definition)
*/
private function getProxyClassName(Definition $definition)
{
return preg_replace('/^.*\\\\/', '', $definition->getClass()).'_'.substr(hash('sha256', $definition->getClass().$this->salt), -7);
return preg_replace('/^.*\\\\/', '', $definition->getClass()).'_'.$this->getIdentifierSuffix($definition);
}

/**
Expand All @@ -122,4 +147,12 @@ private function generateProxyClass(Definition $definition)

return $generatedClass;
}

/**
* @return string
*/
private function getIdentifierSuffix(Definition $definition)
{
return substr(hash('sha256', $definition->getClass().$this->salt), -7);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public function testGetProxyCode()
);
}

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

$this->assertSame($this->dumper->getProxyCode($definition), $this->dumper->getProxyCode($definition));
}

public function testGetProxyFactoryCode()
{
$definition = new Definition(__CLASS__);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.