oliverde8 /
comfyEasyAdminBundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace oliverde8\ComfyEasyAdminBundle\Controller; |
||
| 4 | |||
| 5 | |||
| 6 | use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem; |
||
| 7 | use oliverde8\ComfyBundle\Form\Type\ConfigsForm; |
||
| 8 | use oliverde8\ComfyBundle\Manager\ConfigDisplayManager; |
||
| 9 | use oliverde8\ComfyBundle\Manager\ConfigManagerInterface; |
||
| 10 | use oliverde8\ComfyBundle\Model\ConfigInterface; |
||
| 11 | use oliverde8\ComfyBundle\Resolver\ScopeResolverInterface; |
||
| 12 | use oliverde8\ComfyEasyAdminBundle\Security\Voter\ConfigEditVoter; |
||
| 13 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||
| 14 | use Symfony\Component\HttpFoundation\Request; |
||
| 15 | use Symfony\Component\HttpFoundation\Response; |
||
| 16 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
| 17 | use Symfony\Component\Routing\Annotation\Route; |
||
| 18 | |||
| 19 | class ConfigController extends AbstractController |
||
| 20 | { |
||
| 21 | protected ConfigManagerInterface $configManger; |
||
| 22 | protected ScopeResolverInterface $scopeResolver; |
||
| 23 | protected ConfigDisplayManager $configDisplayManager; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * ConfigController constructor. |
||
| 27 | * |
||
| 28 | * @param ConfigManagerInterface $configManger |
||
| 29 | * @param ScopeResolverInterface $scopeResolver |
||
| 30 | * @param ConfigDisplayManager $configDisplayManager |
||
| 31 | */ |
||
| 32 | public function __construct(ConfigManagerInterface $configManger, ScopeResolverInterface $scopeResolver, ConfigDisplayManager $configDisplayManager) |
||
| 33 | { |
||
| 34 | $this->configManger = $configManger; |
||
| 35 | $this->scopeResolver = $scopeResolver; |
||
| 36 | $this->configDisplayManager = $configDisplayManager; |
||
| 37 | } |
||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * @Route("/comfy/configs", name="comfy_configs") |
||
| 42 | */ |
||
| 43 | public function index(Request $request): Response |
||
| 44 | { |
||
| 45 | $scope = $this->getConfigScopeFromRequest($request); |
||
| 46 | $configPath = $this->getConfigPathFromRequest($request); |
||
| 47 | $configs = $this->getConfigsForPath($configPath); |
||
| 48 | |||
| 49 | $form = $this->createForm(ConfigsForm::class, ['scope' => $scope, 'configs' => $configs]); |
||
| 50 | $form->handleRequest($request); |
||
| 51 | if ($form->isSubmitted()) { |
||
| 52 | if ($form->isValid()) { |
||
| 53 | // We need to recreate the form because config won't take their inheritance properly into account untill all |
||
| 54 | // of them are saved. |
||
| 55 | $form = $this->createForm(ConfigsForm::class, ['scope' => $scope, 'configs' => $configs]); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | return $this->render( |
||
| 60 | "@oliverde8ComfyEasyAdmin/config.html.twig", |
||
| 61 | [ |
||
| 62 | 'form' => $form->createView(), |
||
| 63 | 'config_path' => $configPath, |
||
| 64 | 'config_keys' => $this->getConfigKeys($configs), |
||
| 65 | 'config_tree' => $this->configManger->getAllConfigs()->getArray(), |
||
| 66 | 'scope' => $scope, |
||
| 67 | 'scopes' => $this->configDisplayManager->getScopeTreeForHtml(), |
||
| 68 | ] |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the config path to use. |
||
| 75 | * |
||
| 76 | * @param Request $request |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | protected function getConfigPathFromRequest(Request $request): string |
||
| 80 | { |
||
| 81 | $configPath = $request->get('config', null); |
||
| 82 | $configPath = str_replace(".", "/", $configPath); |
||
| 83 | $configPath = ltrim($configPath, '/'); |
||
| 84 | |||
| 85 | if (empty($configPath)) { |
||
| 86 | $configPath = $this->configDisplayManager->getRecursiveFirstConfigPath($this->configManger->getAllConfigs()->getArray()); |
||
| 87 | $configPath = ltrim($configPath, '/'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 88 | } |
||
| 89 | |||
| 90 | return $configPath; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the scope we are editing the configs for. |
||
| 95 | * |
||
| 96 | * @param Request $request |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | protected function getConfigScopeFromRequest(Request $request): string |
||
| 100 | { |
||
| 101 | $scope = $this->scopeResolver->getScope($request->get("scope", null)); |
||
| 102 | |||
| 103 | if (!$this->scopeResolver->validateScope($scope)) { |
||
| 104 | throw new NotFoundHttpException("Unknown scope."); |
||
| 105 | } |
||
| 106 | |||
| 107 | return $scope; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get modifiable configs for this path. |
||
| 112 | * |
||
| 113 | * @param Request $request |
||
| 114 | * @return ConfigInterface[] |
||
| 115 | * |
||
| 116 | * @throws NotFoundHttpException |
||
| 117 | */ |
||
| 118 | protected function getConfigsForPath(string $configPath): array |
||
| 119 | { |
||
| 120 | /** @var ConfigInterface[] $configs */ |
||
| 121 | $configs = $this->configManger->getAllConfigs()->get($configPath, []); |
||
| 122 | $configs = $this->filterAllowedConfigs($configs); |
||
| 123 | |||
| 124 | if (empty($configs)) { |
||
| 125 | throw new NotFoundHttpException("Unknown config path."); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $configs; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param ConfigInterface[] $configs |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | protected function filterAllowedConfigs(array $configs) |
||
| 136 | { |
||
| 137 | $allowedConfigs = []; |
||
| 138 | foreach ($configs as $config) { |
||
| 139 | if ($config->isHidden()) { |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($this->isGranted(ConfigEditVoter::ACTION_NEW, $config)) { |
||
| 144 | $allowedConfigs[] = $config; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return $allowedConfigs; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param Request $request |
||
| 153 | * @param ConfigInterface[] $configs |
||
| 154 | */ |
||
| 155 | protected function getSubmitedData(Request $request, $configs) |
||
| 156 | { |
||
| 157 | $data = []; |
||
| 158 | foreach ($configs as $config) { |
||
| 159 | $name = $this->configDisplayManager->getConfigHtmlName($config); |
||
| 160 | $data[$config->getPath()] = $request->get($name); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | protected function getConfigKeys(array $configs) |
||
| 165 | { |
||
| 166 | $configKeys = []; |
||
| 167 | foreach ($configs as $config) { |
||
| 168 | $configKeys[] = $this->configDisplayManager->getConfigHtmlName($config); |
||
| 169 | } |
||
| 170 | |||
| 171 | return $configKeys; |
||
| 172 | } |
||
| 173 | } |
||
| 174 |