PluginProbe
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 3.11.1
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v3.11.1
4.7.4 4.7.5 4.7.3 4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 All 95 releases
cookiebot / addons / inc / Dependencies / DI / Proxy / ProxyFactory.php

ProxyFactory.php in Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode 3.11.1, at addons/inc/Dependencies/DI/Proxy/ProxyFactory.php

89 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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