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_menu\Interfaces\IMetaFileParser; |
9
|
|
|
use kalanis\kw_menu\Interfaces\IMetaSource; |
10
|
|
|
use kalanis\kw_menu\Interfaces\IMNTranslations; |
11
|
|
|
use kalanis\kw_menu\Menu\Menu; |
12
|
|
|
use kalanis\kw_menu\MenuException; |
13
|
|
|
use kalanis\kw_menu\Traits\TLang; |
14
|
|
|
use kalanis\kw_paths\PathsException; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Files |
19
|
|
|
* @package kalanis\kw_menu\MetaSource |
20
|
|
|
* Data source is in passed Files package |
21
|
|
|
*/ |
22
|
|
|
class Files implements IMetaSource |
23
|
|
|
{ |
24
|
|
|
use TLang; |
25
|
|
|
|
26
|
|
|
/** @var string[] */ |
27
|
|
|
protected $key = []; |
28
|
|
|
/** @var CompositeAdapter */ |
29
|
|
|
protected $files = null; |
30
|
|
|
/** @var IMetaFileParser */ |
31
|
|
|
protected $parser = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param CompositeAdapter $files |
35
|
|
|
* @param IMetaFileParser $parser |
36
|
|
|
* @param IMNTranslations|null $lang |
37
|
|
|
* @param string[] $metaKey |
38
|
|
|
*/ |
39
|
8 |
|
public function __construct(CompositeAdapter $files, IMetaFileParser $parser, ?IMNTranslations $lang = null, array $metaKey = []) |
40
|
|
|
{ |
41
|
8 |
|
$this->setMnLang($lang); |
42
|
8 |
|
$this->files = $files; |
43
|
8 |
|
$this->parser = $parser; |
44
|
8 |
|
$this->key = $metaKey; |
45
|
8 |
|
} |
46
|
|
|
|
47
|
4 |
|
public function setSource(array $metaPath): void |
48
|
|
|
{ |
49
|
4 |
|
$this->key = $metaPath; |
50
|
4 |
|
} |
51
|
|
|
|
52
|
5 |
|
public function exists(): bool |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
5 |
|
return $this->files->exists($this->key); |
56
|
1 |
|
} catch (FilesException | PathsException $ex) { |
57
|
1 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
public function load(): Menu |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
5 |
|
return $this->parser->unpack($this->toString($this->files->readFile($this->key))); |
65
|
1 |
|
} catch (FilesException | PathsException $ex) { |
66
|
1 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string|resource $content |
72
|
|
|
* @throws MenuException |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
4 |
|
protected function toString($content): string |
76
|
|
|
{ |
77
|
4 |
|
if (is_resource($content)) { |
78
|
3 |
|
rewind($content); |
79
|
3 |
|
$data = stream_get_contents($content, -1, 0); |
80
|
3 |
|
if (false === $data) { |
81
|
|
|
// @codeCoverageIgnoreStart |
82
|
|
|
// must die something with stream reading |
83
|
|
|
throw new MenuException($this->getMnLang()->mnCannotOpen()); |
84
|
|
|
} |
85
|
|
|
// @codeCoverageIgnoreEnd |
86
|
3 |
|
return strval($data); |
87
|
|
|
} else { |
88
|
1 |
|
return strval($content); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function save(Menu $content): bool |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
2 |
|
return $this->files->saveFile($this->key, $this->parser->pack($content)); |
96
|
1 |
|
} catch (FilesException | PathsException $ex) { |
97
|
1 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|