Completed
Pull Request — master (#423)
by Marco
07:12
created

LazyLoadingGhostFactory::getGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\Factory;
6
7
use Closure;
8
use ProxyManager\Proxy\GhostObjectInterface;
9
use ProxyManager\ProxyGenerator\LazyLoadingGhostGenerator;
10
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
11
use ProxyManager\Signature\Exception\InvalidSignatureException;
12
use ProxyManager\Signature\Exception\MissingSignatureException;
13
14
/**
15
 * Factory responsible of producing ghost instances
16
 *
17
 * @author Marco Pivetta <[email protected]>
18
 * @license MIT
19
 */
20
class LazyLoadingGhostFactory extends AbstractBaseFactory
21
{
22
    /**
23
     * @var \ProxyManager\ProxyGenerator\LazyLoadingGhostGenerator|null
24
     */
25
    private $generator;
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    protected function getGenerator() : ProxyGeneratorInterface
31
    {
32
        return $this->generator ?: $this->generator = new LazyLoadingGhostGenerator();
33
    }
34
35
    /**
36
     * Creates a new lazy proxy instance of the given class with
37
     * the given initializer
38
     *
39
     * Please refer to the following documentation when using this method:
40
     *
41
     * @link https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-ghost-object.md
42
     *
43
     * @param string  $className   name of the class to be proxied
44
     * @param Closure $initializer initializer to be passed to the proxy. The initializer closure should have following
45
     *                             signature:
46
     *
47
     *                             <code>
48
     *                             $initializer = function (
49
     *                                 GhostObjectInterface $proxy,
50
     *                                 string $method,
51
     *                                 array $parameters,
52
     *                                 & $initializer,
53
     *                                 array $properties
54
     *                             ) {};
55
     *                             </code>
56
     *
57
     *                             Where:
58
     *                              - $proxy is the proxy instance on which the initializer is acting
59
     *                              - $method is the name of the method that triggered the lazy initialization
60
     *                              - $parameters are the parameters that were passed to $method
61
     *                              - $initializer by-ref initializer - should be assigned null in the initializer body
62
     *                              - $properties a by-ref map of the properties of the object, indexed by PHP
63
     *                                            internal property name. Assign values to it to initialize the
64
     *                                            object state
65
     *
66
     * @param mixed[] $proxyOptions a set of options to be used when generating the proxy. Currently supports only
67
     *                              key "skippedProperties", which allows to skip lazy-loading of some properties.
68
     *                              "skippedProperties" is a string[], containing a list of properties referenced
69
     *                              via PHP's internal property name (i.e. "\0ClassName\0propertyName")
70
     *
71
     * @throws MissingSignatureException
72
     * @throws InvalidSignatureException
73
     * @throws \OutOfBoundsException
74
     */
75 1
    public function createProxy(
76
        string $className,
77
        Closure $initializer,
78
        array $proxyOptions = []
79
    ) : GhostObjectInterface {
80 1
        $proxyClassName = $this->generateProxy($className, $proxyOptions);
81
82 1
        return $proxyClassName::staticProxyConstructor($initializer);
83
    }
84
}
85