PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.1
Defender Security – Malware Scanner, Login Security & Firewall v5.6.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / DI / Proxy / ProxyFactory.php

ProxyFactory.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.1, at lib/packages/DI/Proxy/ProxyFactory.php

102 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace WPMU_DEV\Defender\Vendor\DI\Proxy;
6
7 use ProxyManager\Configuration;
8 use ProxyManager\Factory\LazyLoadingValueHolderFactory;
9 use ProxyManager\FileLocator\FileLocator;
10 use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
11 use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
12 use ProxyManager\Proxy\LazyLoadingInterface;
13
14 /**
15 * Creates proxy classes.
16 *
17 * Wraps Ocramius/ProxyManager LazyLoadingValueHolderFactory.
18 *
19 * @see \ProxyManager\Factory\LazyLoadingValueHolderFactory
20 *
21 * @since 5.0
22 * @author Matthieu Napoli <[email protected]>
23 */
24 class ProxyFactory
25 {
26 /**
27 * If true, write the proxies to disk to improve performances.
28 * @var bool
29 */
30 private $writeProxiesToFile;
31
32 /**
33 * Directory where to write the proxies (if $writeProxiesToFile is enabled).
34 * @var string|null
35 */
36 private $proxyDirectory;
37
38 /**
39 * @var LazyLoadingValueHolderFactory|null
40 */
41 private $proxyManager;
42
43 public function __construct(bool $writeProxiesToFile = false, string $proxyDirectory = null)
44 {
45 $this->writeProxiesToFile = $writeProxiesToFile;
46 $this->proxyDirectory = $proxyDirectory;
47 }
48
49 /**
50 * Creates a new lazy proxy instance of the given class with
51 * the given initializer.
52 *
53 * @param string $className name of the class to be proxied
54 * @param \Closure $initializer initializer to be passed to the proxy
55 */
56 public function createProxy(string $className, \Closure $initializer) : LazyLoadingInterface
57 {
58 $this->createProxyManager();
59
60 return $this->proxyManager->createProxy($className, $initializer);
61 }
62
63 /**
64 * Generates and writes the proxy class to file.
65 *
66 * @param string $className name of the class to be proxied
67 */
68 public function generateProxyClass(string $className)
69 {
70 // If proxy classes a written to file then we pre-generate the class
71 // If they are not written to file then there is no point to do this
72 if ($this->writeProxiesToFile) {
73 $this->createProxyManager();
74 $this->createProxy($className, function () {});
75 }
76 }
77
78 private function createProxyManager()
79 {
80 if ($this->proxyManager !== null) {
81 return;
82 }
83
84 if (! class_exists(Configuration::class)) {
85 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".');
86 }
87
88 $config = new Configuration();
89
90 if ($this->writeProxiesToFile) {
91 $config->setProxiesTargetDir($this->proxyDirectory);
92 $config->setGeneratorStrategy(new FileWriterGeneratorStrategy(new FileLocator($this->proxyDirectory)));
93 // @phpstan-ignore-next-line
94 spl_autoload_register($config->getProxyAutoloader());
95 } else {
96 $config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
97 }
98
99 $this->proxyManager = new LazyLoadingValueHolderFactory($config);
100 }
101 }
102