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