Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace DI\Proxy;
6
 
7
use ProxyManager\Configuration;
8
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
9
use ProxyManager\FileLocator\FileLocator;
10
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
11
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
12
use ProxyManager\Proxy\LazyLoadingInterface;
13
 
14
/**
15
 * Creates proxy classes.
16
 *
17
 * Wraps Ocramius/ProxyManager LazyLoadingValueHolderFactory.
18
 *
19
 * @see \ProxyManager\Factory\LazyLoadingValueHolderFactory
20
 *
21
 * @since  5.0
22
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
23
 */
24
class ProxyFactory
25
{
26
    private ?LazyLoadingValueHolderFactory $proxyManager = null;
27
 
28
    /**
29
     * @param string|null $proxyDirectory If set, write the proxies to disk in this directory to improve performances.
30
     */
31
    public function __construct(
32
        private ?string $proxyDirectory = null,
33
    ) {
34
    }
35
 
36
    /**
37
     * Creates a new lazy proxy instance of the given class with
38
     * the given initializer.
39
     *
40
     * @param class-string $className name of the class to be proxied
41
     * @param \Closure $initializer initializer to be passed to the proxy
42
     */
43
    public function createProxy(string $className, \Closure $initializer) : LazyLoadingInterface
44
    {
45
        return $this->proxyManager()->createProxy($className, $initializer);
46
    }
47
 
48
    /**
49
     * Generates and writes the proxy class to file.
50
     *
51
     * @param class-string $className name of the class to be proxied
52
     */
53
    public function generateProxyClass(string $className) : void
54
    {
55
        // If proxy classes a written to file then we pre-generate the class
56
        // If they are not written to file then there is no point to do this
57
        if ($this->proxyDirectory) {
58
            $this->createProxy($className, function () {});
59
        }
60
    }
61
 
62
    private function proxyManager() : LazyLoadingValueHolderFactory
63
    {
64
        if ($this->proxyManager === null) {
65
            if (! class_exists(Configuration::class)) {
66
                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:~2.0".');
67
            }
68
 
69
            $config = new Configuration();
70
 
71
            if ($this->proxyDirectory) {
72
                $config->setProxiesTargetDir($this->proxyDirectory);
73
                $config->setGeneratorStrategy(new FileWriterGeneratorStrategy(new FileLocator($this->proxyDirectory)));
74
                // @phpstan-ignore-next-line
75
                spl_autoload_register($config->getProxyAutoloader());
76
            } else {
77
                $config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
78
            }
79
 
80
            $this->proxyManager = new LazyLoadingValueHolderFactory($config);
81
        }
82
 
83
        return $this->proxyManager;
84
    }
85
}