Completed
Push — ezp_30981_content_info_proxy ( 01dade...014174 )
by
unknown
14:51
created

ProxyGenerator::createProxy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\ProxyFactory;
10
11
use Closure;
12
use ProxyManager\Configuration;
13
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
14
use ProxyManager\FileLocator\FileLocator;
15
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
16
use ProxyManager\Proxy\VirtualProxyInterface;
17
use RuntimeException;
18
19
/**
20
 * @internal
21
 */
22
final class ProxyGenerator implements ProxyGeneratorInterface
23
{
24
    /** @var \ProxyManager\Factory\LazyLoadingValueHolderFactory|null */
25
    private $lazyLoadingValueHolderFactory;
26
27
    /** @var string */
28
    private $proxyCacheDir;
29
30
    public function __construct(string $proxyCacheDir)
31
    {
32
        $this->proxyCacheDir = $proxyCacheDir;
33
    }
34
35
    public function createProxy(
36
        string $className,
37
        Closure $initializer,
38
        array $proxyOptions = []
39
    ): VirtualProxyInterface {
40
        if ($this->lazyLoadingValueHolderFactory === null) {
41
            $this->lazyLoadingValueHolderFactory = $this->createLazyLoadingValueHolderFactory();
42
        }
43
44
        return $this->lazyLoadingValueHolderFactory->createProxy($className, $initializer, $proxyOptions);
45
    }
46
47
    public function warmUp(iterable $classes): void
48
    {
49
        foreach ($classes as $class) {
50
            $this->createProxy($class, function () {});
51
        }
52
    }
53
54
    private function createLazyLoadingValueHolderFactory(): LazyLoadingValueHolderFactory
55
    {
56
        $config = new Configuration();
57
58
        // Proxy cache directory needs to be created before
59
        if (!is_dir($this->proxyCacheDir)) {
60
            if (false === @mkdir($this->proxyCacheDir, 0777, true)) {
61
                if (!is_dir($this->proxyCacheDir)) {
62
                    $error = error_get_last();
63
64
                    throw new RuntimeException(sprintf(
65
                        'Unable to create the Repository Proxy directory "%s": %s',
66
                        $this->proxyCacheDir,
67
                        $error['message']
68
                    ));
69
                }
70
            }
71
        } elseif (!is_writable($this->proxyCacheDir)) {
72
            throw new RuntimeException(sprintf(
73
                'The Repository Proxy directory "%s" is not writeable for the current system user.',
74
                $this->proxyCacheDir
75
            ));
76
        }
77
78
        $config->setGeneratorStrategy(new FileWriterGeneratorStrategy(new FileLocator($this->proxyCacheDir)));
79
        $config->setProxiesTargetDir($this->proxyCacheDir);
80
81
        spl_autoload_register($config->getProxyAutoloader());
82
83
        return new LazyLoadingValueHolderFactory($config);
84
    }
85
}
86