# cookiebot/3.11.1/addons/inc/Dependencies/DI/Proxy/ProxyFactory.php

Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA &amp; Google Consent Mode, version 3.11.1. 89 lines.

- Page: https://pluginprobe.com/plugins/cookiebot/3.11.1/code/addons/inc/Dependencies/DI/Proxy/ProxyFactory.php
- Raw: https://pluginprobe.com/plugins/cookiebot/3.11.1/raw/addons/inc/Dependencies/DI/Proxy/ProxyFactory.php
- Modified: 2021-03-23T11:49:48+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/cookiebot/3.11.1/code/addons/inc/Dependencies/DI/Proxy/ProxyFactory.php#L10-L20`.

```php
<?php
/**
 * PHP-DI
 *
 * @link      http://php-di.org/
 * @copyright Matthieu Napoli (http://mnapoli.fr/)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
 */

namespace Cybot\Dependencies\DI\Proxy;

use ProxyManager\Configuration;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;

/**
 * Creates proxy classes.
 *
 * Wraps Ocramius/ProxyManager LazyLoadingValueHolderFactory.
 *
 * @see ProxyManager\Factory\LazyLoadingValueHolderFactory
 *
 * @since  5.0
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
 */
class ProxyFactory
{
    /**
     * If true, write the proxies to disk to improve performances.
     * @var boolean
     */
    private $writeProxiesToFile;

    /**
     * Directory where to write the proxies (if $writeProxiesToFile is enabled).
     * @var string
     */
    private $proxyDirectory;

    /**
     * @var LazyLoadingValueHolderFactory|null
     */
    private $proxyManager;

    public function __construct($writeProxiesToFile, $proxyDirectory = null)
    {
        $this->writeProxiesToFile = $writeProxiesToFile;
        $this->proxyDirectory = $proxyDirectory;
    }

    /**
     * Creates a new lazy proxy instance of the given class with
     * the given initializer
     *
     * @param string   $className   name of the class to be proxied
     * @param \Closure $initializer initializer to be passed to the proxy
     *
     * @return \ProxyManager\Proxy\LazyLoadingInterface
     */
    public function createProxy($className, \Closure $initializer)
    {
        $this->createProxyManager();

        return $this->proxyManager->createProxy($className, $initializer);
    }

    private function createProxyManager()
    {
        if ($this->proxyManager !== null) {
            return;
        }

        if (! class_exists('ProxyManager\Configuration')) {
            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".');
        }

        $config = new Configuration();

        if ($this->writeProxiesToFile) {
            $config->setProxiesTargetDir($this->proxyDirectory);
            spl_autoload_register($config->getProxyAutoloader());
        } else {
            $config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
        }

        $this->proxyManager = new LazyLoadingValueHolderFactory($config);
    }
}

```
