1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Nopolabs\Yabot\Plugin\PluginInterface; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
10
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
11
|
|
|
|
12
|
|
|
class YabotContainer extends ContainerBuilder |
13
|
|
|
{ |
14
|
|
|
const YABOT_ID = 'yabot'; |
15
|
|
|
const YABOT_PLUGIN_TAG = 'yabot.plugin'; |
16
|
|
|
|
17
|
|
|
public function __construct($servicesPath = __DIR__.'/../config/yabot.xml') |
18
|
|
|
{ |
19
|
|
|
parent::__construct(); |
20
|
|
|
$this->load($servicesPath); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function load($file, $type = null) |
24
|
|
|
{ |
25
|
|
|
$type = $type ?? pathinfo($file)['extension']; |
26
|
|
|
|
27
|
|
|
if ($type === 'xml') { |
28
|
|
|
$this->loadXmlFile($file); |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($type === 'yml') { |
33
|
|
|
$this->loadYamlFile($file); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
throw new Exception("Do not know how to load $file"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function overrideParameters(array $parameters) |
40
|
|
|
{ |
41
|
|
|
$this->getParameterBag()->add($parameters); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getYabot() : Yabot |
45
|
|
|
{ |
46
|
|
|
/** @var Yabot $yabot */ |
47
|
|
|
$yabot = $this->get(self::YABOT_ID); |
48
|
|
|
|
49
|
|
|
$plugins = $this->getTaggedPlugins(self::YABOT_PLUGIN_TAG); |
50
|
|
|
|
51
|
|
|
$this->initPlugins($plugins); |
52
|
|
|
|
53
|
|
|
$yabot->init($plugins); |
54
|
|
|
|
55
|
|
|
return $yabot; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getParameterOrDefault($name, array $default = []) : array |
59
|
|
|
{ |
60
|
|
|
return $this->hasParameter($name) ? $this->getParameter($name) : $default; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getTaggedPlugins($tag) : array |
64
|
|
|
{ |
65
|
|
|
return array_reduce( |
66
|
|
|
array_keys($this->findTaggedServiceIds($tag)), |
67
|
|
|
function(array $plugins, string $pluginId) { |
68
|
|
|
$plugins[$pluginId] = $this->get($pluginId); |
69
|
|
|
return $plugins; |
70
|
|
|
}, |
71
|
|
|
[] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function initPlugins(array $plugins) |
76
|
|
|
{ |
77
|
|
|
array_map( |
78
|
|
|
function(string $pluginId, PluginInterface $plugin) { |
79
|
|
|
$config = $this->getParameterOrDefault($pluginId); |
80
|
|
|
$plugin->init($pluginId, $config); |
81
|
|
|
}, |
82
|
|
|
array_keys($plugins), $plugins |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function loadXmlFile($file) |
87
|
|
|
{ |
88
|
|
|
$loader = new XmlFileLoader($this, new FileLocator()); |
89
|
|
|
$loader->load($file); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function loadYamlFile($file) |
93
|
|
|
{ |
94
|
|
|
$loader = new YamlFileLoader($this, new FileLocator()); |
95
|
|
|
$loader->load($file); |
96
|
|
|
} |
97
|
|
|
} |