/
ProxyDumper.php
140 lines (112 loc) · 4.33 KB
/
ProxyDumper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?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 Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper;
use ProxyManager\Generator\ClassGenerator;
use ProxyManager\GeneratorStrategy\BaseGeneratorStrategy;
use ProxyManager\Version;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface;
/**
* Generates dumped PHP code of proxies via reflection.
*
* @author Marco Pivetta <ocramius@gmail.com>
*
* @final
*/
class ProxyDumper implements DumperInterface
{
private $salt;
private $proxyGenerator;
private $classGenerator;
public function __construct(string $salt = '')
{
$this->salt = $salt;
$this->proxyGenerator = new LazyLoadingValueHolderGenerator();
$this->classGenerator = new BaseGeneratorStrategy();
}
/**
* {@inheritdoc}
*/
public function isProxyCandidate(Definition $definition): bool
{
return ($definition->isLazy() || $definition->hasTag('proxy')) && $this->proxyGenerator->getProxifiedClass($definition);
}
/**
* {@inheritdoc}
*/
public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null): string
{
$instantiation = 'return';
if ($definition->isShared()) {
$instantiation .= sprintf(' $this->%s[%s] =', $definition->isPublic() && !$definition->isPrivate() ? 'services' : 'privates', var_export($id, true));
}
if (null === $factoryCode) {
throw new \InvalidArgumentException(sprintf('Missing factory code to construct the service "%s".', $id));
}
$proxyClass = $this->getProxyClassName($definition);
return <<<EOF
if (\$lazyLoad) {
$instantiation \$this->createProxy('$proxyClass', function () {
return \\$proxyClass::staticProxyConstructor(function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) {
\$wrappedInstance = $factoryCode;
\$proxy->setProxyInitializer(null);
return true;
});
});
}
EOF;
}
/**
* {@inheritdoc}
*/
public function getProxyCode(Definition $definition): string
{
$code = $this->classGenerator->generate($this->generateProxyClass($definition));
if (version_compare(self::getProxyManagerVersion(), '2.2', '<')) {
$code = preg_replace(
'/((?:\$(?:this|initializer|instance)->)?(?:publicProperties|initializer|valueHolder))[0-9a-f]++/',
'${1}'.$this->getIdentifierSuffix($definition),
$code
);
}
if (version_compare(self::getProxyManagerVersion(), '2.5', '<')) {
$code = preg_replace('/ \\\\Closure::bind\(function ((?:& )?\(\$instance(?:, \$value)?\))/', ' \Closure::bind(static function \1', $code);
}
return $code;
}
private static function getProxyManagerVersion(): string
{
if (!class_exists(Version::class)) {
return '0.0.1';
}
return \defined(Version::class.'::VERSION') ? Version::VERSION : Version::getVersion();
}
/**
* Produces the proxy class name for the given definition.
*/
private function getProxyClassName(Definition $definition): string
{
$class = $this->proxyGenerator->getProxifiedClass($definition);
return preg_replace('/^.*\\\\/', '', $class).'_'.$this->getIdentifierSuffix($definition);
}
private function generateProxyClass(Definition $definition): ClassGenerator
{
$generatedClass = new ClassGenerator($this->getProxyClassName($definition));
$class = $this->proxyGenerator->getProxifiedClass($definition);
$this->proxyGenerator->setFluentSafe($definition->hasTag('proxy'));
$this->proxyGenerator->generate(new \ReflectionClass($class), $generatedClass);
return $generatedClass;
}
private function getIdentifierSuffix(Definition $definition): string
{
$class = $this->proxyGenerator->getProxifiedClass($definition);
return substr(hash('sha256', $class.$this->salt), -7);
}
}