|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by Gorlum 10.02.2017 0:07 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Core; |
|
8
|
|
|
|
|
9
|
|
|
use \Common\ContainerPlus; |
|
10
|
|
|
use \Common\GlobalContainer; |
|
11
|
|
|
use TextEntity; |
|
12
|
|
|
use TextModel; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Core\Repository |
|
16
|
|
|
* |
|
17
|
|
|
* Holds current entity objects |
|
18
|
|
|
*/ |
|
19
|
|
|
class Repository { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* "Not an Object" marker |
|
23
|
|
|
*/ |
|
24
|
|
|
const NaO = '\\NaO'; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ContainerPlus $_repository |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_repository; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ContainerPlus $_oldRepo |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $_oldRepo; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var GlobalContainer $gc |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $gc; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Core\Repository constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param GlobalContainer $gc |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(GlobalContainer $gc) { |
|
48
|
|
|
$this->gc = $gc; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param TextModel $model |
|
53
|
|
|
* @param int|string $id |
|
54
|
|
|
* |
|
55
|
|
|
* @return TextEntity |
|
56
|
|
|
* @deprecated |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getById($model, $id) { |
|
59
|
|
|
|
|
60
|
|
|
// TODO - is_object() |
|
61
|
|
|
|
|
62
|
|
|
// Index is fully qualified class name plus ID like Namespace\ClassName\$id |
|
63
|
|
|
$entityIndex = get_class($model) . '\\' . $id; |
|
64
|
|
|
if (!isset($this->_oldRepo[$entityIndex])) { |
|
65
|
|
|
$entity = $model->loadById($id); |
|
66
|
|
|
if ($entity && !$entity->isEmpty()) { |
|
67
|
|
|
$this->_oldRepo[$entityIndex] = $entity; |
|
68
|
|
|
} |
|
69
|
|
|
} else { |
|
70
|
|
|
$entity = $this->_oldRepo[$entityIndex]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $entity; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns collection name for supplied object |
|
78
|
|
|
* |
|
79
|
|
|
* @param $object |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getCollectionName($object) { |
|
84
|
|
|
return is_object($object) ? get_class($object) : self::NaO; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function get($entityClass, $id) { |
|
88
|
|
|
$entityIndex = get_class($entityClass) . '\\' . $id; |
|
89
|
|
|
if(!isset($this->_repository[$entityIndex])) { |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->_repository[$entityIndex]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function getPool($entityClass) { |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function registerFactory($factory) { |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.