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\Type\Any; |
||||
9 | use Ffcms\Core\Helper\Type\Arr; |
||||
10 | use Ffcms\Core\Helper\Type\Str; |
||||
11 | use Illuminate\Support\Collection; |
||||
12 | |||||
13 | /** |
||||
14 | * Class App - active record for 'prefix_apps' table. |
||||
15 | * @package Apps\ActiveRecord |
||||
16 | * @property int $id |
||||
17 | * @property string $type |
||||
18 | * @property string $sys_name |
||||
19 | * @property string $name |
||||
20 | * @property string $configs |
||||
21 | * @property bool $disabled |
||||
22 | * @property string $version |
||||
23 | * @property string $created_at |
||||
24 | * @property string $updated_at |
||||
25 | */ |
||||
26 | class App extends ActiveModel |
||||
27 | { |
||||
28 | protected $casts = [ |
||||
29 | 'id' => 'integer', |
||||
30 | 'type' => 'string', |
||||
31 | 'sys_name' => 'string', |
||||
32 | 'name' => 'serialize', |
||||
33 | 'configs' => 'serialize', |
||||
34 | 'disabled' => 'boolean', |
||||
35 | 'version' => 'string' |
||||
36 | ]; |
||||
37 | |||||
38 | /** |
||||
39 | * Get all objects with query caching |
||||
40 | * @param array $columns |
||||
41 | * @return \Illuminate\Database\Eloquent\Collection|null |
||||
42 | */ |
||||
43 | public static function all($columns = ['*']): ?Collection |
||||
44 | { |
||||
45 | $cacheName = 'activercord.app.all.' . implode('.', $columns); |
||||
46 | $records = MemoryObject::instance()->get($cacheName); |
||||
47 | if ($records === null) { |
||||
48 | $records = parent::all($columns); |
||||
49 | MemoryObject::instance()->set($cacheName, $records); |
||||
50 | } |
||||
51 | |||||
52 | return $records; |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * Get all object by defined $type with caching query in memory |
||||
57 | * @param string $type |
||||
58 | * @return array|null |
||||
59 | */ |
||||
60 | public static function getAllByType(string $type): ?array |
||||
61 | { |
||||
62 | $response = null; |
||||
63 | foreach (self::all() as $object) { |
||||
64 | if ($object->type === $type) { |
||||
65 | $response[] = $object; |
||||
66 | } |
||||
67 | } |
||||
68 | |||||
69 | return $response; |
||||
70 | } |
||||
71 | |||||
72 | /** |
||||
73 | * Get single row by defined type and sys_name with query caching |
||||
74 | * @param string $type |
||||
75 | * @param string|array $name |
||||
76 | * @return self|null |
||||
77 | */ |
||||
78 | public static function getItem($type, $name): ?self |
||||
79 | { |
||||
80 | foreach (self::all() as $object) { |
||||
81 | if ($object->type === $type) { //&& $object->sys_name === $sys_name) { |
||||
82 | if (Any::isArray($name) && Arr::in($object->sys_name, $name)) { // many different app name - maybe alias or something else |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
83 | return $object; |
||||
84 | } elseif (Any::isStr($name) && $object->sys_name === $name) { |
||||
85 | return $object; |
||||
86 | } |
||||
87 | } |
||||
88 | } |
||||
89 | |||||
90 | return null; |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * Get application configs |
||||
95 | * @param string $type |
||||
96 | * @param string $name |
||||
97 | * @return array|null|string |
||||
98 | */ |
||||
99 | public static function getConfigs(string $type, string $name) |
||||
100 | { |
||||
101 | foreach (self::all() as $row) { |
||||
102 | if ($row->type === $type && $row->sys_name === $name) { |
||||
103 | return $row->configs; |
||||
104 | } |
||||
105 | } |
||||
106 | |||||
107 | return null; |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * Get single config value by ext type, ext name and config key |
||||
112 | * @param string $type |
||||
113 | * @param string $name |
||||
114 | * @param string $configKey |
||||
115 | * @return array|string|null |
||||
116 | */ |
||||
117 | public static function getConfig(string $type, string $name, string $configKey) |
||||
118 | { |
||||
119 | $configs = self::getConfigs($type, $name); |
||||
120 | if (isset($configs[$configKey])) { |
||||
121 | return $configs[$configKey]; |
||||
122 | } |
||||
123 | |||||
124 | return null; |
||||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * Get localized application name |
||||
129 | * @return string |
||||
130 | */ |
||||
131 | public function getLocaleName(): string |
||||
132 | { |
||||
133 | if (!$this->sys_name) { |
||||
134 | return ''; |
||||
135 | } |
||||
136 | |||||
137 | $name = $this->getLocaled('name'); |
||||
138 | if (Str::likeEmpty($name)) { |
||||
0 ignored issues
–
show
It seems like
$name can also be of type array ; however, parameter $string of Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
139 | $name = $this->sys_name; |
||||
140 | } |
||||
141 | return $name; |
||||
0 ignored issues
–
show
|
|||||
142 | } |
||||
143 | |||||
144 | /** |
||||
145 | * Check if app version match db version of this app |
||||
146 | * @return bool |
||||
147 | * @throws SyntaxException |
||||
148 | */ |
||||
149 | public function checkVersion(): bool |
||||
150 | { |
||||
151 | if ($this->sys_name === null) { |
||||
152 | throw new SyntaxException('Application object is not founded'); |
||||
153 | } |
||||
154 | |||||
155 | $scriptVersion = $this->getScriptVersion(); |
||||
156 | |||||
157 | return version_compare($scriptVersion, $this->version) === 0; |
||||
158 | } |
||||
159 | |||||
160 | /** |
||||
161 | * Get extension script version if exists |
||||
162 | * @return string |
||||
163 | */ |
||||
164 | public function getScriptVersion(): string |
||||
165 | { |
||||
166 | $class = 'Apps\Controller\Admin\\' . $this->sys_name; |
||||
167 | if (!class_exists($class)) { |
||||
168 | return false; |
||||
0 ignored issues
–
show
|
|||||
169 | } |
||||
170 | |||||
171 | if (!defined($class . '::VERSION')) { |
||||
172 | return false; |
||||
0 ignored issues
–
show
|
|||||
173 | } |
||||
174 | |||||
175 | return constant($class . '::VERSION'); |
||||
176 | } |
||||
177 | } |
||||
178 |