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