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 | use Sludio\HelperBundle\Openid\Login\Login; |
||
11 | |||
12 | class Openid extends AbstractComponent implements ExtensionInterface |
||
13 | { |
||
14 | protected $alias; |
||
15 | |||
16 | public function configure(ContainerBuilder $container, $alias) |
||
17 | { |
||
18 | $this->alias = $alias.'.openid'; |
||
19 | $clientConfigurations = $container->getParameter($this->alias.'.clients'); |
||
20 | $clientServiceKeys = []; |
||
21 | /** @var $clientConfigurations array */ |
||
22 | foreach ($clientConfigurations as $key => $clientConfig) { |
||
23 | $tree = new TreeBuilder(); |
||
24 | $node = $tree->root('sludio_helper_openid_client/clients/'.$key); |
||
25 | $this->buildClientConfiguration($node); |
||
26 | $processor = new Processor(); |
||
27 | $config = $processor->process($tree->buildTree(), [$clientConfig]); |
||
28 | $clientServiceKey = $this->alias.'.client.'.$key; |
||
29 | $service = [ |
||
30 | 'key' => $clientServiceKey, |
||
31 | ]; |
||
32 | if (isset($config['provider_options']['name'])) { |
||
33 | $service['name'] = $config['provider_options']['name']; |
||
34 | } else { |
||
35 | $service['name'] = ucfirst($key); |
||
36 | } |
||
37 | |||
38 | $clientServiceKeys[$key] = $service; |
||
39 | foreach ($config as $ckey => $cvalue) { |
||
40 | if ($ckey === 'provider_options') { |
||
41 | if (\is_array($cvalue)) { |
||
42 | foreach ($cvalue as $pkey => $pvalue) { |
||
43 | $container->setParameter($clientServiceKey.'.option.'.$pkey, $pvalue); |
||
44 | } |
||
45 | } |
||
46 | } else { |
||
47 | $container->setParameter($clientServiceKey.'.'.$ckey, $cvalue); |
||
48 | } |
||
49 | } |
||
50 | $this->configureClient($container, $clientServiceKey); |
||
51 | } |
||
52 | $container->getDefinition($this->alias.'.registry')->replaceArgument(0, $clientServiceKeys); |
||
53 | if ($container->getParameter($alias.'.oauth.enabled') === true) { |
||
54 | $container->getDefinition($alias.'.registry')->replaceArgument(1, $clientServiceKeys); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | public function buildClientConfiguration(NodeDefinition $node) |
||
59 | { |
||
60 | $node->addDefaultsIfNotSet(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
61 | $optionsNode = $node->children(); |
||
62 | |||
63 | // @formatter:off |
||
64 | $optionsNode |
||
65 | ->scalarNode('api_key')->isRequired()->cannotBeEmpty()->end() |
||
66 | ->scalarNode('openid_url')->isRequired()->cannotBeEmpty()->end() |
||
67 | ->scalarNode('preg_check')->isRequired()->cannotBeEmpty()->end() |
||
68 | ->scalarNode('ns_mode')->defaultValue('sreg')->end() |
||
69 | ->scalarNode('user_class')->isRequired()->end() |
||
70 | ->scalarNode('user_provider')->defaultValue(Login::class)->end() |
||
71 | ->scalarNode('redirect_route')->isRequired()->cannotBeEmpty()->end() |
||
72 | ->arrayNode('provider_options')->prototype('variable')->end()->end() |
||
73 | ; |
||
74 | // @formatter:on |
||
75 | |||
76 | $optionsNode->end(); |
||
77 | } |
||
78 | |||
79 | public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = []) |
||
80 | { |
||
81 | $clientDefinition = $container->register($clientServiceKey, $container->getParameter($clientServiceKey.'.user_provider')); |
||
82 | $clientDefinition->setArguments([ |
||
83 | $clientServiceKey, |
||
84 | new Reference('request_stack'), |
||
85 | new Reference('router'), |
||
86 | ])->addMethodCall('setContainer', [new Reference('service_container')])->addMethodCall('makeParameters'); |
||
87 | } |
||
88 | } |
||
89 |