@@ -6,32 +6,32 @@ discard block |
||
6 | 6 | use Symfony\Component\Config\Definition\ConfigurationInterface; |
7 | 7 | |
8 | 8 | class Configuration implements ConfigurationInterface { |
9 | - public function getConfigTreeBuilder() { |
|
10 | - $treeBuilder = new TreeBuilder(); |
|
11 | - $rootNode = $treeBuilder->root('one_guard_dynamic_configuration'); |
|
9 | + public function getConfigTreeBuilder() { |
|
10 | + $treeBuilder = new TreeBuilder(); |
|
11 | + $rootNode = $treeBuilder->root('one_guard_dynamic_configuration'); |
|
12 | 12 | |
13 | - $rootNode |
|
14 | - ->addDefaultsIfNotSet() |
|
15 | - ->children() |
|
16 | - ->arrayNode('definitions') |
|
17 | - ->prototype('array') |
|
18 | - ->children() |
|
19 | - ->enumNode('type') |
|
20 | - ->values(['entity', 'string']) |
|
21 | - ->isRequired() |
|
22 | - ->end() |
|
23 | - ->arrayNode('options') |
|
24 | - ->children() |
|
25 | - ->scalarNode('class') |
|
26 | - ->validate() |
|
27 | - ->ifTrue(function ($class) { |
|
28 | - return !class_exists($class); |
|
29 | - }) |
|
30 | - ->thenInvalid("Class doesn't exist.") |
|
31 | - ->end() |
|
32 | - ->end() |
|
33 | - ->scalarNode('choice_label')->end() |
|
34 | - ->end() |
|
13 | + $rootNode |
|
14 | + ->addDefaultsIfNotSet() |
|
15 | + ->children() |
|
16 | + ->arrayNode('definitions') |
|
17 | + ->prototype('array') |
|
18 | + ->children() |
|
19 | + ->enumNode('type') |
|
20 | + ->values(['entity', 'string']) |
|
21 | + ->isRequired() |
|
22 | + ->end() |
|
23 | + ->arrayNode('options') |
|
24 | + ->children() |
|
25 | + ->scalarNode('class') |
|
26 | + ->validate() |
|
27 | + ->ifTrue(function ($class) { |
|
28 | + return !class_exists($class); |
|
29 | + }) |
|
30 | + ->thenInvalid("Class doesn't exist.") |
|
31 | + ->end() |
|
32 | + ->end() |
|
33 | + ->scalarNode('choice_label')->end() |
|
34 | + ->end() |
|
35 | 35 | // ->validate() |
36 | 36 | // ->ifTrue(function ($options) { |
37 | 37 | // if (!empty($options['class'])) { |
@@ -42,19 +42,19 @@ discard block |
||
42 | 42 | // }) |
43 | 43 | // ->thenInvalid("Property not accessible.") |
44 | 44 | // ->end() |
45 | - ->end() |
|
46 | - ->end() |
|
47 | - ->end() |
|
48 | - ->end() |
|
49 | - ->scalarNode('translation_domain') |
|
50 | - ->defaultValue('messages') |
|
51 | - ->cannotBeEmpty() |
|
52 | - ->end() |
|
53 | - ->scalarNode('translation_prefix') |
|
54 | - ->defaultValue('') |
|
55 | - ->end() |
|
56 | - ->end(); |
|
45 | + ->end() |
|
46 | + ->end() |
|
47 | + ->end() |
|
48 | + ->end() |
|
49 | + ->scalarNode('translation_domain') |
|
50 | + ->defaultValue('messages') |
|
51 | + ->cannotBeEmpty() |
|
52 | + ->end() |
|
53 | + ->scalarNode('translation_prefix') |
|
54 | + ->defaultValue('') |
|
55 | + ->end() |
|
56 | + ->end(); |
|
57 | 57 | |
58 | - return $treeBuilder; |
|
59 | - } |
|
58 | + return $treeBuilder; |
|
59 | + } |
|
60 | 60 | } |
@@ -17,93 +17,93 @@ |
||
17 | 17 | * @Route(path="/configuration-value") |
18 | 18 | */ |
19 | 19 | class ConfigurationValueController extends Controller { |
20 | - /** |
|
21 | - * @var \Doctrine\ORM\EntityManagerInterface |
|
22 | - */ |
|
23 | - private $entityManager; |
|
20 | + /** |
|
21 | + * @var \Doctrine\ORM\EntityManagerInterface |
|
22 | + */ |
|
23 | + private $entityManager; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var DefinitionRegistry |
|
27 | - */ |
|
28 | - private $registry; |
|
25 | + /** |
|
26 | + * @var DefinitionRegistry |
|
27 | + */ |
|
28 | + private $registry; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @var ConfigurationResolverFactory |
|
32 | - */ |
|
33 | - private $factory; |
|
30 | + /** |
|
31 | + * @var ConfigurationResolverFactory |
|
32 | + */ |
|
33 | + private $factory; |
|
34 | 34 | |
35 | - public function __construct( |
|
36 | - RegistryInterface $doctrine, |
|
37 | - DefinitionRegistry $registry, |
|
38 | - ConfigurationResolverFactory $factory |
|
39 | - ) { |
|
40 | - $this->entityManager = $doctrine->getEntityManagerForClass(ConfigurationValue::class); |
|
41 | - $this->registry = $registry; |
|
42 | - $this->factory = $factory; |
|
43 | - } |
|
35 | + public function __construct( |
|
36 | + RegistryInterface $doctrine, |
|
37 | + DefinitionRegistry $registry, |
|
38 | + ConfigurationResolverFactory $factory |
|
39 | + ) { |
|
40 | + $this->entityManager = $doctrine->getEntityManagerForClass(ConfigurationValue::class); |
|
41 | + $this->registry = $registry; |
|
42 | + $this->factory = $factory; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @Route(path="/assign", name="configurationValue.assign") |
|
47 | - * @throws \Doctrine\ORM\OptimisticLockException |
|
48 | - */ |
|
49 | - public function assignAction(Request $request) { |
|
50 | - $configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
|
51 | - $existingValueKeys = array_map(function (ConfigurationValue $value) { |
|
52 | - return $value->getKey(); |
|
53 | - }, $configurationValues); |
|
54 | - $missingValueKeys = array_diff($this->registry->keys(), $existingValueKeys); |
|
55 | - $configurationValues = array_merge( |
|
56 | - $configurationValues, |
|
57 | - array_map(function (string $key) { |
|
58 | - $value = new ConfigurationValue(); |
|
59 | - $value->setKey($key); |
|
60 | - return $value; |
|
61 | - }, $missingValueKeys) |
|
62 | - ); |
|
45 | + /** |
|
46 | + * @Route(path="/assign", name="configurationValue.assign") |
|
47 | + * @throws \Doctrine\ORM\OptimisticLockException |
|
48 | + */ |
|
49 | + public function assignAction(Request $request) { |
|
50 | + $configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
|
51 | + $existingValueKeys = array_map(function (ConfigurationValue $value) { |
|
52 | + return $value->getKey(); |
|
53 | + }, $configurationValues); |
|
54 | + $missingValueKeys = array_diff($this->registry->keys(), $existingValueKeys); |
|
55 | + $configurationValues = array_merge( |
|
56 | + $configurationValues, |
|
57 | + array_map(function (string $key) { |
|
58 | + $value = new ConfigurationValue(); |
|
59 | + $value->setKey($key); |
|
60 | + return $value; |
|
61 | + }, $missingValueKeys) |
|
62 | + ); |
|
63 | 63 | |
64 | - $form = $this->createForm(ConfigurationValuesType::class, ['configurationValues' => $configurationValues]); |
|
65 | - $form->handleRequest($request); |
|
66 | - if ($form->isSubmitted() and $form->isValid()) { |
|
67 | - /* @var $configurationValue ConfigurationValue */ |
|
68 | - foreach ($form->get('configurationValues')->getData() as $configurationValue) { |
|
69 | - $isPersisted = \Doctrine\ORM\UnitOfWork::STATE_MANAGED === |
|
70 | - $this->entityManager->getUnitOfWork()->getEntityState($configurationValue); |
|
64 | + $form = $this->createForm(ConfigurationValuesType::class, ['configurationValues' => $configurationValues]); |
|
65 | + $form->handleRequest($request); |
|
66 | + if ($form->isSubmitted() and $form->isValid()) { |
|
67 | + /* @var $configurationValue ConfigurationValue */ |
|
68 | + foreach ($form->get('configurationValues')->getData() as $configurationValue) { |
|
69 | + $isPersisted = \Doctrine\ORM\UnitOfWork::STATE_MANAGED === |
|
70 | + $this->entityManager->getUnitOfWork()->getEntityState($configurationValue); |
|
71 | 71 | |
72 | - if ($configurationValue->getValue() === null and $isPersisted) { |
|
73 | - $this->entityManager->remove($configurationValue); |
|
74 | - } else if ($configurationValue->getValue() !== null and !$isPersisted) { |
|
75 | - $this->entityManager->persist($configurationValue); |
|
76 | - } |
|
77 | - } |
|
78 | - $this->entityManager->flush(); |
|
72 | + if ($configurationValue->getValue() === null and $isPersisted) { |
|
73 | + $this->entityManager->remove($configurationValue); |
|
74 | + } else if ($configurationValue->getValue() !== null and !$isPersisted) { |
|
75 | + $this->entityManager->persist($configurationValue); |
|
76 | + } |
|
77 | + } |
|
78 | + $this->entityManager->flush(); |
|
79 | 79 | |
80 | - return $this->redirectToRoute('configurationValue.view'); |
|
81 | - } |
|
80 | + return $this->redirectToRoute('configurationValue.view'); |
|
81 | + } |
|
82 | 82 | |
83 | - return $this->render('OneGuardDynamicConfigurationBundle:ConfigurationValue:assign.html.twig', ['form' => $form->createView()]); |
|
84 | - } |
|
83 | + return $this->render('OneGuardDynamicConfigurationBundle:ConfigurationValue:assign.html.twig', ['form' => $form->createView()]); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @Route(path="/view", name="configurationValue.view") |
|
88 | - */ |
|
89 | - public function viewAction() { |
|
90 | - /* @var $configurationValues ConfigurationValue[] */ |
|
91 | - $configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
|
92 | - $labels = []; |
|
93 | - $propertyAccessor = new PropertyAccessor(); |
|
94 | - foreach ($configurationValues as $configurationValue) { |
|
95 | - $definition = $this->registry->get($configurationValue->getKey()); |
|
96 | - if ($definition instanceof EntityDefinition) { |
|
97 | - $object = $this->factory->create($definition->getKey())->resolve(); |
|
98 | - $labels[$definition->getKey()] = $propertyAccessor->getValue($object, $definition->getChoiceLabel()); |
|
99 | - } |
|
100 | - } |
|
86 | + /** |
|
87 | + * @Route(path="/view", name="configurationValue.view") |
|
88 | + */ |
|
89 | + public function viewAction() { |
|
90 | + /* @var $configurationValues ConfigurationValue[] */ |
|
91 | + $configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
|
92 | + $labels = []; |
|
93 | + $propertyAccessor = new PropertyAccessor(); |
|
94 | + foreach ($configurationValues as $configurationValue) { |
|
95 | + $definition = $this->registry->get($configurationValue->getKey()); |
|
96 | + if ($definition instanceof EntityDefinition) { |
|
97 | + $object = $this->factory->create($definition->getKey())->resolve(); |
|
98 | + $labels[$definition->getKey()] = $propertyAccessor->getValue($object, $definition->getChoiceLabel()); |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - return $this->render('OneGuardDynamicConfigurationBundle:ConfigurationValue:view.html.twig', [ |
|
103 | - 'configurationValues' => $configurationValues, |
|
104 | - 'translationDomain' => $this->getParameter('one_guard.dynamic_configuration.translation_domain'), |
|
105 | - 'translationPrefix' => $this->getParameter('one_guard.dynamic_configuration.translation_prefix'), |
|
106 | - 'labels' => $labels |
|
107 | - ]); |
|
108 | - } |
|
102 | + return $this->render('OneGuardDynamicConfigurationBundle:ConfigurationValue:view.html.twig', [ |
|
103 | + 'configurationValues' => $configurationValues, |
|
104 | + 'translationDomain' => $this->getParameter('one_guard.dynamic_configuration.translation_domain'), |
|
105 | + 'translationPrefix' => $this->getParameter('one_guard.dynamic_configuration.translation_prefix'), |
|
106 | + 'labels' => $labels |
|
107 | + ]); |
|
108 | + } |
|
109 | 109 | } |
@@ -18,39 +18,39 @@ |
||
18 | 18 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
19 | 19 | |
20 | 20 | class OneGuardDynamicConfigurationExtension extends Extension { |
21 | - /** |
|
22 | - * @param array $configs |
|
23 | - * @param ContainerBuilder $container |
|
24 | - * @throws \Exception |
|
25 | - */ |
|
26 | - public function load(array $configs, ContainerBuilder $container) { |
|
27 | - $loader = new YamlFileLoader( |
|
28 | - $container, |
|
29 | - new FileLocator(__DIR__.'/../Resources/config') |
|
30 | - ); |
|
31 | - $loader->load('services.yml'); |
|
32 | - |
|
33 | - $configuration = new Configuration(); |
|
34 | - $config = $this->processConfiguration($configuration, $configs); |
|
35 | - |
|
36 | - $container->setParameter('one_guard.dynamic_configuration.translation_domain', $config['translation_domain']); |
|
37 | - $container->setParameter('one_guard.dynamic_configuration.translation_prefix', $config['translation_prefix']); |
|
38 | - |
|
39 | - $register = $container->getDefinition(DefinitionRegistry::class); |
|
40 | - |
|
41 | - foreach ($config['definitions'] as $key => $definitionConfig) { |
|
42 | - switch ($definitionConfig['type']) { |
|
43 | - case 'entity': |
|
44 | - $register->addMethodCall('registerEntity', [ |
|
45 | - $key, |
|
46 | - $definitionConfig['options']['class'], |
|
47 | - $definitionConfig['options']['choice_label'] |
|
48 | - ]); |
|
49 | - break; |
|
50 | - case 'string': |
|
51 | - $register->addMethodCall('registerString', [$key]); |
|
52 | - break; |
|
53 | - } |
|
54 | - } |
|
55 | - } |
|
21 | + /** |
|
22 | + * @param array $configs |
|
23 | + * @param ContainerBuilder $container |
|
24 | + * @throws \Exception |
|
25 | + */ |
|
26 | + public function load(array $configs, ContainerBuilder $container) { |
|
27 | + $loader = new YamlFileLoader( |
|
28 | + $container, |
|
29 | + new FileLocator(__DIR__.'/../Resources/config') |
|
30 | + ); |
|
31 | + $loader->load('services.yml'); |
|
32 | + |
|
33 | + $configuration = new Configuration(); |
|
34 | + $config = $this->processConfiguration($configuration, $configs); |
|
35 | + |
|
36 | + $container->setParameter('one_guard.dynamic_configuration.translation_domain', $config['translation_domain']); |
|
37 | + $container->setParameter('one_guard.dynamic_configuration.translation_prefix', $config['translation_prefix']); |
|
38 | + |
|
39 | + $register = $container->getDefinition(DefinitionRegistry::class); |
|
40 | + |
|
41 | + foreach ($config['definitions'] as $key => $definitionConfig) { |
|
42 | + switch ($definitionConfig['type']) { |
|
43 | + case 'entity': |
|
44 | + $register->addMethodCall('registerEntity', [ |
|
45 | + $key, |
|
46 | + $definitionConfig['options']['class'], |
|
47 | + $definitionConfig['options']['choice_label'] |
|
48 | + ]); |
|
49 | + break; |
|
50 | + case 'string': |
|
51 | + $register->addMethodCall('registerString', [$key]); |
|
52 | + break; |
|
53 | + } |
|
54 | + } |
|
55 | + } |
|
56 | 56 | } |
@@ -18,50 +18,50 @@ |
||
18 | 18 | * @ORM\Entity() |
19 | 19 | */ |
20 | 20 | class ConfigurationValue { |
21 | - /** |
|
22 | - * @var string |
|
23 | - * |
|
24 | - * @ORM\Id() |
|
25 | - * @ORM\Column(name="`key`", nullable=false, type="string", length=64) |
|
26 | - * |
|
27 | - * @Assert\NotNull() |
|
28 | - * @Assert\NotBlank() |
|
29 | - * @Assert\Length(max="64") |
|
30 | - */ |
|
31 | - private $key; |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + * |
|
24 | + * @ORM\Id() |
|
25 | + * @ORM\Column(name="`key`", nullable=false, type="string", length=64) |
|
26 | + * |
|
27 | + * @Assert\NotNull() |
|
28 | + * @Assert\NotBlank() |
|
29 | + * @Assert\Length(max="64") |
|
30 | + */ |
|
31 | + private $key; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @var string |
|
35 | - * |
|
36 | - * @ORM\Column(name="`value`", nullable=false, type="text") |
|
37 | - */ |
|
38 | - private $value; |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + * |
|
36 | + * @ORM\Column(name="`value`", nullable=false, type="text") |
|
37 | + */ |
|
38 | + private $value; |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string|null |
|
42 | - */ |
|
43 | - public function getKey() { |
|
44 | - return $this->key; |
|
45 | - } |
|
40 | + /** |
|
41 | + * @return string|null |
|
42 | + */ |
|
43 | + public function getKey() { |
|
44 | + return $this->key; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param string|null $key |
|
49 | - */ |
|
50 | - public function setKey(string $key = null) { |
|
51 | - $this->key = $key; |
|
52 | - } |
|
47 | + /** |
|
48 | + * @param string|null $key |
|
49 | + */ |
|
50 | + public function setKey(string $key = null) { |
|
51 | + $this->key = $key; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @return string|null |
|
56 | - */ |
|
57 | - public function getValue() { |
|
58 | - return $this->value; |
|
59 | - } |
|
54 | + /** |
|
55 | + * @return string|null |
|
56 | + */ |
|
57 | + public function getValue() { |
|
58 | + return $this->value; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param string|null $value |
|
63 | - */ |
|
64 | - public function setValue(string $value = null) { |
|
65 | - $this->value = $value; |
|
66 | - } |
|
61 | + /** |
|
62 | + * @param string|null $value |
|
63 | + */ |
|
64 | + public function setValue(string $value = null) { |
|
65 | + $this->value = $value; |
|
66 | + } |
|
67 | 67 | } |
@@ -12,54 +12,54 @@ |
||
12 | 12 | namespace OneGuard\Bundle\DynamicConfigurationBundle; |
13 | 13 | |
14 | 14 | class DefinitionRegistry { |
15 | - /** |
|
16 | - * @var Definition[] |
|
17 | - */ |
|
18 | - private $definitions = []; |
|
15 | + /** |
|
16 | + * @var Definition[] |
|
17 | + */ |
|
18 | + private $definitions = []; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @param Definition $definition |
|
22 | - * @throws \InvalidArgumentException |
|
23 | - */ |
|
24 | - public function register(Definition $definition) { |
|
25 | - if (array_key_exists($definition->getKey(), $this->definitions)) { |
|
26 | - throw new \InvalidArgumentException('Definition already exists.'); |
|
27 | - } |
|
28 | - $this->definitions[$definition->getKey()] = $definition; |
|
29 | - } |
|
20 | + /** |
|
21 | + * @param Definition $definition |
|
22 | + * @throws \InvalidArgumentException |
|
23 | + */ |
|
24 | + public function register(Definition $definition) { |
|
25 | + if (array_key_exists($definition->getKey(), $this->definitions)) { |
|
26 | + throw new \InvalidArgumentException('Definition already exists.'); |
|
27 | + } |
|
28 | + $this->definitions[$definition->getKey()] = $definition; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param string $key |
|
33 | - * @return Definition |
|
34 | - * @throws \InvalidArgumentException |
|
35 | - */ |
|
36 | - public function get(string $key) : Definition { |
|
37 | - if (!array_key_exists($key, $this->definitions)) { |
|
38 | - throw new \InvalidArgumentException('Definition not found.'); |
|
39 | - } |
|
40 | - return $this->definitions[$key]; |
|
41 | - } |
|
31 | + /** |
|
32 | + * @param string $key |
|
33 | + * @return Definition |
|
34 | + * @throws \InvalidArgumentException |
|
35 | + */ |
|
36 | + public function get(string $key) : Definition { |
|
37 | + if (!array_key_exists($key, $this->definitions)) { |
|
38 | + throw new \InvalidArgumentException('Definition not found.'); |
|
39 | + } |
|
40 | + return $this->definitions[$key]; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param string $key |
|
45 | - * @return bool |
|
46 | - */ |
|
47 | - public function has(string $key) : bool { |
|
48 | - return array_key_exists($key, $this->definitions); |
|
49 | - } |
|
43 | + /** |
|
44 | + * @param string $key |
|
45 | + * @return bool |
|
46 | + */ |
|
47 | + public function has(string $key) : bool { |
|
48 | + return array_key_exists($key, $this->definitions); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @return string[] |
|
53 | - */ |
|
54 | - public function keys() : array { |
|
55 | - return array_keys($this->definitions); |
|
56 | - } |
|
51 | + /** |
|
52 | + * @return string[] |
|
53 | + */ |
|
54 | + public function keys() : array { |
|
55 | + return array_keys($this->definitions); |
|
56 | + } |
|
57 | 57 | |
58 | - public function registerEntity($key, $class, $choiceLabel) { |
|
59 | - $this->register(new EntityDefinition($key, $class, $choiceLabel)); |
|
60 | - } |
|
58 | + public function registerEntity($key, $class, $choiceLabel) { |
|
59 | + $this->register(new EntityDefinition($key, $class, $choiceLabel)); |
|
60 | + } |
|
61 | 61 | |
62 | - public function registerString($key) { |
|
63 | - $this->register(new StringDefinition($key)); |
|
64 | - } |
|
62 | + public function registerString($key) { |
|
63 | + $this->register(new StringDefinition($key)); |
|
64 | + } |
|
65 | 65 | } |