1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/* |
5
|
|
|
* Go! AOP framework |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright 2016, Lisachenko Alexander <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Go\Proxy\Part; |
14
|
|
|
|
15
|
|
|
use Go\Aop\Framework\ClassFieldAccess; |
16
|
|
|
|
17
|
|
|
use function array_key_exists; |
18
|
|
|
use function get_parent_class; |
19
|
|
|
use function method_exists; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait that holds all general logic for working with intercepted properties |
23
|
|
|
*/ |
24
|
|
|
trait PropertyInterceptionTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Holds a collection of current values for intercepted properties |
28
|
|
|
*/ |
29
|
|
|
private array $__properties = []; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritDoc |
33
|
|
|
*/ |
34
|
|
|
public function &__get(string $name) |
35
|
|
|
{ |
36
|
|
|
if (array_key_exists($name, $this->__properties)) { |
37
|
|
|
/** @var ClassFieldAccess $fieldAccess */ |
38
|
|
|
$fieldAccess = self::$__joinPoints["prop:$name"]; |
39
|
|
|
$fieldAccess->ensureScopeRule(); |
40
|
|
|
|
41
|
|
|
$value = &$fieldAccess->__invoke($this, ClassFieldAccess::READ, $this->__properties[$name]); |
42
|
|
|
} elseif (method_exists(get_parent_class(), __FUNCTION__)) { |
43
|
|
|
$value = parent::__get($name); |
44
|
|
|
} else { |
45
|
|
|
trigger_error("Trying to access undeclared property {$name}"); |
46
|
|
|
|
47
|
|
|
$value = null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $value; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function __set(string $name, $value) |
57
|
|
|
{ |
58
|
|
|
if (array_key_exists($name, $this->__properties)) { |
59
|
|
|
/** @var ClassFieldAccess $fieldAccess */ |
60
|
|
|
$fieldAccess = self::$__joinPoints["prop:$name"]; |
61
|
|
|
$fieldAccess->ensureScopeRule(); |
62
|
|
|
|
63
|
|
|
$this->__properties[$name] = $fieldAccess->__invoke( |
64
|
|
|
$this, |
65
|
|
|
ClassFieldAccess::WRITE, |
66
|
|
|
$this->__properties[$name], |
67
|
|
|
$value |
68
|
|
|
); |
69
|
|
|
} elseif (method_exists(get_parent_class(), __FUNCTION__)) { |
70
|
|
|
parent::__set($name, $value); |
71
|
|
|
} else { |
72
|
|
|
$this->$name = $value; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
|
public function __isset($name) |
80
|
|
|
{ |
81
|
|
|
return isset($this->__properties[$name]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
|
|
public function __unset($name) |
88
|
|
|
{ |
89
|
|
|
$this->__properties[$name] = null; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|