|
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 `__unset` method for lazy loading value holder objects |
|
17
|
|
|
*/ |
|
18
|
|
|
class MagicUnset 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($originalClass, '__unset', [new ParameterGenerator('name')]); |
|
32
|
|
|
|
|
33
|
|
|
$hasParent = $originalClass->hasMethod('__unset'); |
|
34
|
2 |
|
$initializer = $initializerProperty->getName(); |
|
35
|
|
|
$valueHolder = $valueHolderProperty->getName(); |
|
36
|
2 |
|
$callParent = ''; |
|
37
|
2 |
|
|
|
38
|
2 |
|
if (! $publicProperties->isEmpty()) { |
|
39
|
2 |
|
$callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" |
|
40
|
|
|
. ' unset($this->' . $valueHolder . '->$name);' . "\n\n return;" |
|
41
|
2 |
|
. "\n}\n\n"; |
|
42
|
2 |
|
} |
|
43
|
2 |
|
|
|
44
|
2 |
|
$callParent .= $hasParent |
|
45
|
|
|
? 'return $this->' . $valueHolder . '->__unset($name);' |
|
46
|
|
|
: PublicScopeSimulator::getPublicAccessSimulationCode( |
|
47
|
2 |
|
PublicScopeSimulator::OPERATION_UNSET, |
|
48
|
1 |
|
'name', |
|
49
|
1 |
|
null, |
|
50
|
1 |
|
$valueHolderProperty |
|
51
|
1 |
|
); |
|
52
|
1 |
|
|
|
53
|
2 |
|
$this->setBody( |
|
54
|
|
|
'$this->' . $initializer . ' && $this->' . $initializer |
|
55
|
|
|
. '->__invoke($this->' . $valueHolder . ', $this, \'__unset\', array(\'name\' => $name), $this->' |
|
56
|
2 |
|
. $initializer . ');' . "\n\n" . $callParent |
|
57
|
2 |
|
); |
|
58
|
2 |
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|