|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Pvg\Application\Configuration; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Pvg\Application\Configuration\ValueObject\EventListenerConfig; |
|
9
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
10
|
|
|
use Symfony\Component\Config\FileLocator; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; |
|
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
13
|
|
|
|
|
14
|
|
|
class ConfigReader |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var FileLocator */ |
|
17
|
|
|
private $fileLocator; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
private $configPath; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
private $configArray; |
|
24
|
|
|
|
|
25
|
|
|
/** @var LoggerInterface */ |
|
26
|
|
|
private $logger; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $eventListenerConfigPath; |
|
30
|
|
|
|
|
31
|
|
|
/** @var ContainerAwareEventDispatcher */ |
|
32
|
|
|
private $dispatcher; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create an global application config from given file. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
FileLocator $fileLocator, |
|
39
|
|
|
string $applicationConfigPath, |
|
40
|
|
|
string $eventListenerConfigPath, |
|
41
|
|
|
ContainerAwareEventDispatcher $dispatcher, |
|
42
|
|
|
LoggerInterface $logger |
|
43
|
|
|
) { |
|
44
|
|
|
$this->fileLocator = $fileLocator; |
|
45
|
|
|
$this->configPath = $applicationConfigPath; |
|
46
|
|
|
$this->logger = $logger; |
|
47
|
|
|
$this->eventListenerConfigPath = $eventListenerConfigPath; |
|
48
|
|
|
$this->dispatcher = $dispatcher; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function init() : void |
|
52
|
|
|
{ |
|
53
|
|
|
if (empty($this->configArray)) { |
|
54
|
|
|
$this->configArray = $this->getYamlFileContent($this->configPath); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$eventListenerConfig = $this->getYamlFileContent($this->eventListenerConfigPath); |
|
58
|
|
|
$this->initEventDispatcher(EventListenerConfig::createFromConfigArray($eventListenerConfig)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function get() : array |
|
62
|
|
|
{ |
|
63
|
|
|
if (empty($this->configArray)) { |
|
64
|
|
|
throw new InvalidConfigurationException('Invalid configuration: empty'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->configArray; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @throws \Symfony\Component\Config\Exception\FileLocatorFileNotFoundException |
|
72
|
|
|
* @throws \Symfony\Component\Yaml\Exception\ParseException |
|
73
|
|
|
* @throws \InvalidArgumentException |
|
74
|
|
|
*/ |
|
75
|
|
|
private function getYamlFileContent(string $path) : array |
|
76
|
|
|
{ |
|
77
|
|
|
$fullPath = $this->fileLocator->locate($path); |
|
78
|
|
|
$this->logger->info('Reading config from {path}', ['path' => $fullPath]); |
|
79
|
|
|
|
|
80
|
|
|
return Yaml::parse(file_get_contents($fullPath)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param EventListenerConfig[] $eventListenerConfig |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \InvalidArgumentException |
|
87
|
|
|
*/ |
|
88
|
|
|
private function initEventDispatcher(array $eventListenerConfig) : void |
|
89
|
|
|
{ |
|
90
|
|
|
/** @var EventListenerConfig $singleListenerConfig */ |
|
91
|
|
|
foreach ($eventListenerConfig as $singleListenerConfig) { |
|
92
|
|
|
$this->dispatcher->addListenerService( |
|
93
|
|
|
$singleListenerConfig->event(), |
|
94
|
|
|
[ |
|
95
|
|
|
$singleListenerConfig->listenerServiceId(), |
|
96
|
|
|
$singleListenerConfig->action(), |
|
97
|
|
|
] |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|