@@ -8,28 +8,28 @@ |
||
8 | 8 | use OneGuard\Bundle\DynamicConfigurationBundle\ProtectedReferencesChecker; |
9 | 9 | |
10 | 10 | class ProtectedReferencesDoctrineSubscriber implements EventSubscriber { |
11 | - /** |
|
12 | - * @var ProtectedReferencesChecker |
|
13 | - */ |
|
14 | - private $checker; |
|
11 | + /** |
|
12 | + * @var ProtectedReferencesChecker |
|
13 | + */ |
|
14 | + private $checker; |
|
15 | 15 | |
16 | - public function __construct(ProtectedReferencesChecker $checker) { |
|
17 | - $this->checker = $checker; |
|
18 | - } |
|
16 | + public function __construct(ProtectedReferencesChecker $checker) { |
|
17 | + $this->checker = $checker; |
|
18 | + } |
|
19 | 19 | |
20 | - public function getSubscribedEvents() { |
|
21 | - return [ |
|
22 | - 'preRemove' |
|
23 | - ]; |
|
24 | - } |
|
20 | + public function getSubscribedEvents() { |
|
21 | + return [ |
|
22 | + 'preRemove' |
|
23 | + ]; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @param LifecycleEventArgs $args |
|
28 | - * @throws ProtectedReferenceException |
|
29 | - * @throws \Doctrine\ORM\NoResultException |
|
30 | - * @throws \Doctrine\ORM\NonUniqueResultException |
|
31 | - */ |
|
32 | - public function preRemove(LifecycleEventArgs $args) { |
|
33 | - $this->checker->checkReferenceForProtection($args->getObject()); |
|
34 | - } |
|
26 | + /** |
|
27 | + * @param LifecycleEventArgs $args |
|
28 | + * @throws ProtectedReferenceException |
|
29 | + * @throws \Doctrine\ORM\NoResultException |
|
30 | + * @throws \Doctrine\ORM\NonUniqueResultException |
|
31 | + */ |
|
32 | + public function preRemove(LifecycleEventArgs $args) { |
|
33 | + $this->checker->checkReferenceForProtection($args->getObject()); |
|
34 | + } |
|
35 | 35 | } |
@@ -7,11 +7,11 @@ |
||
7 | 7 | use Symfony\Component\Form\FormBuilderInterface; |
8 | 8 | |
9 | 9 | class ConfigurationValuesType extends AbstractType { |
10 | - public function buildForm(FormBuilderInterface $builder, array $options) { |
|
11 | - $builder |
|
12 | - ->add('configurationValues', CollectionType::class, [ |
|
13 | - 'entry_type' => ConfigurationValueType::class, |
|
14 | - 'attr' => ['class' => 'panel panel-default panel-body'], |
|
15 | - ]); |
|
16 | - } |
|
10 | + public function buildForm(FormBuilderInterface $builder, array $options) { |
|
11 | + $builder |
|
12 | + ->add('configurationValues', CollectionType::class, [ |
|
13 | + 'entry_type' => ConfigurationValueType::class, |
|
14 | + 'attr' => ['class' => 'panel panel-default panel-body'], |
|
15 | + ]); |
|
16 | + } |
|
17 | 17 | } |
@@ -8,26 +8,26 @@ |
||
8 | 8 | use Symfony\Component\Form\FormBuilderInterface; |
9 | 9 | |
10 | 10 | class EntityConfigurationValueType extends EntityType { |
11 | - /** |
|
12 | - * @var RegistryInterface |
|
13 | - */ |
|
14 | - private $doctrine; |
|
11 | + /** |
|
12 | + * @var RegistryInterface |
|
13 | + */ |
|
14 | + private $doctrine; |
|
15 | 15 | |
16 | - public function __construct(RegistryInterface $doctrine) { |
|
17 | - parent::__construct($doctrine); |
|
18 | - $this->doctrine = $doctrine; |
|
19 | - } |
|
16 | + public function __construct(RegistryInterface $doctrine) { |
|
17 | + parent::__construct($doctrine); |
|
18 | + $this->doctrine = $doctrine; |
|
19 | + } |
|
20 | 20 | |
21 | - public function buildForm(FormBuilderInterface $builder, array $options) { |
|
22 | - parent::buildForm($builder, $options); |
|
23 | - $builder->addModelTransformer(new CallbackTransformer( |
|
24 | - function ($id) use ($options) { |
|
25 | - return $id === null ? null : $this->doctrine->getRepository($options['class'])->find($id); |
|
26 | - }, |
|
27 | - function ($entity) use ($options) { |
|
28 | - return $entity === null ? |
|
29 | - null : $this->doctrine->getEntityManagerForClass($options['class'])->getUnitOfWork()->getSingleIdentifierValue($entity); |
|
30 | - } |
|
31 | - )); |
|
32 | - } |
|
21 | + public function buildForm(FormBuilderInterface $builder, array $options) { |
|
22 | + parent::buildForm($builder, $options); |
|
23 | + $builder->addModelTransformer(new CallbackTransformer( |
|
24 | + function ($id) use ($options) { |
|
25 | + return $id === null ? null : $this->doctrine->getRepository($options['class'])->find($id); |
|
26 | + }, |
|
27 | + function ($entity) use ($options) { |
|
28 | + return $entity === null ? |
|
29 | + null : $this->doctrine->getEntityManagerForClass($options['class'])->getUnitOfWork()->getSingleIdentifierValue($entity); |
|
30 | + } |
|
31 | + )); |
|
32 | + } |
|
33 | 33 | } |
@@ -13,55 +13,55 @@ |
||
13 | 13 | use Symfony\Component\OptionsResolver\OptionsResolver; |
14 | 14 | |
15 | 15 | class ConfigurationValueType extends AbstractType { |
16 | - /** |
|
17 | - * @var DefinitionRegistry |
|
18 | - */ |
|
19 | - private $registry; |
|
16 | + /** |
|
17 | + * @var DefinitionRegistry |
|
18 | + */ |
|
19 | + private $registry; |
|
20 | 20 | |
21 | - private $translationDomain; |
|
22 | - private $translationPrefix; |
|
21 | + private $translationDomain; |
|
22 | + private $translationPrefix; |
|
23 | 23 | |
24 | - public function __construct( |
|
25 | - DefinitionRegistry $registry, |
|
26 | - string $translationDomain, |
|
27 | - string $translationPrefix |
|
28 | - ) { |
|
29 | - $this->registry = $registry; |
|
30 | - $this->translationDomain = $translationDomain; |
|
31 | - $this->translationPrefix = $translationPrefix; |
|
32 | - } |
|
24 | + public function __construct( |
|
25 | + DefinitionRegistry $registry, |
|
26 | + string $translationDomain, |
|
27 | + string $translationPrefix |
|
28 | + ) { |
|
29 | + $this->registry = $registry; |
|
30 | + $this->translationDomain = $translationDomain; |
|
31 | + $this->translationPrefix = $translationPrefix; |
|
32 | + } |
|
33 | 33 | |
34 | - public function buildForm(FormBuilderInterface $builder, array $options) { |
|
35 | - $builder |
|
36 | - ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { |
|
37 | - /* @var $configurationValue ConfigurationValue */ |
|
38 | - $configurationValue = $event->getData(); |
|
39 | - $definition = $this->registry->get($configurationValue->getKey()); |
|
40 | - switch (get_class($definition)) { |
|
41 | - case EntityDefinition::class: |
|
42 | - /* @var $definition EntityDefinition */ |
|
43 | - $form = $event->getForm(); |
|
44 | - $form->add('value', EntityConfigurationValueType::class, [ |
|
45 | - 'class' => $definition->getClass(), |
|
46 | - 'choice_label' => $definition->getChoiceLabel(), |
|
47 | - 'label' => $this->translationPrefix . $configurationValue->getKey(), |
|
48 | - 'translation_domain' => $this->translationDomain, |
|
49 | - 'placeholder' => 'Please choose', |
|
50 | - 'empty_data' => null, |
|
51 | - 'required' => false |
|
52 | - ]); |
|
53 | - break; |
|
54 | - default: // assume StringDefinition |
|
55 | - $event->getForm()->add('value', TextType::class, [ |
|
56 | - 'label' => $this->translationPrefix . $configurationValue->getKey(), |
|
57 | - 'translation_domain' => $this->translationDomain, |
|
58 | - 'required' => false |
|
59 | - ]); |
|
60 | - } |
|
61 | - }); |
|
62 | - } |
|
34 | + public function buildForm(FormBuilderInterface $builder, array $options) { |
|
35 | + $builder |
|
36 | + ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { |
|
37 | + /* @var $configurationValue ConfigurationValue */ |
|
38 | + $configurationValue = $event->getData(); |
|
39 | + $definition = $this->registry->get($configurationValue->getKey()); |
|
40 | + switch (get_class($definition)) { |
|
41 | + case EntityDefinition::class: |
|
42 | + /* @var $definition EntityDefinition */ |
|
43 | + $form = $event->getForm(); |
|
44 | + $form->add('value', EntityConfigurationValueType::class, [ |
|
45 | + 'class' => $definition->getClass(), |
|
46 | + 'choice_label' => $definition->getChoiceLabel(), |
|
47 | + 'label' => $this->translationPrefix . $configurationValue->getKey(), |
|
48 | + 'translation_domain' => $this->translationDomain, |
|
49 | + 'placeholder' => 'Please choose', |
|
50 | + 'empty_data' => null, |
|
51 | + 'required' => false |
|
52 | + ]); |
|
53 | + break; |
|
54 | + default: // assume StringDefinition |
|
55 | + $event->getForm()->add('value', TextType::class, [ |
|
56 | + 'label' => $this->translationPrefix . $configurationValue->getKey(), |
|
57 | + 'translation_domain' => $this->translationDomain, |
|
58 | + 'required' => false |
|
59 | + ]); |
|
60 | + } |
|
61 | + }); |
|
62 | + } |
|
63 | 63 | |
64 | - public function configureOptions(OptionsResolver $resolver) { |
|
65 | - $resolver->setDefault('data_class', ConfigurationValue::class); |
|
66 | - } |
|
64 | + public function configureOptions(OptionsResolver $resolver) { |
|
65 | + $resolver->setDefault('data_class', ConfigurationValue::class); |
|
66 | + } |
|
67 | 67 | } |
@@ -9,50 +9,50 @@ |
||
9 | 9 | * @ORM\Entity() |
10 | 10 | */ |
11 | 11 | class ConfigurationValue { |
12 | - /** |
|
13 | - * @var string |
|
14 | - * |
|
15 | - * @ORM\Id() |
|
16 | - * @ORM\Column(name="`key`", nullable=false, type="string", length=64) |
|
17 | - * |
|
18 | - * @Assert\NotNull() |
|
19 | - * @Assert\NotBlank() |
|
20 | - * @Assert\Length(max="64") |
|
21 | - */ |
|
22 | - private $key; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var string |
|
26 | - * |
|
27 | - * @ORM\Column(name="`value`", nullable=false, type="text") |
|
28 | - */ |
|
29 | - private $value; |
|
30 | - |
|
31 | - /** |
|
32 | - * @return string|null |
|
33 | - */ |
|
34 | - public function getKey() { |
|
35 | - return $this->key; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string|null $key |
|
40 | - */ |
|
41 | - public function setKey(string $key = null) { |
|
42 | - $this->key = $key; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @return string|null |
|
47 | - */ |
|
48 | - public function getValue() { |
|
49 | - return $this->value; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param string|null $value |
|
54 | - */ |
|
55 | - public function setValue(string $value = null) { |
|
56 | - $this->value = $value; |
|
57 | - } |
|
12 | + /** |
|
13 | + * @var string |
|
14 | + * |
|
15 | + * @ORM\Id() |
|
16 | + * @ORM\Column(name="`key`", nullable=false, type="string", length=64) |
|
17 | + * |
|
18 | + * @Assert\NotNull() |
|
19 | + * @Assert\NotBlank() |
|
20 | + * @Assert\Length(max="64") |
|
21 | + */ |
|
22 | + private $key; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var string |
|
26 | + * |
|
27 | + * @ORM\Column(name="`value`", nullable=false, type="text") |
|
28 | + */ |
|
29 | + private $value; |
|
30 | + |
|
31 | + /** |
|
32 | + * @return string|null |
|
33 | + */ |
|
34 | + public function getKey() { |
|
35 | + return $this->key; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string|null $key |
|
40 | + */ |
|
41 | + public function setKey(string $key = null) { |
|
42 | + $this->key = $key; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @return string|null |
|
47 | + */ |
|
48 | + public function getValue() { |
|
49 | + return $this->value; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param string|null $value |
|
54 | + */ |
|
55 | + public function setValue(string $value = null) { |
|
56 | + $this->value = $value; |
|
57 | + } |
|
58 | 58 | } |
@@ -3,54 +3,54 @@ |
||
3 | 3 | namespace OneGuard\Bundle\DynamicConfigurationBundle; |
4 | 4 | |
5 | 5 | class DefinitionRegistry { |
6 | - /** |
|
7 | - * @var Definition[] |
|
8 | - */ |
|
9 | - private $definitions = []; |
|
10 | - |
|
11 | - /** |
|
12 | - * @param Definition $definition |
|
13 | - * @throws \InvalidArgumentException |
|
14 | - */ |
|
15 | - public function register(Definition $definition) { |
|
16 | - if (array_key_exists($definition->getKey(), $this->definitions)) { |
|
17 | - throw new \InvalidArgumentException('Definition already exists.'); |
|
18 | - } |
|
19 | - $this->definitions[$definition->getKey()] = $definition; |
|
20 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * @param string $key |
|
24 | - * @return Definition |
|
25 | - * @throws \InvalidArgumentException |
|
26 | - */ |
|
27 | - public function get(string $key) : Definition { |
|
28 | - if (!array_key_exists($key, $this->definitions)) { |
|
29 | - throw new \InvalidArgumentException('Definition not found.'); |
|
30 | - } |
|
31 | - return $this->definitions[$key]; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * @param string $key |
|
36 | - * @return bool |
|
37 | - */ |
|
38 | - public function has(string $key) : bool { |
|
39 | - return array_key_exists($key, $this->definitions); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @return string[] |
|
44 | - */ |
|
45 | - public function keys() : array { |
|
46 | - return array_keys($this->definitions); |
|
47 | - } |
|
48 | - |
|
49 | - public function registerEntity($key, $class, $choiceLabel) { |
|
50 | - $this->register(new EntityDefinition($key, $class, $choiceLabel)); |
|
51 | - } |
|
52 | - |
|
53 | - public function registerString($key) { |
|
54 | - $this->register(new StringDefinition($key)); |
|
55 | - } |
|
6 | + /** |
|
7 | + * @var Definition[] |
|
8 | + */ |
|
9 | + private $definitions = []; |
|
10 | + |
|
11 | + /** |
|
12 | + * @param Definition $definition |
|
13 | + * @throws \InvalidArgumentException |
|
14 | + */ |
|
15 | + public function register(Definition $definition) { |
|
16 | + if (array_key_exists($definition->getKey(), $this->definitions)) { |
|
17 | + throw new \InvalidArgumentException('Definition already exists.'); |
|
18 | + } |
|
19 | + $this->definitions[$definition->getKey()] = $definition; |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * @param string $key |
|
24 | + * @return Definition |
|
25 | + * @throws \InvalidArgumentException |
|
26 | + */ |
|
27 | + public function get(string $key) : Definition { |
|
28 | + if (!array_key_exists($key, $this->definitions)) { |
|
29 | + throw new \InvalidArgumentException('Definition not found.'); |
|
30 | + } |
|
31 | + return $this->definitions[$key]; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * @param string $key |
|
36 | + * @return bool |
|
37 | + */ |
|
38 | + public function has(string $key) : bool { |
|
39 | + return array_key_exists($key, $this->definitions); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @return string[] |
|
44 | + */ |
|
45 | + public function keys() : array { |
|
46 | + return array_keys($this->definitions); |
|
47 | + } |
|
48 | + |
|
49 | + public function registerEntity($key, $class, $choiceLabel) { |
|
50 | + $this->register(new EntityDefinition($key, $class, $choiceLabel)); |
|
51 | + } |
|
52 | + |
|
53 | + public function registerString($key) { |
|
54 | + $this->register(new StringDefinition($key)); |
|
55 | + } |
|
56 | 56 | } |
@@ -5,25 +5,25 @@ |
||
5 | 5 | use Symfony\Bridge\Doctrine\RegistryInterface; |
6 | 6 | |
7 | 7 | class ConfigurationResolverFactory { |
8 | - /** |
|
9 | - * @var RegistryInterface |
|
10 | - */ |
|
11 | - private $doctrine; |
|
8 | + /** |
|
9 | + * @var RegistryInterface |
|
10 | + */ |
|
11 | + private $doctrine; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @var DefinitionRegistry |
|
15 | - */ |
|
16 | - private $registry; |
|
13 | + /** |
|
14 | + * @var DefinitionRegistry |
|
15 | + */ |
|
16 | + private $registry; |
|
17 | 17 | |
18 | - public function __construct(RegistryInterface $doctrine, DefinitionRegistry $registry) { |
|
19 | - $this->doctrine = $doctrine; |
|
20 | - $this->registry = $registry; |
|
21 | - } |
|
18 | + public function __construct(RegistryInterface $doctrine, DefinitionRegistry $registry) { |
|
19 | + $this->doctrine = $doctrine; |
|
20 | + $this->registry = $registry; |
|
21 | + } |
|
22 | 22 | |
23 | - public function create(string $key) { |
|
24 | - return new ConfigurationResolver( |
|
25 | - $this->doctrine, |
|
26 | - $this->registry->get($key) |
|
27 | - ); |
|
28 | - } |
|
23 | + public function create(string $key) { |
|
24 | + return new ConfigurationResolver( |
|
25 | + $this->doctrine, |
|
26 | + $this->registry->get($key) |
|
27 | + ); |
|
28 | + } |
|
29 | 29 | } |
@@ -5,15 +5,15 @@ |
||
5 | 5 | use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
6 | 6 | |
7 | 7 | class ProtectedReferenceException extends \Exception implements HttpExceptionInterface { |
8 | - public function __construct($message = "", $code = 0, \Throwable $previous = null) { |
|
9 | - parent::__construct($message, $code, $previous); |
|
10 | - } |
|
8 | + public function __construct($message = "", $code = 0, \Throwable $previous = null) { |
|
9 | + parent::__construct($message, $code, $previous); |
|
10 | + } |
|
11 | 11 | |
12 | - public function getStatusCode() { |
|
13 | - return 400; |
|
14 | - } |
|
12 | + public function getStatusCode() { |
|
13 | + return 400; |
|
14 | + } |
|
15 | 15 | |
16 | - public function getHeaders() { |
|
17 | - return []; |
|
18 | - } |
|
16 | + public function getHeaders() { |
|
17 | + return []; |
|
18 | + } |
|
19 | 19 | } |
@@ -6,42 +6,42 @@ |
||
6 | 6 | use Symfony\Bridge\Doctrine\RegistryInterface; |
7 | 7 | |
8 | 8 | class ConfigurationResolver { |
9 | - /** |
|
10 | - * @var RegistryInterface |
|
11 | - */ |
|
12 | - private $doctrine; |
|
13 | - |
|
14 | - /** |
|
15 | - * @var Definition |
|
16 | - */ |
|
17 | - private $definition; |
|
18 | - |
|
19 | - public function __construct(RegistryInterface $doctrine, Definition $definition) { |
|
20 | - $this->doctrine = $doctrine; |
|
21 | - $this->definition = $definition; |
|
22 | - } |
|
23 | - |
|
24 | - public function getDefinition() : Definition { |
|
25 | - return $this->definition; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @return null|object |
|
30 | - * @throws \UnexpectedValueException |
|
31 | - */ |
|
32 | - public function resolve() { |
|
33 | - $repo = $this->doctrine->getRepository(ConfigurationValue::class); |
|
34 | - $configurationValue = $repo->find($this->definition->getKey()); |
|
35 | - |
|
36 | - switch (get_class($this->definition)) { |
|
37 | - case StringDefinition::class: |
|
38 | - return $configurationValue->getValue(); |
|
39 | - case EntityDefinition::class: |
|
40 | - /* @var $entityDefinition EntityDefinition */ |
|
41 | - $entityDefinition = $this->definition; |
|
42 | - return $this->doctrine->getRepository($entityDefinition->getClass())->find($configurationValue->getValue()); |
|
43 | - default: |
|
44 | - throw new \UnexpectedValueException(); |
|
45 | - } |
|
46 | - } |
|
9 | + /** |
|
10 | + * @var RegistryInterface |
|
11 | + */ |
|
12 | + private $doctrine; |
|
13 | + |
|
14 | + /** |
|
15 | + * @var Definition |
|
16 | + */ |
|
17 | + private $definition; |
|
18 | + |
|
19 | + public function __construct(RegistryInterface $doctrine, Definition $definition) { |
|
20 | + $this->doctrine = $doctrine; |
|
21 | + $this->definition = $definition; |
|
22 | + } |
|
23 | + |
|
24 | + public function getDefinition() : Definition { |
|
25 | + return $this->definition; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @return null|object |
|
30 | + * @throws \UnexpectedValueException |
|
31 | + */ |
|
32 | + public function resolve() { |
|
33 | + $repo = $this->doctrine->getRepository(ConfigurationValue::class); |
|
34 | + $configurationValue = $repo->find($this->definition->getKey()); |
|
35 | + |
|
36 | + switch (get_class($this->definition)) { |
|
37 | + case StringDefinition::class: |
|
38 | + return $configurationValue->getValue(); |
|
39 | + case EntityDefinition::class: |
|
40 | + /* @var $entityDefinition EntityDefinition */ |
|
41 | + $entityDefinition = $this->definition; |
|
42 | + return $this->doctrine->getRepository($entityDefinition->getClass())->find($configurationValue->getValue()); |
|
43 | + default: |
|
44 | + throw new \UnexpectedValueException(); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | } |