-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstance.php
More file actions
157 lines (130 loc) · 3.58 KB
/
Instance.php
File metadata and controls
157 lines (130 loc) · 3.58 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Elite Dangerous Star Map
* @link https://www.edsm.net/
*/
namespace Component;
abstract class Instance
{
protected static $view = null; //TODO: Is it used?!
/**
* Multiton implementation
*/
protected static $instances = array();
public static function getInstance($id, $identity = null)
{
$id = (int) $id;
$className = static::class;
// Garbage removal
foreach(self::$instances AS $class => $instances)
{
if(array_key_exists($class, self::$instances) && count(self::$instances[$class]) > 500)
{
self::$instances[$class] = array();
}
}
if(!array_key_exists($className, self::$instances))
{
self::$instances[$className] = array();
}
if(!array_key_exists($id, self::$instances[$className]))
{
self::$instances[$className][$id] = new $className($id, $identity);
}
return self::$instances[$className][$id];
}
public static function destroyInstance($id)
{
$id = (int) $id;
$className = static::class;
if(array_key_exists($className, self::$instances))
{
if(array_key_exists($id, self::$instances[$className]))
{
unset(self::$instances[$className][$id]);
}
}
}
/**
* Default constructor to autopopulate instance
*
* @param type $id
* @param type $identity
* @return type
*/
protected function __construct($id, $identity)
{
$this->_id = (int) $id;
if(is_null($id))
{
return;
}
// Override default populate in case we want to avoid database call
if(!is_null($identity))
{
if(is_array($identity) && $identity[$this->_primaryKey] == $id)
{
$this->_identity = $identity;
return;
}
}
// Populate IDENTITY based on default model from class
$this->_identity = self::getModel($this->_defaultModel)->getById( $this->_id );
}
protected function __clone(){}
/**
* Database Models
*/
protected static $models = array();
protected function getModel($model)
{
if(!array_key_exists($model, self::$models))
{
self::$models[$model] = new $model;
}
return self::$models[$model];
}
/**
* Cache
*/
protected static $caches = array();
protected function getCache($cache)
{
if(!array_key_exists($cache, self::$caches))
{
$bootstrap = \Zend_Registry::get('Zend_Application');
$cacheManager = $bootstrap->getResource('cachemanager');
self::$caches[$cache] = $cacheManager->getCache($cache);
}
return self::$caches[$cache];
}
/**
* Identity
*/
protected $_id;
protected $_identity;
public function isValid()
{
if(is_null($this->_identity) || count($this->_identity) == 0)
{
return false;
}
return true;
}
public function getId()
{
return $this->_id;
}
protected function getIdentity($key = null)
{
if(!is_null($key))
{
if(is_array($this->_identity) && array_key_exists($key, $this->_identity))
{
return $this->_identity[$key];
}
return null;
}
return $this->_identity;
}
}