1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator; |
22
|
|
|
|
23
|
|
|
use ProxyManager\Generator\MagicMethodGenerator; |
24
|
|
|
use Zend\Code\Generator\ParameterGenerator; |
25
|
|
|
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; |
26
|
|
|
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator; |
27
|
|
|
use ReflectionClass; |
28
|
|
|
use Zend\Code\Generator\PropertyGenerator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Magic `__get` for lazy loading value holder objects |
32
|
|
|
* |
33
|
|
|
* @author Marco Pivetta <[email protected]> |
34
|
|
|
* @license MIT |
35
|
|
|
*/ |
36
|
|
|
class MagicGet extends MagicMethodGenerator |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param ReflectionClass $originalClass |
42
|
|
|
* @param PropertyGenerator $initializerProperty |
43
|
|
|
* @param PropertyGenerator $valueHolderProperty |
44
|
|
|
* @param PublicPropertiesMap $publicProperties |
45
|
|
|
* |
46
|
|
|
* @throws \Zend\Code\Generator\Exception\InvalidArgumentException |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
*/ |
49
|
2 |
|
public function __construct( |
50
|
|
|
ReflectionClass $originalClass, |
51
|
|
|
PropertyGenerator $initializerProperty, |
52
|
|
|
PropertyGenerator $valueHolderProperty, |
53
|
|
|
PublicPropertiesMap $publicProperties |
54
|
|
|
) { |
55
|
2 |
|
$hasParent = $originalClass->hasMethod('__get'); |
56
|
|
|
|
57
|
2 |
|
$initializer = $initializerProperty->getName(); |
58
|
2 |
|
$valueHolder = $valueHolderProperty->getName(); |
59
|
2 |
|
$callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" |
60
|
2 |
|
. ' return $this->' . $valueHolder . '->$name;' |
61
|
2 |
|
. "\n}\n\n"; |
62
|
|
|
|
63
|
2 |
|
if ($hasParent) { |
64
|
1 |
|
$parameter = $originalClass->getMethod('__get')->getParameters()[0]; |
65
|
|
|
|
66
|
1 |
|
$parameterType = $parameter->hasType() ? $parameter->getType() : null; |
67
|
|
|
|
68
|
1 |
|
parent::__construct($originalClass, '__get', [new ParameterGenerator('name', $parameterType)]); |
69
|
|
|
|
70
|
1 |
|
$this->setDocBlock(($hasParent ? "{@inheritDoc}\n" : '') . '@param ' . $parameterType . ' $name'); |
71
|
|
|
|
72
|
1 |
|
$this->setInitializerBody( |
73
|
|
|
$initializer, |
74
|
|
|
$valueHolder, |
75
|
1 |
|
$callParent . 'return $this->' . $valueHolder . '->__get($name);' |
76
|
|
|
); |
77
|
|
|
|
78
|
1 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
parent::__construct($originalClass, '__get', [new ParameterGenerator('name')]); |
82
|
|
|
|
83
|
1 |
|
$this->setDocBlock(($hasParent ? "{@inheritDoc}\n" : '') . '@param string $name'); |
84
|
|
|
|
85
|
1 |
|
$this->setInitializerBody( |
86
|
|
|
$initializer, |
87
|
|
|
$valueHolder, |
88
|
1 |
|
$callParent . PublicScopeSimulator::getPublicAccessSimulationCode( |
89
|
1 |
|
PublicScopeSimulator::OPERATION_GET, |
90
|
1 |
|
'name', |
91
|
1 |
|
null, |
92
|
|
|
$valueHolderProperty |
93
|
|
|
) |
94
|
|
|
); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $initializer |
99
|
|
|
* @param string $valueHolder |
100
|
|
|
* @param string $callParent |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
1 |
|
private function setInitializerBody(string $initializer, string $valueHolder, string $callParent) |
105
|
|
|
{ |
106
|
1 |
|
$this->setBody( |
107
|
1 |
|
'$this->' . $initializer . ' && $this->' . $initializer |
108
|
1 |
|
. '->__invoke($this->' . $valueHolder . ', $this, \'__get\', [\'name\' => $name], $this->' |
109
|
1 |
|
. $initializer . ');' |
110
|
1 |
|
. "\n\n" . $callParent |
111
|
|
|
); |
112
|
1 |
|
} |
113
|
|
|
} |
114
|
|
|
|