|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManager\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use OutOfBoundsException; |
|
8
|
|
|
use ProxyManager\Configuration; |
|
9
|
|
|
use ProxyManager\Generator\ClassGenerator; |
|
10
|
|
|
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface; |
|
11
|
|
|
use ProxyManager\Signature\Exception\InvalidSignatureException; |
|
12
|
|
|
use ProxyManager\Signature\Exception\MissingSignatureException; |
|
13
|
|
|
use ProxyManager\Version; |
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use function array_key_exists; |
|
16
|
|
|
use function class_exists; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Base factory common logic |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AbstractBaseFactory |
|
22
|
|
|
{ |
|
23
|
|
|
protected Configuration $configuration; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Cached checked class names |
|
27
|
|
|
* |
|
28
|
|
|
* @var string[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private array $checkedClasses = []; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(?Configuration $configuration = null) |
|
33
|
|
|
{ |
|
34
|
15 |
|
$this->configuration = $configuration ?: new Configuration(); |
|
35
|
|
|
} |
|
36
|
15 |
|
|
|
37
|
15 |
|
/** |
|
38
|
|
|
* Generate a proxy from a class name |
|
39
|
|
|
* |
|
40
|
|
|
* @param mixed[] $proxyOptions |
|
41
|
|
|
* |
|
42
|
|
|
* @throws InvalidSignatureException |
|
43
|
|
|
* @throws MissingSignatureException |
|
44
|
|
|
* @throws OutOfBoundsException |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function generateProxy(string $className, array $proxyOptions = []) : string |
|
47
|
|
|
{ |
|
48
|
1 |
|
if (array_key_exists($className, $this->checkedClasses)) { |
|
49
|
|
|
return $this->checkedClasses[$className]; |
|
50
|
1 |
|
} |
|
51
|
1 |
|
|
|
52
|
|
|
$proxyParameters = [ |
|
53
|
|
|
'className' => $className, |
|
54
|
|
|
'factory' => static::class, |
|
55
|
1 |
|
'proxyManagerVersion' => Version::getVersion(), |
|
56
|
1 |
|
]; |
|
57
|
1 |
|
$proxyClassName = $this |
|
58
|
|
|
->configuration |
|
59
|
|
|
->getClassNameInflector() |
|
60
|
1 |
|
->getProxyClassName($className, $proxyParameters); |
|
61
|
1 |
|
|
|
62
|
1 |
|
if (! class_exists($proxyClassName)) { |
|
63
|
|
|
$this->generateProxyClass( |
|
64
|
1 |
|
$proxyClassName, |
|
65
|
1 |
|
$className, |
|
66
|
1 |
|
$proxyParameters, |
|
67
|
1 |
|
$proxyOptions |
|
68
|
1 |
|
); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this |
|
72
|
|
|
->configuration |
|
73
|
|
|
->getSignatureChecker() |
|
74
|
1 |
|
->checkSignature(new ReflectionClass($proxyClassName), $proxyParameters); |
|
75
|
1 |
|
|
|
76
|
1 |
|
return $this->checkedClasses[$className] = $proxyClassName; |
|
77
|
|
|
} |
|
78
|
1 |
|
|
|
79
|
|
|
abstract protected function getGenerator() : ProxyGeneratorInterface; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Generates the provided `$proxyClassName` from the given `$className` and `$proxyParameters` |
|
83
|
|
|
* |
|
84
|
|
|
* @param string[] $proxyParameters |
|
85
|
|
|
* @param mixed[] $proxyOptions |
|
86
|
|
|
*/ |
|
87
|
|
|
private function generateProxyClass( |
|
88
|
|
|
string $proxyClassName, |
|
89
|
1 |
|
string $className, |
|
90
|
|
|
array $proxyParameters, |
|
91
|
|
|
array $proxyOptions = [] |
|
92
|
|
|
) : void { |
|
93
|
|
|
$className = $this->configuration->getClassNameInflector()->getUserClassName($className); |
|
94
|
|
|
$phpClass = new ClassGenerator($proxyClassName); |
|
95
|
1 |
|
|
|
96
|
1 |
|
$this->getGenerator()->generate(new ReflectionClass($className), $phpClass, $proxyOptions); |
|
97
|
|
|
|
|
98
|
1 |
|
$phpClass = $this->configuration->getClassSignatureGenerator()->addSignature($phpClass, $proxyParameters); |
|
99
|
|
|
|
|
100
|
1 |
|
$this->configuration->getGeneratorStrategy()->generate($phpClass, $proxyOptions); |
|
101
|
|
|
|
|
102
|
1 |
|
$autoloader = $this->configuration->getProxyAutoloader(); |
|
103
|
|
|
|
|
104
|
1 |
|
$autoloader($proxyClassName); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|