| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WPDeveloper\BetterDocs\Dependencies\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 <matthieu@mnapoli.fr> |
| 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 |
private function createProxyManager() |
| 64 |
{ |
| 65 |
if ($this->proxyManager !== null) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if (! class_exists(Configuration::class)) { |
| 70 |
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".'); |
| 71 |
} |
| 72 |
|
| 73 |
$config = new Configuration(); |
| 74 |
|
| 75 |
if ($this->writeProxiesToFile) { |
| 76 |
$config->setProxiesTargetDir($this->proxyDirectory); |
| 77 |
$config->setGeneratorStrategy(new FileWriterGeneratorStrategy(new FileLocator($this->proxyDirectory))); |
| 78 |
spl_autoload_register($config->getProxyAutoloader()); |
| 79 |
} else { |
| 80 |
$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy()); |
| 81 |
} |
| 82 |
|
| 83 |
$this->proxyManager = new LazyLoadingValueHolderFactory($config); |
| 84 |
} |
| 85 |
} |
| 86 |
|