Skip to content

Commit

Permalink
[FEATURE] includes needed classes of "jackiedo/dotenv-editor" locally…
Browse files Browse the repository at this point in the history
… to reduce dependencies

[TASK] refactores Companion and DotEnv/* classes
[TASK] adds/ updates test cases
  • Loading branch information
EvilBMP committed Jul 13, 2024
1 parent 0208169 commit 9802381
Show file tree
Hide file tree
Showing 22 changed files with 1,305 additions and 16 deletions.
4 changes: 4 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
MIT License

Copyright (c) 2017 Anh Vũ Đỗ
Copyright (c) 2018 Samuel Rozé
Copyright (c) 2023 Axel Böswetter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
],
"require": {
"php": ">=8.0",
"jackiedo/dotenv-editor": "^2.0",
"symfony/console": "^6.0 || ^7.0",
"symfony/dotenv": "^6.0 || ^7.0",
"symfony/process": "^6.0 || ^7.0"
Expand Down
28 changes: 28 additions & 0 deletions features/fill-variables.feature
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ Feature:
A_BASE64_VALUE=abc123=
"""

Scenario: It quotes variable values containing whitespaces
Given the file ".env.dist" contains:
"""
## Something
MY_VARIABLE=
"""
When I run the companion with the following answers:
| Let's fix this? (y) | y |
| MY_VARIABLE ? | Some string with spaces |
And the file ".env" should contain:
"""
MY_VARIABLE='Some string with spaces'
"""

Scenario: It quotes variable values containing special characters
Given the file ".env.dist" contains:
"""
## Something
MY_VARIABLE=
"""
When I run the companion with the following answers:
| Let's fix this? (y) | y |
| MY_VARIABLE ? | S0me$Value"With^Sp3cial&Chars% |
And the file ".env" should contain:
"""
MY_VARIABLE='S0me$Value"With^Sp3cial&Chars%'
"""

Scenario: It displays correctly boolean with 1 and 0
Given the file ".env.dist" contains:
"""
Expand Down
18 changes: 10 additions & 8 deletions src/Companienv/Companion.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use Companienv\DotEnv\File;
use Companienv\DotEnv\MissingVariable;
use Companienv\DotEnv\Parser;
use Companienv\DotEnvEditor\DotenvReader;
use Companienv\DotEnvEditor\DotenvWriter;
use Companienv\DotEnvEditor\Workers\Formatters\Formatter;
use Companienv\DotEnvEditor\Workers\Parsers\ParserV3;
use Companienv\IO\FileSystem\FileSystem;
use Companienv\IO\Interaction;
use Jackiedo\DotenvEditor\DotenvReader;
use Jackiedo\DotenvEditor\DotenvWriter;
use Jackiedo\DotenvEditor\Workers\Formatters\Formatter;
use Jackiedo\DotenvEditor\Workers\Parsers\ParserV3;
use Symfony\Component\Dotenv\Dotenv;

class Companion
{
Expand Down Expand Up @@ -97,6 +98,7 @@ private function writeVariable(string $name, ?string $value = null): void

$writer = new DotenvWriter(new Formatter());
$reader = (new DotenvReader(new ParserV3()))->load($this->envFileName);

foreach ($reader->entries(true) as $entry) {
if (isset($entry['parsed_data'])) {
switch ($entry['parsed_data']['type']) {
Expand All @@ -109,7 +111,7 @@ private function writeVariable(string $name, ?string $value = null): void
case 'setter':
$writer->appendSetter(
$entry['parsed_data']['key'],
trim($entry['parsed_data']['value'], '"'),
$entry['parsed_data']['value'],
$entry['parsed_data']['comment'],
$entry['parsed_data']['export']
);
Expand All @@ -124,7 +126,7 @@ private function writeVariable(string $name, ?string $value = null): void
$writer->appendSetter($name, $value);
}

$this->fileSystem->write($this->envFileName, $writer->getBuffer(false));
$this->fileSystem->write($this->envFileName, $writer->getBufferAsString());
}

/**
Expand All @@ -137,7 +139,7 @@ private function getVariablesRequiringValues(): array

foreach ($this->reference->getBlocks() as $block) {
foreach ($block->getVariables() as $variable) {
$currentValue = isset($variablesInFile[$variable->getName()]) ? $variablesInFile[$variable->getName()] : null;
$currentValue = $variablesInFile[$variable->getName()] ?? null;

if ($this->extension->isVariableRequiringValue($this, $block, $variable, $currentValue) === Extension::VARIABLE_REQUIRED) {
$missingVariables[$variable->getName()] = new MissingVariable($variable, $currentValue);
Expand All @@ -155,7 +157,7 @@ public function getDefinedVariablesHash(): array
{
$variablesInFile = [];
if ($this->fileSystem->exists($this->envFileName)) {
$dotEnv = new \Symfony\Component\Dotenv\Dotenv();
$dotEnv = new Dotenv();
$variablesInFile = $dotEnv->parse($this->fileSystem->getContents($this->envFileName), $this->envFileName);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Companienv/DotEnv/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public function getVariables(): array
*/
public function getVariablesInBlock(array $variables): array
{
$blockVariableNames = array_map(function (Variable $variable) {
$blockVariableNames = array_map(static function (Variable $variable) {
return $variable->getName();
}, $this->variables);

return array_filter($variables, function (Variable $variable) use ($blockVariableNames) {
return array_filter($variables, static function (Variable $variable) use ($blockVariableNames) {
return in_array($variable->getName(), $blockVariableNames, true);
});
}
Expand Down Expand Up @@ -112,7 +112,7 @@ public function getAttribute(string $name, Variable $forVariable = null): ?Attri
{
foreach ($this->attributes as $attribute) {
if (
$attribute->getName() == $name
$attribute->getName() === $name
&& (
$forVariable === null
|| in_array($forVariable->getName(), $attribute->getVariableNames(), true)
Expand Down
10 changes: 6 additions & 4 deletions src/Companienv/DotEnv/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Companienv\DotEnv;

use Companienv\IO\FileSystem\FileSystem;
use Symfony\Component\Dotenv\Dotenv;

class Parser
{
Expand Down Expand Up @@ -37,10 +38,11 @@ public function parse(FileSystem $fileSystem, string $path): File
$blocks[] = $block = new Block();
}

$block->addVariable(new Variable(
substr($line, 0, $firstEquals),
substr($line, $firstEquals + 1)
));
$parsedLine = (new Dotenv())->parse($line);
$name = (string)key($parsedLine);
$value = (string)current($parsedLine);

$block->addVariable(new Variable($name, $value));
} else {
throw new \InvalidArgumentException(sprintf(
'The line %d of the file %s is invalid: %s',
Expand Down
37 changes: 37 additions & 0 deletions src/Companienv/DotEnvEditor/Contracts/FormatterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Companienv\DotEnvEditor\Contracts;

interface FormatterInterface
{
/**
* Formatting the key of setter to writing.
*
* @param string $key
* @param bool $export optional
*
* @return string
*/
public function formatKey(string $key, bool $export = false);

/**
* Build an setter from the individual components for writing.
*
* @param string $key
* @param string|null $value optional
* @param string|null $comment optional
* @param bool $export optional
*
* @return string
*/
public function formatSetter(string $key, ?string $value = null, ?string $comment = null, bool $export = false);

/**
* Formatting the comment to writing.
*
* @param string $comment
*
* @return string
*/
public function formatComment(?string $comment);
}
28 changes: 28 additions & 0 deletions src/Companienv/DotEnvEditor/Contracts/ParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Companienv\DotEnvEditor\Contracts;

interface ParserInterface
{
/**
* Parse dotenv file content into separate entries.
*
* This will produce an array of entries, each entry
* being an informational array of starting line and raw data.
*
* @param string $filePath The path to dotenv file
*
* @return array<int, mixed[]>
*/
public function parseFile(string $filePath): array;

/**
* Parses an entry data into an array of type, export allowed or not,
* key, value, and comment information.
*
* @param string $data The entry data
*
* @return array<string, mixed>
*/
public function parseEntry(string $data): array;
}
33 changes: 33 additions & 0 deletions src/Companienv/DotEnvEditor/Contracts/ReaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Companienv\DotEnvEditor\Contracts;

interface ReaderInterface
{
/**
* Load .env file.
*
* @param string $filePath The path to dotenv file
*/
public function load(string $filePath): self;

/**
* Get content of .env file.
*/
public function content(): string;

/**
* Get information of all entries from file content.
*
* @param bool $withParsedData Includes the parsed data in the result
* @return mixed[]
*/
public function entries(bool $withParsedData = false): array;

/**
* Get all key information in .env file.
*
* @return mixed[]
*/
public function keys(): array;
}
73 changes: 73 additions & 0 deletions src/Companienv/DotEnvEditor/Contracts/WriterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Companienv\DotEnvEditor\Contracts;

interface WriterInterface
{
/**
* Load current content into buffer.
*
* @param array<int, mixed[]> $content
*/
public function setBuffer(array $content): self;

/**
* Return content in buffer.
*
* @return array<int, mixed[]>
*/
public function getBuffer(): array;

/**
* Return content in buffer.
*
* @return string
*/
public function getBufferAsString(): string;

/**
* Append empty line to buffer.
*/
public function appendEmpty(): self;

/**
* Append comment line to buffer.
*
* @param string $comment
*/
public function appendComment(string $comment): self;

/**
* Append one setter to buffer.
*
* @param string $key
* @param string|null $value
* @param string|null $comment
* @param bool $export
*/
public function appendSetter(string $key, ?string $value = null, ?string $comment = null, bool $export = false): self;

/**
* Update one setter in buffer.
*
* @param string $key
* @param string|null $value
* @param string|null $comment
* @param bool $export
*/
public function updateSetter(string $key, ?string $value = null, ?string $comment = null, bool $export = false): self;

/**
* Delete one setter in buffer.
*
* @param string $key
*/
public function deleteSetter(string $key): self;

/**
* Save buffer to special file.
*
* @param string $filePath
*/
public function saveTo(string $filePath): self;
}
Loading

0 comments on commit 9802381

Please sign in to comment.