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
|
<?php
declare (strict_types=1); namespace ElementorDeps\DI\Proxy;
use ElementorDeps\ProxyManager\Configuration; use ElementorDeps\ProxyManager\Factory\LazyLoadingValueHolderFactory; use ElementorDeps\ProxyManager\FileLocator\FileLocator; use ElementorDeps\ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy; use ElementorDeps\ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy; use ElementorDeps\ProxyManager\Proxy\LazyLoadingInterface; /** * Creates proxy classes. * * Wraps Ocramius/ProxyManager LazyLoadingValueHolderFactory. * * @see \ProxyManager\Factory\LazyLoadingValueHolderFactory * * @since 5.0 * @author Matthieu Napoli <[email protected]> */ class ProxyFactory { /** * If true, write the proxies to disk to improve performances. * @var bool */ private $writeProxiesToFile; /** * Directory where to write the proxies (if $writeProxiesToFile is enabled). * @var string|null */ private $proxyDirectory; /** * @var LazyLoadingValueHolderFactory|null */ private $proxyManager; public function __construct(bool $writeProxiesToFile = \false, string $proxyDirectory = null) { $this->writeProxiesToFile = $writeProxiesToFile; $this->proxyDirectory = $proxyDirectory; } /** * Creates a new lazy proxy instance of the given class with * the given initializer. * * @param string $className name of the class to be proxied * @param \Closure $initializer initializer to be passed to the proxy */ public function createProxy(string $className, \Closure $initializer) : LazyLoadingInterface { $this->createProxyManager(); return $this->proxyManager->createProxy($className, $initializer); } /** * Generates and writes the proxy class to file. * * @param string $className name of the class to be proxied */ public function generateProxyClass(string $className) { // If proxy classes a written to file then we pre-generate the class // If they are not written to file then there is no point to do this if ($this->writeProxiesToFile) { $this->createProxyManager(); $this->createProxy($className, function () { }); } } private function createProxyManager() { if ($this->proxyManager !== null) { return; } if (!\class_exists(Configuration::class)) { throw new \RuntimeException('The ocramius/proxy-manager library is not installed. Lazy injection requires that library to be installed with Composer in order to work. Run "composer require ocramius/proxy-manager:~2.0".'); } $config = new Configuration(); if ($this->writeProxiesToFile) { $config->setProxiesTargetDir($this->proxyDirectory); $config->setGeneratorStrategy(new FileWriterGeneratorStrategy(new FileLocator($this->proxyDirectory))); // @phpstan-ignore-next-line \spl_autoload_register($config->getProxyAutoloader()); } else { $config->setGeneratorStrategy(new EvaluatingGeneratorStrategy()); } $this->proxyManager = new LazyLoadingValueHolderFactory($config); } }
|