1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\ActiveRecord; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Arch\ActiveModel; |
6
|
|
|
use Ffcms\Core\Cache\MemoryObject; |
7
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
8
|
|
|
use Ffcms\Core\Helper\Serialize; |
9
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
10
|
|
|
use Ffcms\Core\Helper\Type\Str; |
11
|
|
|
|
12
|
|
|
class App extends ActiveModel |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Get all objects with query caching |
17
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static |
18
|
|
|
* @throws SyntaxException |
19
|
|
|
*/ |
20
|
|
|
public static function getAll() |
21
|
|
|
{ |
22
|
|
|
$object = MemoryObject::instance()->get('app.cache.all'); |
23
|
|
|
// empty? |
24
|
|
|
if ($object === null) { |
25
|
|
|
$object = self::all(); |
26
|
|
|
MemoryObject::instance()->set('app.cache.all', $object); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($object === null) { |
30
|
|
|
throw new SyntaxException('Application table "prefix_app" is empty!!!'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $object; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get all object by defined $type with caching query in memory |
38
|
|
|
* @param $type |
39
|
|
|
* @return array|null |
40
|
|
|
* @throws SyntaxException |
41
|
|
|
*/ |
42
|
|
|
public static function getAllByType($type) |
43
|
|
|
{ |
44
|
|
|
$response = null; |
45
|
|
|
foreach (self::getAll() as $object) { |
|
|
|
|
46
|
|
|
if ($object['type'] === $type) { |
47
|
|
|
$response[] = $object; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $response; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get single row by defined type and sys_name with query caching |
56
|
|
|
* @param string $type |
57
|
|
|
* @param string $sys_name |
58
|
|
|
* @return mixed|null |
59
|
|
|
* @throws SyntaxException |
60
|
|
|
*/ |
61
|
|
|
public static function getItem($type, $sys_name) |
62
|
|
|
{ |
63
|
|
|
foreach (self::getAll() as $object) { |
|
|
|
|
64
|
|
|
if ($object['type'] === $type && $object['sys_name'] === $sys_name) { |
65
|
|
|
return $object; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get application configs |
74
|
|
|
* @param string $type |
75
|
|
|
* @param string $name |
76
|
|
|
* @return array|null|string |
77
|
|
|
* @throws SyntaxException |
78
|
|
|
*/ |
79
|
|
|
public static function getConfigs($type, $name) |
80
|
|
|
{ |
81
|
|
|
foreach (self::getAll() as $row) { |
|
|
|
|
82
|
|
|
if ($row->type === $type && $row->sys_name === $name) { |
83
|
|
|
return Serialize::decode($row->configs); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get localized application name |
92
|
|
|
* @return string |
93
|
|
|
* @throws SyntaxException |
94
|
|
|
*/ |
95
|
|
|
public function getLocaleName() |
96
|
|
|
{ |
97
|
|
|
if ($this->sys_name === null) { |
|
|
|
|
98
|
|
|
throw new SyntaxException('Application object is not founded'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$nameObject = Serialize::decode($this->name); |
|
|
|
|
102
|
|
|
$lang = \Ffcms\Core\App::$Request->getLanguage(); |
103
|
|
|
$name = $nameObject[$lang]; |
104
|
|
|
if (Str::likeEmpty($name)) { |
105
|
|
|
$name = $this->sys_name; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
return $name; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check if app version match db version of this app |
112
|
|
|
* @return bool |
113
|
|
|
* @throws SyntaxException |
114
|
|
|
*/ |
115
|
|
|
public function checkVersion() |
116
|
|
|
{ |
117
|
|
|
if ($this->sys_name === null) { |
|
|
|
|
118
|
|
|
throw new SyntaxException('Application object is not founded'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$class = 'Apps\Controller\Admin\\' . $this->sys_name; |
|
|
|
|
122
|
|
|
if (!class_exists($class)) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (!defined($class . '::VERSION')) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return (float)constant($class.'::VERSION') === (float)$this->version; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
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.