6
6
7
7
use App \Entity \ScreenLayout ;
8
8
use App \Entity \ScreenLayoutRegions ;
9
+ use App \Enum \ResourceTypeEnum ;
9
10
use App \Model \ScreenLayoutData ;
10
11
use App \Repository \ScreenLayoutRegionsRepository ;
12
+ use App \Utils \ResourceLoader ;
11
13
use Doctrine \ORM \EntityManagerInterface ;
12
14
use Doctrine \ORM \Id \AssignedGenerator ;
13
- use JsonSchema \Constraints \Factory ;
14
- use JsonSchema \SchemaStorage ;
15
- use JsonSchema \Validator ;
16
- use Symfony \Component \Finder \Finder ;
17
15
use Symfony \Component \Uid \Ulid ;
18
16
19
17
class ScreenLayoutService
@@ -24,41 +22,15 @@ class ScreenLayoutService
24
22
public function __construct (
25
23
private readonly EntityManagerInterface $ entityManager ,
26
24
private readonly ScreenLayoutRegionsRepository $ layoutRegionsRepository ,
25
+ private readonly ResourceLoader $ loader ,
27
26
) {}
28
27
29
- public function getAllScreenLayouts (): array
28
+ public function getScreenLayouts (): array
30
29
{
31
- return array_merge ($ this ->getCoreScreenLayouts (), $ this ->getCustomScreenLayouts ());
32
- }
33
-
34
- public function getCoreScreenLayouts (): array
35
- {
36
- $ finder = new Finder ();
37
-
38
- if (is_dir ($ this ::CORE_SCREEN_LAYOUTS_PATH )) {
39
- $ finder ->files ()->followLinks ()->ignoreUnreadableDirs ()->in ($ this ::CORE_SCREEN_LAYOUTS_PATH )->depth ('== 0 ' )->name ('*.json ' );
40
-
41
- if ($ finder ->hasResults ()) {
42
- return $ this ->getScreenLayouts ($ finder );
43
- }
44
- }
45
-
46
- return [];
47
- }
48
-
49
- public function getCustomScreenLayouts (): array
50
- {
51
- $ finder = new Finder ();
52
-
53
- if (is_dir ($ this ::CUSTOM_SCREEN_LAYOUTS_PATH )) {
54
- $ finder ->files ()->followLinks ()->ignoreUnreadableDirs ()->in ($ this ::CUSTOM_SCREEN_LAYOUTS_PATH )->depth ('== 0 ' )->name ('*.json ' );
55
-
56
- if ($ finder ->hasResults ()) {
57
- return $ this ->getScreenLayouts ($ finder , true );
58
- }
59
- }
30
+ $ core = $ this ->loader ->getResourceJsonInDirectory ($ this ::CORE_SCREEN_LAYOUTS_PATH , ScreenLayoutData::class, ResourceTypeEnum::CORE );
31
+ $ custom = $ this ->loader ->getResourceJsonInDirectory ($ this ::CUSTOM_SCREEN_LAYOUTS_PATH , ScreenLayoutData::class, ResourceTypeEnum::CUSTOM );
60
32
61
- return [] ;
33
+ return array_merge ( $ core , $ custom ) ;
62
34
}
63
35
64
36
public function installScreenLayout (ScreenLayoutData $ screenLayoutData , bool $ update = false , bool $ cleanupRegions = false ): void
@@ -138,122 +110,4 @@ public function updateScreenLayout(ScreenLayoutData $screenLayoutToUpdate): void
138
110
$ this ->installScreenLayout ($ screenLayoutToUpdate , true );
139
111
}
140
112
}
141
-
142
- public function getScreenLayouts (iterable $ finder , bool $ custom = false ): array
143
- {
144
- $ screenLayouts = [];
145
-
146
- // Validate template json.
147
- $ schemaStorage = new SchemaStorage ();
148
- $ jsonSchemaObject = $ this ->getSchema ();
149
- $ schemaStorage ->addSchema ('file://contentSchema ' , $ jsonSchemaObject );
150
- $ validator = new Validator (new Factory ($ schemaStorage ));
151
-
152
- foreach ($ finder as $ file ) {
153
- $ content = json_decode ((string ) $ file ->getContents ());
154
- $ validator ->validate ($ content , $ jsonSchemaObject );
155
-
156
- if (!$ validator ->isValid ()) {
157
- $ message = 'JSON file ' .$ file ->getFilename ()." does not validate. Violations: \n" ;
158
- foreach ($ validator ->getErrors () as $ error ) {
159
- $ message .= sprintf ("\n[%s] %s " , $ error ['property ' ], $ error ['message ' ]);
160
- }
161
-
162
- throw new \Exception ($ message );
163
- }
164
-
165
- if (!Ulid::isValid ($ content ->id )) {
166
- throw new \Exception ('The Ulid is not valid ' );
167
- }
168
-
169
- $ repository = $ this ->entityManager ->getRepository (ScreenLayout::class);
170
- $ screenLayout = $ repository ->findOneBy (['id ' => Ulid::fromString ($ content ->id )]);
171
-
172
- $ screenLayouts [] = new ScreenLayoutData (
173
- $ content ->id ,
174
- $ content ->title ,
175
- $ custom ? 'Custom ' : 'Core ' ,
176
- $ content ->grid ->rows ,
177
- $ content ->grid ->columns ,
178
- $ screenLayout ,
179
- null !== $ screenLayout ,
180
- $ content ->regions ,
181
- );
182
- }
183
-
184
- return $ screenLayouts ;
185
- }
186
-
187
- /**
188
- * Supplies json schema for validation.
189
- *
190
- * @return mixed
191
- * Json schema
192
- *
193
- * @throws \JsonException
194
- */
195
- public function getSchema (): object
196
- {
197
- $ jsonSchema = <<<'JSON'
198
- {
199
- "$schema": "https://json-schema.org/draft/2020-12/schema",
200
- "$id": "https://os2display.dk/config-schema.json",
201
- "title": "Config file schema",
202
- "description": "Schema for defining config files for templates",
203
- "type": "object",
204
- "properties": {
205
- "id": {
206
- "description": "Ulid of the screen layout",
207
- "type": "string"
208
- },
209
- "title": {
210
- "description": "The title of the screen layout",
211
- "type": "string"
212
- },
213
- "grid": {
214
- "description": "Grid properties",
215
- "type": "object",
216
- "properties": {
217
- "rows": {
218
- "type": "integer",
219
- "description": "Number of rows"
220
- },
221
- "columns": {
222
- "type": "integer",
223
- "description": "Number of columns"
224
- }
225
- }
226
- },
227
- "regions": {
228
- "description": "The regions of the screen layout",
229
- "type": "array",
230
- "items": {
231
- "type": "object",
232
- "description": "Region",
233
- "properties": {
234
- "id": {
235
- "description": "Ulid of the region",
236
- "type": "string"
237
- },
238
- "title": {
239
- "description": "The title of the region",
240
- "type": "string"
241
- },
242
- "gridArea": {
243
- "description": "Grid area value",
244
- "type": "array",
245
- "items": {
246
- "type": "string"
247
- }
248
- }
249
- }
250
- }
251
- }
252
- },
253
- "required": ["id", "title", "grid", "regions"]
254
- }
255
- JSON;
256
-
257
- return json_decode ($ jsonSchema , false , 512 , JSON_THROW_ON_ERROR );
258
- }
259
113
}
0 commit comments