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\AppBuilder; |
21
|
|
|
use Rade\DI\AbstractContainer; |
22
|
|
|
use Rade\DI\Definition; |
23
|
|
|
use Rade\DI\Definitions\Statement; |
24
|
|
|
use Rade\DI\Loader\{ClosureLoader, DirectoryLoader, GlobFileLoader, PhpFileLoader, YamlFileLoader}; |
25
|
|
|
use Rade\DI\Services\AliasedInterface; |
26
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
27
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
28
|
|
|
use Symfony\Component\Config\FileLocator; |
29
|
|
|
use Symfony\Component\Config\Loader\LoaderResolver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Symfony's Config component extension. |
33
|
|
|
* |
34
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class ConfigExtension implements AliasedInterface, ConfigurationInterface, ExtensionInterface |
37
|
|
|
{ |
38
|
|
|
private string $rootDir; |
39
|
|
|
|
40
|
|
|
public function __construct(string $rootDir) |
41
|
|
|
{ |
42
|
|
|
$this->rootDir = \rtrim($rootDir, \DIRECTORY_SEPARATOR); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function getAlias(): string |
49
|
|
|
{ |
50
|
|
|
return 'config'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function getConfigTreeBuilder(): TreeBuilder |
57
|
|
|
{ |
58
|
|
|
$treeBuilder = new TreeBuilder($this->getAlias()); |
59
|
|
|
|
60
|
|
|
$treeBuilder->getRootNode() |
61
|
|
|
->addDefaultsIfNotSet() |
|
|
|
|
62
|
|
|
->children() |
63
|
|
|
->scalarNode('locale')->defaultValue('en')->end() |
64
|
|
|
->booleanNode('debug')->defaultFalse()->end() |
65
|
|
|
->arrayNode('paths') |
66
|
|
|
->prototype('scalar')->isRequired()->cannotBeEmpty()->end() |
67
|
|
|
->end() |
68
|
|
|
->arrayNode('loaders') |
69
|
|
|
->prototype('scalar')->defaultValue([])->end() |
70
|
|
|
->end() |
71
|
|
|
->end() |
72
|
|
|
; |
73
|
|
|
|
74
|
|
|
return $treeBuilder; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function register(AbstractContainer $container, array $configs = []): void |
81
|
|
|
{ |
82
|
|
|
// The default configs ... |
83
|
|
|
$container->parameters['locale'] = $configs['locale'] ?? null; |
84
|
|
|
$container->parameters['debug'] = $configs['debug'] ?? false; |
85
|
|
|
$container->parameters['project_dir'] = $this->rootDir; |
86
|
|
|
|
87
|
|
|
// Configurations ... |
88
|
|
|
$configLoaders = [...$configs['loaders'], ...[PhpFileLoader::class, YamlFileLoader::class, ClosureLoader::class, DirectoryLoader::class, GlobFileLoader::class]]; |
89
|
|
|
|
90
|
|
|
if ($container instanceof AppBuilder) { |
91
|
|
|
$builderResolver = new LoaderResolver(); |
92
|
|
|
$fileLocator = new FileLocator(\array_map([$container, 'parameter'], $configs['paths'])); |
93
|
|
|
|
94
|
|
|
foreach ($configLoaders as $builderLoader) { |
95
|
|
|
$builderResolver->addLoader(new $builderLoader($container, $fileLocator)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$container->set('builder.loader_resolver', $builderResolver); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ($configLoaders as &$configLoader) { |
102
|
|
|
$configLoader = new Statement($configLoader); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$container->autowire('config.loader_locator', new Definition(FileLocator::class, [\array_map([$container, 'parameter'], $configs['paths'])])); |
106
|
|
|
$container->set('config.loader_resolver', new Definition(LoaderResolver::class, [$configLoaders])); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|