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