forked from webiny/Entity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityPool.php
executable file
·101 lines (86 loc) · 1.99 KB
/
EntityPool.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @copyright Copyright Webiny LTD
*/
namespace Webiny\Component\Entity;
use Webiny\Component\StdLib\SingletonTrait;
use Webiny\Component\StdLib\StdLibTrait;
use Webiny\Component\StdLib\StdObject\ArrayObject\ArrayObject;
/**
* EntityPool class holds instantiated entities
*
* @package Webiny\Component\Entity
*/
class EntityPool
{
use SingletonTrait, StdLibTrait;
/**
* @var ArrayObject
*/
private $pool;
protected function init()
{
$this->pool = $this->arr();
}
/**
* Get entity instance or false if entity does not exist
*
* @param $class
* @param $id
*
* @return bool|EntityAbstract
*/
public function get($class, $id)
{
$entityPool = $this->pool->key($class, $this->arr(), true);
if ($entityPool->keyExists($id)) {
return $entityPool->key($id);
}
return false;
}
/**
* Add instance to the pool
*
* @param $instance
*
* @return mixed
*/
public function add($instance)
{
$class = get_class($instance);
$entityPool = $this->pool->key($class, $this->arr(), true);
$entityPool->key($instance->getId()->getValue(), $instance);
return $instance;
}
/**
* Remove instance from pool
*
* @param $instance
*
* @return bool
*/
public function remove(EntityAbstract $instance)
{
$entityPool = $this->pool->key(get_class($instance), $this->arr(), true);
$entityPool->removeKey($instance->getId()->getValue());
unset($instance);
return true;
}
/**
* Remove all loaded instances from pool
*/
public function reset()
{
$this->pool = $this->arr();
}
/**
* Get entity database
* @return \Webiny\Component\Mongo\Mongo
*/
public function getDatabase()
{
return Entity::getInstance()->getDatabase();
}
}