-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSettingsFormFactory.php
More file actions
159 lines (133 loc) · 7.19 KB
/
Copy pathSettingsFormFactory.php
File metadata and controls
159 lines (133 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
* This file is part of jbtronics/settings-bundle (https://github.com/jbtronics/settings-bundle).
*
* Copyright (c) 2024 Jan Böhmer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
declare(strict_types=1);
namespace Jbtronics\SettingsBundle\Form;
use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface;
use Jbtronics\SettingsBundle\Metadata\MetadataManagerInterface;
use Jbtronics\SettingsBundle\Metadata\SettingsMetadata;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
final class SettingsFormFactory implements SettingsFormFactoryInterface
{
public function __construct(
private readonly SettingsManagerInterface $settingsManager,
private readonly MetadataManagerInterface $metadataManager,
private readonly SettingsFormBuilderInterface $settingsFormBuilder,
private readonly FormFactoryInterface $formFactory,
) {
}
public function createSettingsForm(string $settingsName): FormInterface
{
return $this->createSettingsFormBuilder($settingsName)->getForm();
}
public function createSettingsFormBuilder(string|object $settings, ?array $groups = null, array $formOptions = []): FormBuilderInterface
{
$settingsMetadata = $this->metadataManager->getSettingsMetadata($settings);
//Ensure that the embedded settings do not cause a infinite loop
if ($this->checkForCircularEmbedded($settingsMetadata, $groups)) {
throw new \LogicException(
sprintf('The settings class "%s" have a circular embedded settings structure, which would cause an infinite loop while form building. Use the groups option to only render a subset of the embedded settings to prevent circular loops.',
$settingsMetadata->getClassName(),
));
}
//If a string is passed, get the current settings instance from the settings manager
//This is deprecated
if (is_string($settings)) {
trigger_deprecation('jbtronics/settings-bundle', '2.0', 'Passing a string as settings name to createSettingsFormBuilder() is deprecated. Pass the settings instance directly instead.');
$settings = $this->settingsManager->get($settings);
}
$formOptions['settings_metadata'] = $settingsMetadata;
$formOptions['help'] = $formOptions['help'] ?? $settingsMetadata->getDescription();
$formBuilder = $this->formFactory->createBuilder(data: $settings, options: $formOptions);
$this->settingsFormBuilder->buildSettingsForm($formBuilder, $settingsMetadata, [], groups: $groups);
return $formBuilder;
}
public function createMultiSettingsFormBuilder(
array $settings,
?array $groups = null,
array $formOptions = [],
array $rootFormOptions = []
): FormBuilderInterface
{
$deprecation_triggered = false;
$formBuilder = $this->formFactory->createBuilder(options: $rootFormOptions);
//The form is a compound form, so we need to set this to true
$formBuilder->setCompound(true);
foreach ($settings as $setting) {
$settingsMetadata = $this->metadataManager->getSettingsMetadata($setting);
//Ensure that the embedded settings do not cause a infinite loop
if ($this->checkForCircularEmbedded($settingsMetadata, $groups)) {
throw new \LogicException(
sprintf('The settings class "%s" have a circular embedded settings structure, which would cause an infinite loop while form building. Use the groups option to only render a subset of the embedded settings to prevent circular loops.',
$settingsMetadata->getClassName(),
));
}
//If a string is passed, get the current settings instance from the settings manager
//This is deprecated
if (is_string($setting)) {
if (!$deprecation_triggered) { //Only trigger the deprecation once
trigger_deprecation('jbtronics/settings-bundle', '2.0',
'Passing a string as settings name to createSettingsFormBuilder() is deprecated. Pass the settings instance directly instead.');
}
$deprecation_triggered = true;
$setting = $this->settingsManager->get($setting);
}
$formOptions['settings_metadata'] = $settingsMetadata;
//Create sub form builder for the settings with the name of the settings class
$subBuilder = $this->formFactory->createNamedBuilder($settingsMetadata->getName(),
data: $setting, options: $formOptions);
//Configure the sub form builder
$this->settingsFormBuilder->buildSettingsForm($subBuilder, $settingsMetadata, [], groups: $groups);
//And add it to the main form builder
$formBuilder->add($subBuilder);
}
return $formBuilder;
}
/**
* Checks if the given settings have a circular embedded settings structure, which would cause an infinite loop.
* It is checked for the given groups, if null all embedded settings are checked.
* @param SettingsMetadata $metadata
* @param array|null $groups
* @param array $already_checked
* @return bool
*/
public function checkForCircularEmbedded(SettingsMetadata $metadata, ?array $groups = null, array $already_checked = []): bool
{
$already_checked[] = $metadata->getClassName();
$embeddedToRender = $groups === null ? $metadata->getEmbeddedSettings() : $metadata->getEmbeddedSettingsWithOneOfGroups($groups);
foreach ($embeddedToRender as $embeddedMetadata) {
if (in_array($embeddedMetadata->getTargetClass(), $already_checked, true)) {
return true;
}
$already_checked[] = $embeddedMetadata->getTargetClass();
$embeddedMeta = $this->metadataManager->getSettingsMetadata($embeddedMetadata->getTargetClass());
if ($this->checkForCircularEmbedded($embeddedMeta, $groups, $already_checked)) {
return true;
}
}
return false;
}
}