1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\ComposerConfigManager\Infrastructure\Loader; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
5
|
|
|
use Symfony\Component\Finder\Finder; |
6
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
7
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
8
|
|
|
use Yoanm\ComposerConfigManager\Application\Loader\ConfigurationLoaderInterface; |
9
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Configuration; |
10
|
|
|
use Yoanm\ComposerConfigManager\Infrastructure\Serializer\Encoder\ComposerEncoder; |
11
|
|
|
use Yoanm\ComposerConfigManager\Infrastructure\Writer\ConfigurationWriter; |
12
|
|
|
|
13
|
|
|
class ConfigurationLoader implements ConfigurationLoaderInterface |
14
|
|
|
{ |
15
|
|
|
/** @var Finder */ |
16
|
|
|
private $finder; |
17
|
|
|
/** @var SerializerInterface */ |
18
|
|
|
private $serializer; |
19
|
|
|
|
20
|
2 |
|
public function __construct(Finder $finder, SerializerInterface $serializer) |
21
|
|
|
{ |
22
|
2 |
|
$this->finder = $finder; |
23
|
2 |
|
$this->serializer = $serializer; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
2 |
|
public function fromPath($path) |
30
|
|
|
{ |
31
|
2 |
|
$finder = $this->finder |
32
|
2 |
|
->in($path) |
33
|
2 |
|
->files() |
34
|
2 |
|
->name(ConfigurationWriter::FILENAME) |
35
|
2 |
|
->depth(0); |
36
|
|
|
|
37
|
|
|
/** @var SplFileInfo|null $file */ |
38
|
2 |
|
$file = null; |
39
|
2 |
|
foreach ($finder as $result) { |
40
|
1 |
|
$file = $result; |
41
|
1 |
|
break; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
2 |
|
if (null === $file) { |
45
|
1 |
|
throw new FileNotFoundException( |
46
|
1 |
|
null, |
47
|
1 |
|
0, |
48
|
1 |
|
null, |
49
|
1 |
|
sprintf( |
50
|
1 |
|
'%s/%s', |
51
|
1 |
|
trim($path, '/'), |
52
|
|
|
ConfigurationWriter::FILENAME |
53
|
1 |
|
) |
54
|
1 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return $this->deserialize($file->getContents()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function fromFilePath($filePath) |
64
|
|
|
{ |
65
|
|
|
if (!is_file($filePath)) { |
66
|
|
|
throw new FileNotFoundException( |
67
|
|
|
null, |
68
|
|
|
0, |
69
|
|
|
null, |
70
|
|
|
$filePath |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
return $this->deserialize(file_get_contents($filePath)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $serializedConfiguration |
78
|
|
|
* |
79
|
|
|
* @return Configuration |
80
|
|
|
*/ |
81
|
1 |
|
protected function deserialize($serializedConfiguration) |
82
|
|
|
{ |
83
|
1 |
|
return $this->serializer->deserialize($serializedConfiguration, Configuration::class, ComposerEncoder::FORMAT); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|