1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OneGuard DynamicConfigurationBundle. |
5
|
|
|
* |
6
|
|
|
* (c) OneGuard <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OneGuard\Bundle\DynamicConfigurationBundle\Controller; |
13
|
|
|
|
14
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\ConfigurationResolverFactory; |
15
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\DefinitionRegistry; |
16
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\Entity\ConfigurationValue; |
17
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\EntityDefinition; |
18
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\Form\ConfigurationValuesType; |
19
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
23
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @Route(path="/configuration-value") |
27
|
|
|
*/ |
28
|
|
|
class ConfigurationValueController extends Controller { |
29
|
|
|
/** |
30
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface |
31
|
|
|
*/ |
32
|
|
|
private $entityManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var DefinitionRegistry |
36
|
|
|
*/ |
37
|
|
|
private $registry; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ConfigurationResolverFactory |
41
|
|
|
*/ |
42
|
|
|
private $factory; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
RegistryInterface $doctrine, |
46
|
|
|
DefinitionRegistry $registry, |
47
|
|
|
ConfigurationResolverFactory $factory |
48
|
|
|
) { |
49
|
|
|
$this->entityManager = $doctrine->getEntityManagerForClass(ConfigurationValue::class); |
50
|
|
|
$this->registry = $registry; |
51
|
|
|
$this->factory = $factory; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @Route(path="/assign", name="configurationValue.assign") |
56
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
57
|
|
|
* @throws \Doctrine\ORM\ORMException |
58
|
|
|
*/ |
59
|
|
|
public function assignAction(Request $request) { |
60
|
|
|
$configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
61
|
|
|
$existingValueKeys = array_map(function (ConfigurationValue $value) { |
62
|
|
|
return $value->getKey(); |
63
|
|
|
}, $configurationValues); |
64
|
|
|
$missingValueKeys = array_diff($this->registry->keys(), $existingValueKeys); |
65
|
|
|
$configurationValues = array_merge( |
66
|
|
|
$configurationValues, |
67
|
|
|
array_map(function (string $key) { |
68
|
|
|
$value = new ConfigurationValue(); |
69
|
|
|
$value->setKey($key); |
70
|
|
|
return $value; |
71
|
|
|
}, $missingValueKeys) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$form = $this->createForm(ConfigurationValuesType::class, ['configurationValues' => $configurationValues]); |
75
|
|
|
$form->handleRequest($request); |
76
|
|
|
if ($form->isSubmitted() and $form->isValid()) { |
77
|
|
|
/* @var $configurationValue ConfigurationValue */ |
78
|
|
|
foreach ($form->get('configurationValues')->getData() as $configurationValue) { |
79
|
|
|
$isPersisted = \Doctrine\ORM\UnitOfWork::STATE_MANAGED === |
80
|
|
|
$this->entityManager->getUnitOfWork()->getEntityState($configurationValue); |
81
|
|
|
|
82
|
|
|
if ($configurationValue->getValue() === null and $isPersisted) { |
83
|
|
|
$this->entityManager->remove($configurationValue); |
84
|
|
|
} else if ($configurationValue->getValue() !== null and !$isPersisted) { |
85
|
|
|
$this->entityManager->persist($configurationValue); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$this->entityManager->flush(); |
89
|
|
|
|
90
|
|
|
return $this->redirectToRoute('configurationValue.view'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->render('@OneGuardDynamicConfiguration/ConfigurationValue/assign.html.twig', ['form' => $form->createView()]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @Route(path="/view", name="configurationValue.view") |
98
|
|
|
*/ |
99
|
|
|
public function viewAction() { |
100
|
|
|
/* @var $configurationValues ConfigurationValue[] */ |
101
|
|
|
$configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
102
|
|
|
$labels = []; |
103
|
|
|
$propertyAccessor = new PropertyAccessor(); |
104
|
|
|
foreach ($configurationValues as $configurationValue) { |
105
|
|
|
$definition = $this->registry->get($configurationValue->getKey()); |
106
|
|
|
if ($definition instanceof EntityDefinition) { |
107
|
|
|
$object = $this->factory->create($definition->getKey())->resolve(); |
108
|
|
|
$labels[$definition->getKey()] = $propertyAccessor->getValue($object, $definition->getChoiceLabel()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->render('@OneGuardDynamicConfiguration/ConfigurationValue/view.html.twig', [ |
113
|
|
|
'configurationValues' => $configurationValues, |
114
|
|
|
'translationDomain' => $this->getParameter('one_guard.dynamic_configuration.translation_domain'), |
115
|
|
|
'translationPrefix' => $this->getParameter('one_guard.dynamic_configuration.translation_prefix'), |
116
|
|
|
'labels' => $labels |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|