Skip to content

Commit c4666e3

Browse files
committed
Make the Manager lighter
1 parent ff834d5 commit c4666e3

File tree

1 file changed

+38
-51
lines changed

1 file changed

+38
-51
lines changed

src/StaticEntityManager.php

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,33 @@ class StaticEntityManager
1919
{
2020
private $class;
2121

22+
/**
23+
* @var StaticEntity[]
24+
*/
2225
private $instances = array();
23-
private $ids = array();
26+
27+
/**
28+
* @var array
29+
*/
30+
private $ids = array();
31+
32+
/**
33+
* @var array
34+
*/
2435
private $dataSet;
2536

37+
/**
38+
* @var \ReflectionClass
39+
*/
40+
private $reflection;
41+
2642
/**
2743
* @param string $staticEntityClass
2844
*/
2945
public function __construct($staticEntityClass)
3046
{
31-
$this->class = $staticEntityClass;
47+
$this->class = $staticEntityClass;
48+
$this->reflection = new \ReflectionClass($this->class);
3249
$this->initDataSet();
3350
}
3451

@@ -39,7 +56,19 @@ public function __construct($staticEntityClass)
3956
*/
4057
public function get($id)
4158
{
42-
$this->createInstance($id);
59+
if (array_key_exists($id, $this->instances)) {
60+
return $this->instances[$id];
61+
} elseif (!$data = $this->getData($id)) {
62+
return $this->instances[$id] = null;
63+
}
64+
65+
$this->instances[$id] = $this->reflection->newInstance();
66+
67+
foreach ($data as $property => $value) {
68+
$reflectionProperty = $this->reflection->getProperty($property);
69+
$reflectionProperty->setAccessible(true);
70+
$reflectionProperty->setValue($this->instances[$id], $value);
71+
}
4372

4473
return $this->instances[$id];
4574
}
@@ -52,8 +81,6 @@ public function get($id)
5281
*/
5382
public function getAssociative($valueKey = 'name')
5483
{
55-
$this->initDataSet();
56-
5784
if (empty($valueKey)) {
5885
$valueKey = 'name';
5986
}
@@ -80,7 +107,7 @@ public function getIds()
80107
public function getAll()
81108
{
82109
foreach ($this->ids as $id) {
83-
$this->createInstance($id);
110+
$this->get($id);
84111
}
85112

86113
return array_filter($this->instances);
@@ -122,65 +149,25 @@ public function convertToId($staticEntity)
122149
*/
123150
private function initDataSet()
124151
{
125-
if (null !== $this->dataSet) {
126-
return;
127-
}
128-
129152
$dataSet = call_user_func(array($this->class, 'getDataSet'));
130153

131-
if (!is_array($dataSet)) {
132-
throw new \Exception(sprintf('%s::getDataSet() must returns an array', $this->class));
154+
if (!is_array($dataSet) || count($dataSet) !== count(array_filter($dataSet, 'is_array'))) {
155+
throw new \Exception('DataSet for class %s seems invalid');
133156
}
134157

135-
foreach ($dataSet as $id => $data) {
136-
$this->checkData($data, $id);
137-
$this->ids[] = $id;
158+
$this->ids = array_keys($dataSet);
159+
160+
foreach ($this->ids as $id) {
138161
$dataSet[$id]['id'] = $id;
139162
}
140163

141164
$this->dataSet = $dataSet;
142165
}
143166

144-
private function checkData($data, $id)
145-
{
146-
if (!is_array($data)) {
147-
throw new \Exception(sprintf('Data at index "%s" must be an array in %s', $id, $this->class));
148-
}
149-
}
150-
151167
private function getData($id)
152168
{
153169
return array_key_exists($id, $this->dataSet)
154170
? $this->dataSet[$id]
155171
: null;
156172
}
157-
158-
/**
159-
* @param mixed $id
160-
*
161-
* @return StaticEntity
162-
*/
163-
private function createInstance($id)
164-
{
165-
if (array_key_exists($id, $this->instances)) {
166-
return;
167-
}
168-
169-
$reflectionClass = new \ReflectionClass($this->class);
170-
$instance = $reflectionClass->newInstance();
171-
172-
if (null === ($data = $this->getData($id))) {
173-
$this->instances[$id] = null;
174-
175-
return;
176-
}
177-
178-
foreach ($data as $property => $value) {
179-
$reflectionProperty = $reflectionClass->getProperty($property);
180-
$reflectionProperty->setAccessible(true);
181-
$reflectionProperty->setValue($instance, $value);
182-
}
183-
184-
$this->instances[$id] = $instance;
185-
}
186173
}

0 commit comments

Comments
 (0)