|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File was created 11.10.2015 13:22 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\Slumber\Data; |
|
7
|
|
|
|
|
8
|
|
|
use PeekAndPoke\Component\Psi\Psi; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Karsten J. Gerber <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class EntityPoolImpl implements EntityPool |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
private $pool = []; |
|
17
|
|
|
/** @var int */ |
|
18
|
|
|
private $numHits = 0; |
|
19
|
|
|
/** @var int */ |
|
20
|
|
|
private $numMisses = 0; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return EntityPoolImpl |
|
24
|
|
|
*/ |
|
25
|
3 |
|
public static function getInstance() |
|
26
|
|
|
{ |
|
27
|
3 |
|
static $instance; |
|
28
|
|
|
|
|
29
|
3 |
|
return $instance ?: $instance = new static; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Singleton constructor |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function __construct() { } |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function all() |
|
41
|
|
|
{ |
|
42
|
2 |
|
return $this->pool; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return EntityPoolStats |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function stats() |
|
49
|
|
|
{ |
|
50
|
1 |
|
return new EntityPoolStats(\count($this->pool), $this->numHits, $this->numMisses); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
//// GET SET CHECK /////////////////////////////////////////////////////////////////////////////////////////////// |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param \ReflectionClass $cls |
|
57
|
|
|
* @param string $idKey |
|
58
|
|
|
* @param mixed $idValue |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
23 |
|
public function has(\ReflectionClass $cls, $idKey, $idValue) |
|
63
|
|
|
{ |
|
64
|
23 |
|
$isset = isset($this->pool[$cls->name . '@' . $idKey . '@' . $idValue]); |
|
65
|
|
|
|
|
66
|
23 |
|
if ($isset) { |
|
67
|
7 |
|
++$this->numHits; |
|
68
|
|
|
} else { |
|
69
|
19 |
|
++$this->numMisses; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
23 |
|
return $isset; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param \ReflectionClass $cls |
|
77
|
|
|
* @param string $idKey |
|
78
|
|
|
* @param mixed $idValue |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
7 |
|
public function get(\ReflectionClass $cls, $idKey, $idValue) |
|
83
|
|
|
{ |
|
84
|
7 |
|
return $this->pool[$cls->name . '@' . $idKey . '@' . $idValue]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param \ReflectionClass $cls |
|
89
|
|
|
* @param string $idKey |
|
90
|
|
|
* @param mixed $idValue |
|
91
|
|
|
* @param mixed $value |
|
92
|
|
|
*/ |
|
93
|
27 |
|
public function set(\ReflectionClass $cls, $idKey, $idValue, $value) |
|
94
|
|
|
{ |
|
95
|
27 |
|
$this->pool[$cls->name . '@' . $idKey . '@' . $idValue] = $value; |
|
96
|
27 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
//// REMOVING ITEMS ////////////////////////////////////////////////////////////////////////////////////////////// |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Remove all entities from the pool |
|
102
|
|
|
*/ |
|
103
|
20 |
|
public function clear() |
|
104
|
|
|
{ |
|
105
|
20 |
|
$this->pool = []; |
|
106
|
20 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param \ReflectionClass $cls |
|
110
|
|
|
* @param string $idKey |
|
111
|
|
|
* @param string $idValue |
|
112
|
|
|
*/ |
|
113
|
|
|
public function remove(\ReflectionClass $cls, $idKey, $idValue) |
|
114
|
|
|
{ |
|
115
|
|
|
unset ($this->pool[$cls->name . '@' . $idKey . '@' . $idValue]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param \ReflectionClass $cls |
|
120
|
|
|
*/ |
|
121
|
|
|
public function removeAllOfType(\ReflectionClass $cls) |
|
122
|
|
|
{ |
|
123
|
|
|
$search = $cls->name . '@'; |
|
124
|
|
|
|
|
125
|
|
|
$this->pool = Psi::it($this->pool) |
|
126
|
|
|
->filterKey(function ($k) use ($search) { |
|
127
|
|
|
return strpos($k, $search) !== 0; |
|
128
|
|
|
}) |
|
129
|
|
|
->toArray(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|