|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Frosh\Tools\Controller; |
| 4 | + |
| 5 | +use Frosh\Tools\Components\Environment\EnvironmentManager; |
| 6 | +use Shopware\Core\Framework\Feature; |
| 7 | +use Shopware\Core\Framework\Routing\Annotation\RouteScope; |
| 8 | +use Shopware\Core\Framework\Routing\Exception\InvalidRequestParameterException; |
| 9 | +use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException; |
| 10 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 14 | +use Symfony\Component\Routing\Annotation\Route; |
| 15 | + |
| 16 | +/** |
| 17 | + * @RouteScope(scopes={"api"}) |
| 18 | + * @Route(path="/api/_action/frosh-tools") |
| 19 | + */ |
| 20 | +class FeatureFlagController |
| 21 | +{ |
| 22 | + private string $envPath; |
| 23 | + |
| 24 | + public function __construct(string $projectDir) |
| 25 | + { |
| 26 | + $this->envPath = $projectDir . '/.env'; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @Route(path="/feature-flag/list", methods={"GET"}, name="api.frosh.tools.feature-flag.list") |
| 31 | + */ |
| 32 | + public function list(): JsonResponse |
| 33 | + { |
| 34 | + $featureFlags = Feature::getRegisteredFeatures(); |
| 35 | + |
| 36 | + foreach (array_keys($featureFlags) as $featureFlag) { |
| 37 | + $featureFlags[$featureFlag]['flag'] = $featureFlag; |
| 38 | + $featureFlags[$featureFlag]['active'] = Feature::isActive($featureFlag); |
| 39 | + } |
| 40 | + |
| 41 | + return new JsonResponse(array_values($featureFlags)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @Route(path="/feature-flag/toggle", methods={"POST"}, name="api.frosh.tools.feature-flag.toggle") |
| 46 | + */ |
| 47 | + public function toggle(Request $request): Response |
| 48 | + { |
| 49 | + $featureFlag = $request->get('flag'); |
| 50 | + |
| 51 | + if (!file_exists($this->envPath)) { |
| 52 | + throw new HttpException( |
| 53 | + Response::HTTP_INTERNAL_SERVER_ERROR, |
| 54 | + sprintf('File at %s does not exist', $this->envPath) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + if (empty($featureFlag)) { |
| 59 | + throw new MissingRequestParameterException('flag'); |
| 60 | + } |
| 61 | + |
| 62 | + if (!Feature::has($featureFlag)) { |
| 63 | + throw new InvalidRequestParameterException('flag'); |
| 64 | + } |
| 65 | + |
| 66 | + $this->updateEnvFile($featureFlag); |
| 67 | + |
| 68 | + return new Response('', Response::HTTP_NO_CONTENT); |
| 69 | + } |
| 70 | + |
| 71 | + private function updateEnvFile(string $featureFlag): void |
| 72 | + { |
| 73 | + $manager = new EnvironmentManager(); |
| 74 | + $file = $manager->read($this->envPath); |
| 75 | + |
| 76 | + if (Feature::isActive($featureFlag)) { |
| 77 | + $file->set($featureFlag, '0'); |
| 78 | + } else { |
| 79 | + $file->set($featureFlag, '1'); |
| 80 | + } |
| 81 | + |
| 82 | + $manager->save($this->envPath, $file); |
| 83 | + } |
| 84 | +} |
0 commit comments