Test Failed
Pull Request — master (#37)
by Divine Niiquaye
11:41
created

ExtensionTrait::getExtensionConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 2
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 2021 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\Traits;
19
20
use Rade\DI\Extensions\ExtensionBuilder;
21
use Rade\DI\Extensions\ExtensionInterface;
22
23
/**
24
 * This trait adds container extensions builder to container.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
trait ExtensionTrait
29
{
30
    /**
31
     * Get the extension builder extensions.
32
     *
33
     * @return array<int,ExtensionInterface>
34
     */
35
    public function getExtensions(): array
36
    {
37
        $extensionBuilder = $this->getExtensionBuilder();
38
39
        return null !== $extensionBuilder ? $extensionBuilder->getExtensions() : [];
40
    }
41
42
    /**
43
     * Get the registered extension from builder.
44
     *
45
     * @param string $extensionName The extension class name or its alias
46
     */
47
    public function getExtension(string $extensionName): ?ExtensionInterface
48
    {
49
        $extensionBuilder = $this->getExtensionBuilder();
50
51
        return null !== $extensionBuilder ? $extensionBuilder->get($extensionName) : $extensionBuilder;
52
    }
53
54
    /**
55
     * Get the registered extension config from builder.
56
     *
57
     * @param string $extensionName The extension class name or its alias
58
     *
59
     * @return array<string,mixed>
60
     */
61
    public function getExtensionConfig(string $extensionName, string $parent = null): array
62
    {
63
        $extensionBuilder = $this->getExtensionBuilder();
64
65
        return null !== $extensionBuilder ? $extensionBuilder->getConfig($extensionName, $parent) : [];
66
    }
67
68
    /**
69
     * Checks if an extension is registered from builder.
70
     *
71
     * @param string $extensionName The extension class name or its alias
72
     */
73
    public function hasExtension(string $extensionName): bool
74
    {
75
        $extensionBuilder = $this->getExtensionBuilder();
76
77
        return null !== $extensionBuilder ? $extensionBuilder->has($extensionName) : false;
78
    }
79
80
    /**
81
     * Returns the container's extensions builder.
82
     */
83
    public function getExtensionBuilder(): ?ExtensionBuilder
84
    {
85
        return $this->definitions[ExtensionInterface::BUILDER] ?? null;
86
    }
87
}
88