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