-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndexMetadataRegistry.php
146 lines (116 loc) · 3.53 KB
/
IndexMetadataRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace MeiliSearchBundle\Metadata;
use MeiliSearchBundle\Exception\InvalidArgumentException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Serializer\SerializerInterface;
use function count;
use function file_get_contents;
use function sprintf;
use function strtr;
use function sys_get_temp_dir;
/**
* @author Guillaume Loulier <[email protected]>
*/
final class IndexMetadataRegistry implements IndexMetadataRegistryInterface
{
private const FILE_PATH_PREFIX = '_ms_bundle_';
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var string
*/
private $path;
/**
* @var SerializerInterface
*/
private $serializer;
public function __construct(Filesystem $filesystem, SerializerInterface $serializer, ?string $path = null)
{
$this->filesystem = $filesystem;
$this->serializer = $serializer;
$this->path = $path ?: sys_get_temp_dir();
}
public function add(string $index, IndexMetadataInterface $metadata): void
{
if ($this->has($index)) {
$this->override($index, $metadata);
return;
}
$data = $this->serializer->serialize($metadata, 'json');
$this->filesystem->dumpFile(sprintf('%s/%s.json', $this->getFilePath(), $index), $data);
}
public function override(string $index, IndexMetadataInterface $newConfiguration): void
{
if (!$this->has($index)) {
$this->add($index, $newConfiguration);
return;
}
$this->remove($index);
$this->add($index, $newConfiguration);
}
public function get(string $index): IndexMetadataInterface
{
if (!$this->has($index)) {
throw new InvalidArgumentException('The desired index does not exist');
}
return $this->serializer->deserialize(
file_get_contents(sprintf('%s/%s.json', $this->getFilePath(), $index)),
IndexMetadata::class,
'json'
);
}
public function remove(string $index): void
{
if (!$this->has($index)) {
throw new InvalidArgumentException('The desired index does not exist');
}
$this->filesystem->remove(sprintf('%s/%s.json', $this->getFilePath(), $index));
}
public function has(string $index): bool
{
return $this->filesystem->exists(sprintf('%s/%s.json', $this->getFilePath(), $index));
}
public function clear(): void
{
if (!$this->filesystem->exists($this->getFilePath())) {
return;
}
$finder = new Finder();
$finder->files()->in($this->getFilePath());
foreach ($finder as $file) {
$this->remove(strtr($file->getFilename(), ['.json' => '']));
}
}
/**
* {@inheritdoc}
*/
public function toArray(): array
{
if (!$this->filesystem->exists($this->getFilePath())) {
return [];
}
$finder = new Finder();
$finder->files()->in($this->getFilePath());
$list = [];
foreach ($finder as $file) {
$index = strtr($file->getFilename(), ['.json' => '']);
$list[$index] = $this->get($index);
}
return $list;
}
/**
* {@inheritdoc}
*/
public function count(): int
{
return count($this->toArray());
}
private function getFilePath(): string
{
return sprintf('%s/%s', $this->path, self::FILE_PATH_PREFIX);
}
}