1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_menu\MetaSource; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
7
|
|
|
use kalanis\kw_files\FilesException; |
8
|
|
|
use kalanis\kw_files\Traits\TToString; |
9
|
|
|
use kalanis\kw_menu\Interfaces\IMetaFileParser; |
10
|
|
|
use kalanis\kw_menu\Interfaces\IMetaSource; |
11
|
|
|
use kalanis\kw_menu\Interfaces\IMNTranslations; |
12
|
|
|
use kalanis\kw_menu\Menu\Menu; |
13
|
|
|
use kalanis\kw_menu\MenuException; |
14
|
|
|
use kalanis\kw_menu\Traits\TLang; |
15
|
|
|
use kalanis\kw_paths\PathsException; |
16
|
|
|
use kalanis\kw_paths\Stuff; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Files |
21
|
|
|
* @package kalanis\kw_menu\MetaSource |
22
|
|
|
* Data source is in passed Files package |
23
|
|
|
*/ |
24
|
|
|
class Files implements IMetaSource |
25
|
|
|
{ |
26
|
|
|
use TLang; |
27
|
|
|
use TToString; |
28
|
|
|
|
29
|
|
|
/** @var string[] */ |
30
|
|
|
protected array $key = []; |
31
|
|
|
protected CompositeAdapter $files; |
32
|
|
|
protected IMetaFileParser $parser; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param CompositeAdapter $files |
36
|
|
|
* @param IMetaFileParser $parser |
37
|
|
|
* @param IMNTranslations|null $lang |
38
|
|
|
* @param string[] $metaKey |
39
|
|
|
*/ |
40
|
5 |
|
public function __construct(CompositeAdapter $files, IMetaFileParser $parser, ?IMNTranslations $lang = null, array $metaKey = []) |
41
|
|
|
{ |
42
|
5 |
|
$this->setMnLang($lang); |
43
|
5 |
|
$this->files = $files; |
44
|
5 |
|
$this->parser = $parser; |
45
|
5 |
|
$this->key = $metaKey; |
46
|
5 |
|
} |
47
|
|
|
|
48
|
1 |
|
public function setSource(array $metaPath): void |
49
|
|
|
{ |
50
|
1 |
|
$this->key = $metaPath; |
51
|
1 |
|
} |
52
|
|
|
|
53
|
2 |
|
public function exists(): bool |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
2 |
|
return $this->files->exists($this->key); |
57
|
1 |
|
} catch (FilesException | PathsException $ex) { |
58
|
1 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
public function load(): Menu |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
2 |
|
return $this->parser->unpack($this->toString( |
66
|
2 |
|
Stuff::arrayToPath($this->key), $this->files->readFile($this->key) |
67
|
|
|
)); |
68
|
1 |
|
} catch (FilesException | PathsException $ex) { |
69
|
1 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function save(Menu $content): bool |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
2 |
|
return $this->files->saveFile($this->key, $this->parser->pack($content)); |
77
|
1 |
|
} catch (FilesException | PathsException $ex) { |
78
|
1 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|