Completed
Push — master ( ad0c4e...dd22e2 )
by Matthieu
07:51 queued 05:30
created

ProxyFactory::createProxyManager()   B

Complexity

Conditions 4
Paths 4

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 22
Code Lines 13

Importance

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