PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.0
Defender Security – Malware Scanner, Login Security & Firewall v5.7.0
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.7.0, at lib/packages/DI/Proxy/ProxyFactory.php

92 lines 3.0 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
13 /**
14 * Creates proxy classes.
15 *
16 * Wraps Ocramius/ProxyManager LazyLoadingValueHolderFactory.
17 *
18 * @see LazyLoadingValueHolderFactory
19 *
20 * @since 5.0
21 * @author Matthieu Napoli <[email protected]>
22 */
23 class ProxyFactory implements ProxyFactoryInterface
24 {
25 private ?LazyLoadingValueHolderFactory $proxyManager = null;
26
27 /**
28 * @param string|null $proxyDirectory If set, write the proxies to disk in this directory to improve performances.
29 */
30 public function __construct(
31 private ?string $proxyDirectory = null,
32 ) {
33 }
34
35 /**
36 * Creates a new lazy proxy instance of the given class with
37 * the given initializer.
38 *
39 * {@inheritDoc}
40 */
41 public function createProxy(string $className, \Closure $createFunction): object
42 {
43 return $this->proxyManager()->createProxy(
44 $className,
45 function (& $wrappedObject, $proxy, $method, $params, & $initializer) use ($createFunction) {
46 $wrappedObject = $createFunction();
47 $initializer = null; // turning off further lazy initialization
48
49 return true;
50 }
51 );
52 }
53
54 /**
55 * Generates and writes the proxy class to file.
56 *
57 * @param class-string $className name of the class to be proxied
58 */
59 public function generateProxyClass(string $className) : void
60 {
61 // If proxy classes a written to file then we pre-generate the class
62 // If they are not written to file then there is no point to do this
63 if ($this->proxyDirectory) {
64 $this->createProxy($className, function () {});
65 }
66 }
67
68 private function proxyManager() : LazyLoadingValueHolderFactory
69 {
70 if ($this->proxyManager === null) {
71 if (! class_exists(Configuration::class)) {
72 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".');
73 }
74
75 $config = new Configuration();
76
77 if ($this->proxyDirectory) {
78 $config->setProxiesTargetDir($this->proxyDirectory);
79 $config->setGeneratorStrategy(new FileWriterGeneratorStrategy(new FileLocator($this->proxyDirectory)));
80 // @phpstan-ignore-next-line
81 spl_autoload_register($config->getProxyAutoloader());
82 } else {
83 $config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
84 }
85
86 $this->proxyManager = new LazyLoadingValueHolderFactory($config);
87 }
88
89 return $this->proxyManager;
90 }
91 }
92