1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RunOpenCode\Bundle\DoctrineNamingStrategy\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension as BaseExtension; |
11
|
|
|
use Symfony\Component\Config\FileLocator; |
12
|
|
|
|
13
|
|
|
final class Extension extends BaseExtension |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
6 |
|
public function getAlias(): string |
19
|
|
|
{ |
20
|
6 |
|
return 'runopencode_doctrine_naming_strategy'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
6 |
|
public function getNamespace(): string |
27
|
|
|
{ |
28
|
6 |
|
return 'http://www.runopencode.com/xsd-schema/doctrine-naming-strategy-bundle'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
2 |
|
public function getXsdValidationBasePath(): string |
35
|
|
|
{ |
36
|
2 |
|
return __DIR__ . '/../Resources/config/schema'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array<array-key, mixed> $config |
41
|
|
|
* |
42
|
|
|
* @throws \Exception |
43
|
|
|
*/ |
44
|
3 |
|
public function load(array $config, ContainerBuilder $container): void |
45
|
|
|
{ |
46
|
3 |
|
$configuration = new Configuration(); |
47
|
3 |
|
$config = $this->processConfiguration($configuration, $config); |
48
|
3 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
49
|
|
|
|
50
|
3 |
|
$loader->load('services.xml'); |
51
|
|
|
|
52
|
3 |
|
$this->configureUnderscoredBundlePrefixNamer($container, $config); |
53
|
3 |
|
$this->configureUnderscoredClassNamespacePrefixNamer($container, $config); |
54
|
3 |
|
$this->configureNamerCollection($container, $config); |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Configure 'runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix' naming strategy. |
59
|
|
|
* |
60
|
|
|
* @param array<array-key, mixed> $config |
61
|
|
|
*/ |
62
|
3 |
View Code Duplication |
private function configureUnderscoredBundlePrefixNamer(ContainerBuilder $container, array $config): void |
|
|
|
|
63
|
|
|
{ |
64
|
3 |
|
if (!isset($config['underscored_bundle_prefix'])) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
if (!$container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix')) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** @psalm-suppress MissingThrowsDocblock */ |
73
|
3 |
|
$definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix'); |
74
|
3 |
|
$config['underscored_bundle_prefix']['case'] = ('uppercase' === $config['underscored_bundle_prefix']['case']) ? CASE_UPPER : CASE_LOWER; |
75
|
3 |
|
$args = $definition->getArguments(); |
76
|
3 |
|
$args[1] = $config['underscored_bundle_prefix']; |
77
|
|
|
|
78
|
3 |
|
$definition->setArguments($args); |
79
|
3 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Configure 'runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix' naming strategy. |
83
|
|
|
* |
84
|
|
|
* @param array<array-key, mixed> $config |
85
|
|
|
*/ |
86
|
3 |
View Code Duplication |
private function configureUnderscoredClassNamespacePrefixNamer(ContainerBuilder $container, array $config): void |
|
|
|
|
87
|
|
|
{ |
88
|
3 |
|
if (!isset($config['underscored_class_namespace_prefix'])) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
if (!$container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix')) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @psalm-suppress MissingThrowsDocblock */ |
97
|
3 |
|
$definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix'); |
98
|
3 |
|
$config['underscored_class_namespace_prefix']['case'] = ('uppercase' === $config['underscored_class_namespace_prefix']['case']) ? CASE_UPPER : CASE_LOWER; |
99
|
3 |
|
$args = $definition->getArguments(); |
100
|
3 |
|
$args[0] = $config['underscored_class_namespace_prefix']; |
101
|
|
|
|
102
|
3 |
|
$definition->setArguments($args); |
103
|
3 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Configure 'runopencode.doctrine.orm.naming_strategy.underscored_namer_collection' naming strategy. |
107
|
|
|
* |
108
|
|
|
* @param array<array-key, mixed> $config |
109
|
|
|
*/ |
110
|
3 |
|
private function configureNamerCollection(ContainerBuilder $container, array $config): void |
111
|
|
|
{ |
112
|
3 |
|
if (!isset($config['underscored_namer_collection'])) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
if (!$container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_namer_collection')) { |
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** @psalm-suppress MissingThrowsDocblock */ |
121
|
3 |
|
$definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_namer_collection'); |
122
|
|
|
|
123
|
3 |
|
$definition->setArguments([ |
124
|
3 |
|
new Reference($config['underscored_namer_collection']['default']), |
125
|
|
|
array_map(static function (string $namerId) { |
126
|
3 |
|
return new Reference($namerId); |
127
|
3 |
|
}, $config['underscored_namer_collection']['namers']), |
128
|
|
|
[ |
129
|
3 |
|
'join_table_field_suffix' => $config['underscored_namer_collection']['join_table_field_suffix'], |
130
|
|
|
], |
131
|
|
|
]); |
132
|
3 |
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.