| 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 |
|