1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dandelion\Configuration; |
6
|
|
|
|
7
|
|
|
use Dandelion\Exception\InvalidTypeException; |
8
|
|
|
use Dandelion\Exception\IOException; |
9
|
|
|
use Dandelion\Filesystem\FilesystemInterface; |
10
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
11
|
|
|
|
12
|
|
|
use function is_object; |
13
|
|
|
|
14
|
|
|
class ConfigurationLoader implements ConfigurationLoaderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Dandelion\Configuration\Configuration|null |
18
|
|
|
*/ |
19
|
|
|
protected $configuration; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
protected $rawConfiguration; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Dandelion\Configuration\ConfigurationFinderInterface |
28
|
|
|
*/ |
29
|
|
|
protected $configurationFinder; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Dandelion\Filesystem\FilesystemInterface |
33
|
|
|
*/ |
34
|
|
|
protected $filesystem; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Symfony\Component\Serializer\SerializerInterface |
38
|
|
|
*/ |
39
|
|
|
protected $serializer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \Dandelion\Configuration\ConfigurationFinderInterface $configurationFinder |
43
|
|
|
* @param \Dandelion\Filesystem\FilesystemInterface $filesystem |
44
|
|
|
* @param \Symfony\Component\Serializer\SerializerInterface $serializer |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
ConfigurationFinderInterface $configurationFinder, |
48
|
|
|
FilesystemInterface $filesystem, |
49
|
|
|
SerializerInterface $serializer |
50
|
|
|
) { |
51
|
|
|
$this->configurationFinder = $configurationFinder; |
52
|
|
|
$this->filesystem = $filesystem; |
53
|
|
|
$this->serializer = $serializer; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return \Dandelion\Configuration\Configuration |
58
|
|
|
* |
59
|
|
|
* @throws \Dandelion\Exception\InvalidTypeException |
60
|
|
|
* @throws \Dandelion\Exception\IOException |
61
|
|
|
*/ |
62
|
|
|
public function load(): Configuration |
63
|
|
|
{ |
64
|
|
|
if ($this->configuration !== null) { |
65
|
|
|
return $this->configuration; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$configurationFileContent = $this->loadRaw(); |
69
|
|
|
|
70
|
|
|
$configuration = $this->serializer->deserialize( |
71
|
|
|
$configurationFileContent, |
72
|
|
|
Configuration::class, |
73
|
|
|
'json' |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
if (!is_object($configuration) || !($configuration instanceof Configuration)) { |
77
|
|
|
throw new InvalidTypeException('Invalid type of deserialized data.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->configuration = $configuration; |
81
|
|
|
|
82
|
|
|
return $this->configuration; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
* |
88
|
|
|
* @throws \Dandelion\Exception\IOException |
89
|
|
|
*/ |
90
|
|
|
public function loadRaw(): string |
91
|
|
|
{ |
92
|
|
|
if ($this->rawConfiguration !== null) { |
93
|
|
|
return $this->rawConfiguration; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$configurationFile = $this->configurationFinder->find(); |
97
|
|
|
$realPathToConfigurationFile = $configurationFile->getRealPath(); |
98
|
|
|
|
99
|
|
|
if ($realPathToConfigurationFile === false) { |
100
|
|
|
throw new IOException('Configuration file does not exists.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->rawConfiguration = $this->filesystem->readFile($realPathToConfigurationFile); |
104
|
|
|
|
105
|
|
|
return $this->rawConfiguration; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|