|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Laminas\Code\Generator\ParameterGenerator; |
|
9
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
|
10
|
|
|
use ProxyManager\Generator\MagicMethodGenerator; |
|
11
|
|
|
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; |
|
12
|
|
|
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator; |
|
13
|
|
|
use ReflectionClass; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Magic `__set` for lazy loading value holder objects |
|
17
|
|
|
*/ |
|
18
|
|
|
class MagicSet extends MagicMethodGenerator |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor |
|
22
|
|
|
* |
|
23
|
|
|
* @throws InvalidArgumentException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct( |
|
26
|
|
|
ReflectionClass $originalClass, |
|
27
|
|
|
PropertyGenerator $initializerProperty, |
|
28
|
2 |
|
PropertyGenerator $valueHolderProperty, |
|
29
|
|
|
PublicPropertiesMap $publicProperties |
|
30
|
|
|
) { |
|
31
|
|
|
parent::__construct( |
|
32
|
|
|
$originalClass, |
|
33
|
|
|
'__set', |
|
34
|
2 |
|
[new ParameterGenerator('name'), new ParameterGenerator('value')] |
|
35
|
2 |
|
); |
|
36
|
2 |
|
|
|
37
|
2 |
|
$hasParent = $originalClass->hasMethod('__set'); |
|
38
|
|
|
$initializer = $initializerProperty->getName(); |
|
39
|
|
|
$valueHolder = $valueHolderProperty->getName(); |
|
40
|
2 |
|
$callParent = ''; |
|
41
|
2 |
|
|
|
42
|
2 |
|
if (! $publicProperties->isEmpty()) { |
|
43
|
2 |
|
$callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" |
|
44
|
|
|
. ' return ($this->' . $valueHolder . '->$name = $value);' |
|
45
|
2 |
|
. "\n}\n\n"; |
|
46
|
2 |
|
} |
|
47
|
2 |
|
|
|
48
|
2 |
|
$callParent .= $hasParent |
|
49
|
|
|
? 'return $this->' . $valueHolder . '->__set($name, $value);' |
|
50
|
|
|
: PublicScopeSimulator::getPublicAccessSimulationCode( |
|
51
|
2 |
|
PublicScopeSimulator::OPERATION_SET, |
|
52
|
1 |
|
'name', |
|
53
|
1 |
|
'value', |
|
54
|
1 |
|
$valueHolderProperty |
|
55
|
1 |
|
); |
|
56
|
1 |
|
|
|
57
|
2 |
|
$this->setBody( |
|
58
|
|
|
'$this->' . $initializer . ' && $this->' . $initializer |
|
59
|
|
|
. '->__invoke($this->' . $valueHolder . ', $this, ' |
|
60
|
2 |
|
. '\'__set\', array(\'name\' => $name, \'value\' => $value), $this->' . $initializer . ');' |
|
61
|
2 |
|
. "\n\n" . $callParent |
|
62
|
2 |
|
); |
|
63
|
2 |
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|