|
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\Loader; |
|
19
|
|
|
|
|
20
|
|
|
use Rade\DI\{AbstractContainer, Container, ContainerBuilder}; |
|
21
|
|
|
use Rade\DI\Services\ServiceProviderInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This class delegates container service providers. |
|
25
|
|
|
* Setting service provider's config, keys should always begin with service provider's class name. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class ProviderLoader |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var ServiceProviderInterface[] */ |
|
32
|
|
|
private array $providers; |
|
33
|
|
|
|
|
34
|
|
|
/** @var array<string,mixed> */ |
|
35
|
|
|
private array $config; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ServiceProviderInterface[] $providers |
|
39
|
|
|
* @param array<string,mixed> $config |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function __construct(array $providers, array $config = []) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$this->providers = $providers; |
|
44
|
2 |
|
$this->config = $config; |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Loads service providers will one configuration. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Container|ContainerBuilder $container |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function load(AbstractContainer $container): void |
|
53
|
|
|
{ |
|
54
|
2 |
|
foreach ($this->providers as $provider) { |
|
55
|
2 |
|
$container->register($provider, $this->config[\get_class($provider)] ?? []); |
|
56
|
|
|
} |
|
57
|
2 |
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|