1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saltwater\RedBean\Provider; |
4
|
|
|
|
5
|
|
|
use Saltwater\Server as S; |
6
|
|
|
use Saltwater\Utils as U; |
7
|
|
|
use Saltwater\Salt\Provider; |
8
|
|
|
use Saltwater\Salt\Module; |
9
|
|
|
|
10
|
|
|
class Entity extends Provider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $name |
14
|
|
|
* |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public function get($name) |
18
|
|
|
{ |
19
|
|
|
$entity = $this->format($name); |
20
|
|
|
|
21
|
|
|
if (!empty($entity)) { |
22
|
|
|
return $entity; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $name; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Format a full class name from an entity name |
30
|
|
|
* |
31
|
|
|
* @param string $name |
32
|
|
|
* |
33
|
|
|
* @return string|null |
34
|
|
|
*/ |
35
|
|
|
private function format($name) |
36
|
|
|
{ |
37
|
|
|
$bit = S::$n->registry->bit('entity.' . $name); |
38
|
|
|
|
39
|
|
|
if (!$bit) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->find($name, $bit); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Search for an entity implementation in the module stack |
48
|
|
|
* |
49
|
|
|
* @param string $name |
50
|
|
|
* @param int $bit |
51
|
|
|
* |
52
|
|
|
* @return bool|null|string |
53
|
|
|
*/ |
54
|
|
|
private function find($name, $bit) |
55
|
|
|
{ |
56
|
|
|
// Try to find the entity within the module that is calling us |
57
|
|
|
if ($class = $this->fromModule(self::$caller, $name, $bit)) { |
58
|
|
|
return $class; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Otherwise try to find it by going up the module stack |
62
|
|
|
$modules = S::$n->modules->finder->modulesBySalt('entity.' . $name); |
63
|
|
|
|
64
|
|
|
foreach ($modules as $module) { |
|
|
|
|
65
|
|
|
if ($class = $this->fromModule($module, $name, $bit)) { |
66
|
|
|
return $class; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check whether a module has an entity declared, if so, either load the |
75
|
|
|
* entity class, or fall back to the basic salt |
76
|
|
|
* |
77
|
|
|
* @param Module|string $module |
78
|
|
|
* @param string $name |
79
|
|
|
* @param int $bit |
80
|
|
|
* |
81
|
|
|
* @return bool|string |
82
|
|
|
*/ |
83
|
|
|
private function fromModule($module, $name, $bit) |
84
|
|
|
{ |
85
|
|
|
$module = S::$n->modules->get($module); |
|
|
|
|
86
|
|
|
|
87
|
|
|
if (!is_object($module) || !$module->has($bit)) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$class = $this->getEntityClass($name, $module); |
92
|
|
|
|
93
|
|
|
return class_exists($class) ? $class : 'Saltwater\RedBean\Salt\Entity'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get Entity class name from a name plus a module |
98
|
|
|
* |
99
|
|
|
* @param string $name |
100
|
|
|
* @param Module $module |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function getEntityClass($name, $module) |
105
|
|
|
{ |
106
|
|
|
return U::className($module::getNamespace(), 'entity', $name); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.