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