-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathDatastore.php
80 lines (67 loc) · 2.14 KB
/
Datastore.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
<?php
declare(strict_types=1);
namespace Acquia\Cli\DataStore;
use Acquia\Cli\Exception\AcquiaCliException;
use Dflydev\DotAccessData\Data;
use Dflydev\DotAccessData\Exception\MissingPathException;
use Grasmash\Expander\Expander;
use Grasmash\Expander\Stringifier;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Filesystem\Filesystem;
abstract class Datastore implements DataStoreInterface
{
protected Data $data;
protected Filesystem $fileSystem;
public string $filepath;
protected Expander $expander;
public function __construct(string $path)
{
$this->fileSystem = new Filesystem();
$this->filepath = $path;
$this->expander = new Expander();
$this->expander->setStringifier(new Stringifier());
$this->data = new Data();
}
public function set(string $key, mixed $value): void
{
$this->data->set($key, $value);
$this->dump();
}
public function get(string $key): mixed
{
try {
return $this->data->get($key);
} catch (MissingPathException) {
return null;
}
}
public function remove(string $key): void
{
$this->data->remove($key);
$this->dump();
}
public function exists(string $key): bool
{
return $this->data->has($key);
}
/**
* @param string $path Path to the datastore on disk.
* @return array<mixed>
*/
protected function processConfig(array $config, ConfigurationInterface $definition, string $path): array
{
try {
return (new Processor())->processConfiguration(
$definition,
[$definition->getName() => $config],
);
} catch (InvalidConfigurationException $e) {
throw new AcquiaCliException(
'Configuration file at the following path contains invalid keys: {path} {error}',
['path' => $path, 'error' => $e->getMessage()]
);
}
}
}