-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Dotenv dumper and use it in the install tool and manager bundle
- Loading branch information
Showing
7 changed files
with
175 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\InstallationBundle\Config; | ||
|
||
use Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
class DotenvDumper | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $dotenvFile; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $parameters = []; | ||
|
||
public function __construct(string $dotenvFile, Filesystem $filesystem = null) | ||
{ | ||
$this->dotenvFile = $dotenvFile; | ||
$this->filesystem = $filesystem ?: new Filesystem(); | ||
|
||
if (!file_exists($dotenvFile)) { | ||
return; | ||
} | ||
|
||
$parameters = (new Dotenv(false))->parse(file_get_contents($dotenvFile)); | ||
|
||
if (0 !== \count($parameters)) { | ||
$this->parameters = array_merge($this->parameters, $parameters); | ||
} | ||
} | ||
|
||
public function setParameter(string $name, $value): void | ||
{ | ||
$this->parameters[$name] = $value; | ||
} | ||
|
||
public function setParameters(array $params): void | ||
{ | ||
foreach ($params as $name => $value) { | ||
$this->setParameter($name, $value); | ||
} | ||
} | ||
|
||
public function unsetParameter(string $name): void | ||
{ | ||
unset($this->parameters[$name]); | ||
} | ||
|
||
/** | ||
* Dumps the parameters into the .env file. | ||
*/ | ||
public function dump(): void | ||
{ | ||
// Remove the .env file if there are no parameters | ||
if (0 === \count($this->parameters)) { | ||
$this->filesystem->remove($this->dotenvFile); | ||
|
||
return; | ||
} | ||
|
||
$parameters = []; | ||
|
||
foreach ($this->parameters as $key => $value) { | ||
$parameters[] = $key.'='.$this->escape($value); | ||
} | ||
|
||
$this->filesystem->dumpFile($this->dotenvFile, implode("\n", $parameters)."\n"); | ||
} | ||
|
||
private function escape($value) | ||
{ | ||
if (\is_string($value)) { | ||
return escapeshellarg($value); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\InstallationBundle\Tests\Config; | ||
|
||
use Contao\InstallationBundle\Config\DotenvDumper; | ||
use Contao\TestCase\ContaoTestCase; | ||
|
||
class DotenvDumperTest extends ContaoTestCase | ||
{ | ||
public function testDumpsADotenvFile(): void | ||
{ | ||
$dotenv = new DotenvDumper($this->getTempDir().'/.env'); | ||
$dotenv->setParameter('FOO1', 42); | ||
$dotenv->setParameter('FOO2', '42'); | ||
$dotenv->setParameter('FOO3', 'String'); | ||
$dotenv->setParameter('FOO4', 'String with spaces'); | ||
$dotenv->setParameter('FOO5', '"DoubleQuotes"'); | ||
$dotenv->setParameter('FOO6', "'SingleQuotes'"); | ||
$dotenv->setParameter('FOO7', '$variable'); | ||
$dotenv->setParameter('FOO8', 'String with "double quotes" and \'single quotes\' and a $variable'); | ||
$dotenv->dump(); | ||
|
||
$expected = <<<'EOT' | ||
FOO1=42 | ||
FOO2='42' | ||
FOO3='String' | ||
FOO4='String with spaces' | ||
FOO5='"DoubleQuotes"' | ||
FOO6=''\''SingleQuotes'\''' | ||
FOO7='$variable' | ||
FOO8='String with "double quotes" and '\''single quotes'\'' and a $variable' | ||
EOT; | ||
|
||
$this->assertSame($expected, file_get_contents($this->getTempDir().'/.env')); | ||
} | ||
|
||
public function testSupportsUnsettingParameters(): void | ||
{ | ||
$dotenv = new DotenvDumper($this->getTempDir().'/.env.local'); | ||
$dotenv->setParameter('FOO', 'bar'); | ||
$dotenv->setParameter('BAR', 'foo'); | ||
$dotenv->unsetParameter('FOO'); | ||
$dotenv->dump(); | ||
|
||
$this->assertSame("BAR='foo'\n", file_get_contents($this->getTempDir().'/.env.local')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters