Test Failed
Branch master (33f0fa)
by Divine Niiquaye
02:33
created

CoreExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI\Extensions;
19
20
use Rade\DI\AbstractContainer;
21
use Rade\DI\Definition;
22
use Rade\DI\Services\AliasedInterface;
23
use Rade\DI\Services\DependenciesInterface;
24
use Rade\Handler\EventHandler;
25
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
26
use Symfony\Component\Config\Definition\ConfigurationInterface;
27
28
/**
29
 * Rade Core Extension.
30
 *
31
 * @author Divine Niiquaye Ibok <[email protected]>
32
 */
33
class CoreExtension implements AliasedInterface, ConfigurationInterface, DependenciesInterface, ExtensionInterface
34
{
35
    private string $rootDir;
36
37
    public function __construct(string $rootDir)
38
    {
39
        $this->rootDir = \rtrim($rootDir, \DIRECTORY_SEPARATOR);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getAlias(): string
46
    {
47
        return 'core';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getConfigTreeBuilder(): TreeBuilder
54
    {
55
        $treeBuilder = new TreeBuilder($this->getAlias());
56
57
        $treeBuilder->getRootNode()
58
            ->children()
59
                ->scalarNode('events_dispatcher')->end()
60
            ->end()
61
        ;
62
63
        return $treeBuilder;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function dependencies(): array
70
    {
71
        return [[ConfigExtension::class, [$this->rootDir]], RoutingExtension::class];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function register(AbstractContainer $container, array $configs = []): void
78
    {
79
        if (!$container->has('events.dispatcher')) {
80
            $eventsDispatcher = $configs['events_dispatcher'] ?? EventHandler::class;
81
82
            if ($container->has($eventsDispatcher)) {
83
                $container->alias('events.dispatcher', $eventsDispatcher);
84
            } else {
85
                $container->autowire('events.dispatcher', new Definition($eventsDispatcher));
86
            }
87
        }
88
    }
89
}
90