YabotContainer::getYabot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
            return;
35
        }
36
37
        throw new Exception("Do not know how to load $file");
38
    }
39
40
    public function overrideParameters(array $parameters)
41
    {
42
        $this->getParameterBag()->add($parameters);
43
    }
44
45
    public function getYabot() : Yabot
46
    {
47
        /** @var Yabot $yabot */
48
        $yabot = $this->get(self::YABOT_ID);
49
50
        $plugins = $this->getTaggedPlugins(self::YABOT_PLUGIN_TAG);
51
52
        $this->initPlugins($plugins);
53
54
        $yabot->init($plugins);
55
56
        return $yabot;
57
    }
58
59
    public function getParameterOrDefault($name, array $default = []) : array
60
    {
61
        return $this->hasParameter($name) ? $this->getParameter($name) : $default;
62
    }
63
64
    private function getTaggedPlugins($tag) : array
65
    {
66
        return array_reduce(
67
            array_keys($this->findTaggedServiceIds($tag)),
68
            function(array $plugins, string $pluginId) {
69
                $plugins[$pluginId] = $this->get($pluginId);
70
                return $plugins;
71
            },
72
            []
73
        );
74
    }
75
76
    private function initPlugins(array $plugins)
77
    {
78
        array_map(
79
            function(string $pluginId, PluginInterface $plugin) {
80
                $config = $this->getParameterOrDefault($pluginId);
81
                $plugin->init($pluginId, $config);
82
            },
83
            array_keys($plugins), $plugins
84
        );
85
    }
86
87
    protected function loadXmlFile($file)
88
    {
89
        $loader = new XmlFileLoader($this, new FileLocator());
90
        $loader->load($file);
91
    }
92
93
    protected function loadYamlFile($file)
94
    {
95
        $loader = new YamlFileLoader($this, new FileLocator());
96
        $loader->load($file);
97
    }
98
}