Skip to content

Commit ca78338

Browse files
committed
use EnvironmentManager to write .env file
1 parent f384e44 commit ca78338

1 file changed

Lines changed: 14 additions & 20 deletions

File tree

src/Controller/FeatureFlagController.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Frosh\Tools\Controller;
44

5+
use Frosh\Tools\Components\Environment\EnvironmentManager;
56
use Shopware\Core\Framework\Feature;
67
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
78
use Shopware\Core\Framework\Routing\Exception\InvalidRequestParameterException;
89
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
9-
use Symfony\Component\Dotenv\Dotenv;
1010
use Symfony\Component\HttpFoundation\JsonResponse;
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\HttpFoundation\Response;
@@ -19,11 +19,11 @@
1919
*/
2020
class FeatureFlagController
2121
{
22-
private string $projectDir;
22+
private string $envPath;
2323

2424
public function __construct(string $projectDir)
2525
{
26-
$this->projectDir = $projectDir;
26+
$this->envPath = $projectDir . '/.env';
2727
}
2828

2929
/**
@@ -48,10 +48,10 @@ public function toggle(Request $request): Response
4848
{
4949
$featureFlag = $request->get('flag');
5050

51-
if (!file_exists($this->projectDir . '/.env')) {
51+
if (!file_exists($this->envPath)) {
5252
throw new HttpException(
5353
Response::HTTP_INTERNAL_SERVER_ERROR,
54-
sprintf('File at %s does not exist', $this->projectDir . '/.env')
54+
sprintf('File at %s does not exist', $this->envPath)
5555
);
5656
}
5757

@@ -63,28 +63,22 @@ public function toggle(Request $request): Response
6363
throw new InvalidRequestParameterException('flag');
6464
}
6565

66-
$dotEnvParsed = (new Dotenv())->parse(file_get_contents($this->projectDir . '/.env'));
67-
68-
if (Feature::isActive($featureFlag)) {
69-
$dotEnvParsed[$featureFlag] = 0;
70-
} else {
71-
$dotEnvParsed[$featureFlag] = 1;
72-
}
73-
74-
$this->updateEnvFile($dotEnvParsed);
66+
$this->updateEnvFile($featureFlag);
7567

7668
return new Response('', Response::HTTP_NO_CONTENT);
7769
}
7870

79-
private function updateEnvFile(array $configuration): void
71+
private function updateEnvFile(string $featureFlag): void
8072
{
81-
$envVars = '';
82-
$envFile = $this->projectDir . '/.env';
73+
$manager = new EnvironmentManager();
74+
$file = $manager->read($this->envPath);
8375

84-
foreach ($configuration as $key => $value) {
85-
$envVars .= $key . '=' . $value . \PHP_EOL;
76+
if (Feature::isActive($featureFlag)) {
77+
$file->set($featureFlag, '0');
78+
} else {
79+
$file->set($featureFlag, '1');
8680
}
8781

88-
file_put_contents($envFile, $envVars);
82+
$manager->save($this->envPath, $file);
8983
}
9084
}

0 commit comments

Comments
 (0)