| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP-DI |
| 4 |
* |
| 5 |
* @link http://php-di.org/ |
| 6 |
* @copyright Matthieu Napoli (http://mnapoli.fr/) |
| 7 |
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file) |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Cybot\Dependencies\DI\Proxy; |
| 11 |
|
| 12 |
use ProxyManager\Configuration; |
| 13 |
use ProxyManager\Factory\LazyLoadingValueHolderFactory; |
| 14 |
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy; |
| 15 |
|
| 16 |
/** |
| 17 |
* Creates proxy classes. |
| 18 |
* |
| 19 |
* Wraps Ocramius/ProxyManager LazyLoadingValueHolderFactory. |
| 20 |
* |
| 21 |
* @see ProxyManager\Factory\LazyLoadingValueHolderFactory |
| 22 |
* |
| 23 |
* @since 5.0 |
| 24 |
* @author Matthieu Napoli <[email protected]> |
| 25 |
*/ |
| 26 |
class ProxyFactory |
| 27 |
{ |
| 28 |
/** |
| 29 |
* If true, write the proxies to disk to improve performances. |
| 30 |
* @var boolean |
| 31 |
*/ |
| 32 |
private $writeProxiesToFile; |
| 33 |
|
| 34 |
/** |
| 35 |
* Directory where to write the proxies (if $writeProxiesToFile is enabled). |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $proxyDirectory; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var LazyLoadingValueHolderFactory|null |
| 42 |
*/ |
| 43 |
private $proxyManager; |
| 44 |
|
| 45 |
public function __construct($writeProxiesToFile, $proxyDirectory = null) |
| 46 |
{ |
| 47 |
$this->writeProxiesToFile = $writeProxiesToFile; |
| 48 |
$this->proxyDirectory = $proxyDirectory; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Creates a new lazy proxy instance of the given class with |
| 53 |
* the given initializer |
| 54 |
* |
| 55 |
* @param string $className name of the class to be proxied |
| 56 |
* @param \Closure $initializer initializer to be passed to the proxy |
| 57 |
* |
| 58 |
* @return \ProxyManager\Proxy\LazyLoadingInterface |
| 59 |
*/ |
| 60 |
public function createProxy($className, \Closure $initializer) |
| 61 |
{ |
| 62 |
$this->createProxyManager(); |
| 63 |
|
| 64 |
return $this->proxyManager->createProxy($className, $initializer); |
| 65 |
} |
| 66 |
|
| 67 |
private function createProxyManager() |
| 68 |
{ |
| 69 |
if ($this->proxyManager !== null) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (! class_exists('ProxyManager\Configuration')) { |
| 74 |
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:~0.3".'); |
| 75 |
} |
| 76 |
|
| 77 |
$config = new Configuration(); |
| 78 |
|
| 79 |
if ($this->writeProxiesToFile) { |
| 80 |
$config->setProxiesTargetDir($this->proxyDirectory); |
| 81 |
spl_autoload_register($config->getProxyAutoloader()); |
| 82 |
} else { |
| 83 |
$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy()); |
| 84 |
} |
| 85 |
|
| 86 |
$this->proxyManager = new LazyLoadingValueHolderFactory($config); |
| 87 |
} |
| 88 |
} |
| 89 |
|