1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\DependencyInjection\Component; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
7
|
|
|
use Symfony\Component\Config\Definition\Processor; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
|
11
|
|
|
class Openid implements Extensionable |
12
|
|
|
{ |
13
|
|
|
public function configure(ContainerBuilder &$container) |
14
|
|
|
{ |
15
|
|
|
$clientConfigurations = $container->getParameter('sludio_helper.openid.clients'); |
16
|
|
|
$clientServiceKeys = []; |
17
|
|
|
foreach ($clientConfigurations as $key => $clientConfig) { |
18
|
|
|
$tree = new TreeBuilder(); |
19
|
|
|
$node = $tree->root('sludio_helper_openid_client/clients/'.$key); |
20
|
|
|
$this->buildClientConfiguration($node); |
21
|
|
|
$processor = new Processor(); |
22
|
|
|
$config = $processor->process($tree->buildTree(), [$clientConfig]); |
23
|
|
|
$clientServiceKey = 'sludio_helper.openid.client.'.$key; |
24
|
|
|
$service = [ |
25
|
|
|
'key' => $clientServiceKey, |
26
|
|
|
]; |
27
|
|
|
if (isset($config['provider_options']) && isset($config['provider_options']['name'])) { |
28
|
|
|
$service['name'] = $config['provider_options']['name']; |
29
|
|
|
} else { |
30
|
|
|
$service['name'] = ucfirst($key); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$clientServiceKeys[$key] = $service; |
34
|
|
View Code Duplication |
foreach ($config as $ckey => $cvalue) { |
|
|
|
|
35
|
|
|
if ($ckey === 'provider_options') { |
36
|
|
|
if (is_array($cvalue)) { |
37
|
|
|
foreach ($cvalue as $pkey => $pvalue) { |
38
|
|
|
$container->setParameter($clientServiceKey.'.option.'.$pkey, $pvalue); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} else { |
42
|
|
|
$container->setParameter($clientServiceKey.'.'.$ckey, $cvalue); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
$this->configureClient($container, $clientServiceKey); |
46
|
|
|
} |
47
|
|
|
$container->getDefinition('sludio_helper.openid.registry')->replaceArgument(1, $clientServiceKeys); |
48
|
|
|
if ($container->getParameter('sludio_helper.oauth.enabled') == true) { |
49
|
|
|
$container->getDefinition('sludio_helper.registry')->replaceArgument(2, $clientServiceKeys); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function buildClientConfiguration(NodeDefinition &$node) |
54
|
|
|
{ |
55
|
|
|
$optionsNode = $node->children(); |
56
|
|
|
|
57
|
|
|
// @formatter:off |
58
|
|
|
$optionsNode |
59
|
|
|
->scalarNode('api_key')->isRequired()->cannotBeEmpty()->end() |
60
|
|
|
->scalarNode('openid_url')->isRequired()->cannotBeEmpty()->end() |
61
|
|
|
->scalarNode('preg_check')->isRequired()->cannotBeEmpty()->end() |
62
|
|
|
->scalarNode('ns_mode')->defaultValue('sreg')->end() |
63
|
|
|
->scalarNode('user_class')->isRequired()->end() |
64
|
|
|
->scalarNode('user_provider')->defaultValue('Sludio\HelperBundle\Openid\Login\Login')->end() |
65
|
|
|
->scalarNode('redirect_route')->isRequired()->cannotBeEmpty()->end() |
66
|
|
|
->arrayNode('provider_options')->prototype('variable')->end()->end() |
67
|
|
|
; |
68
|
|
|
// @formatter:on |
69
|
|
|
|
70
|
|
|
$optionsNode->end(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = []) |
74
|
|
|
{ |
75
|
|
|
$clientDefinition = $container->register($clientServiceKey, $container->getParameter($clientServiceKey.'.user_provider')); |
76
|
|
|
$clientDefinition->setArguments([ |
77
|
|
|
$clientServiceKey, |
78
|
|
|
new Reference('request_stack'), |
79
|
|
|
new Reference('service_container'), |
80
|
|
|
new Reference('router'), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.