aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Compiler/Compiler.php42
-rw-r--r--src/Compiler/CompilerInterface.php10
-rw-r--r--src/Dotenv.php7
-rw-r--r--src/Parser/Parser.php6
-rw-r--r--src/Parser/ParserInterface.php6
-rw-r--r--src/Types/KeyValue.php53
6 files changed, 33 insertions, 91 deletions
diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php
index 6a587d2..28b60e4 100644
--- a/src/Compiler/Compiler.php
+++ b/src/Compiler/Compiler.php
@@ -9,7 +9,6 @@ declare(strict_types=1);
namespace NeonXP\Dotenv\Compiler;
use NeonXP\Dotenv\Exception\RuntimeException;
-use NeonXP\Dotenv\Types\KeyValue;
/**
* Class Compiler
@@ -20,61 +19,64 @@ class Compiler implements CompilerInterface
const REGEX_VARIABLE = '/\$\{(.+?)\}/';
/**
- * @var KeyValue[]
+ * @var array[]
*/
protected $collection = [];
/**
- * @var KeyValue[]
+ * @var array[]
*/
protected $cache = [];
/**
* @inheritdoc
- * @param KeyValue[] $collection
+ * @param array[] $collection
*/
public function setRawCollection(array $collection): void
{
$this->collection = [];
$this->cache = [];
- foreach ($collection as $keyValue) {
- $this->collection[$keyValue->getKey()] = $keyValue;
+ foreach ($collection as $array) {
+ $this->collection[$array['key']] = $array;
}
}
/**
* @inheritdoc
- * @param KeyValue $keyValue
- * @return KeyValue
+ * @param array $array
+ * @return array
*/
- public function compileKeyValue(KeyValue $keyValue): KeyValue
+ public function compile(array $array): array
{
- $newValue = preg_replace_callback(self::REGEX_VARIABLE, function ($variable) use ($keyValue) {
+ $newValue = preg_replace_callback(self::REGEX_VARIABLE, function ($variable) use ($array) {
$variable = $variable[1];
- if ($variable === $keyValue->getKey()) {
+ if ($variable === $array['key']) {
throw new RuntimeException('Self referencing');
}
if (isset($this->cache[$variable])) {
- return $this->cache[$variable]->getValue();
+ return $this->cache[$variable]['value'];
} elseif (isset($this->collection[$variable]) && !$this->needToCompile($this->collection[$variable])) {
- return $this->collection[$variable]->getValue();
+ return $this->collection[$variable]['value'];
} elseif (isset($this->collection[$variable]) && $this->needToCompile($this->collection[$variable])) {
- return $this->compileKeyValue($this->collection[$variable])->getValue();
+ return $this->compile($this->collection[$variable])['value'];
}
return "UNKNOWN VARIABLE {$variable}";
- }, $keyValue->getValue());
- $result = new KeyValue($keyValue->getKey(), $newValue);
- $this->cache[$result->getKey()] = $result;
+ }, $array['value']);
+ $result = [
+ 'key' => $array['key'],
+ 'value' => $newValue
+ ];
+ $this->cache[$result['key']] = $result;
return $result;
}
/**
- * @param KeyValue $keyValue
+ * @param array $array
* @return bool
*/
- protected function needToCompile(KeyValue $keyValue): bool
+ protected function needToCompile(array $array): bool
{
- return !!preg_match(self::REGEX_VARIABLE, $keyValue->getValue());
+ return !!preg_match(self::REGEX_VARIABLE, $array['value']);
}
} \ No newline at end of file
diff --git a/src/Compiler/CompilerInterface.php b/src/Compiler/CompilerInterface.php
index 15b6823..22daad7 100644
--- a/src/Compiler/CompilerInterface.php
+++ b/src/Compiler/CompilerInterface.php
@@ -8,8 +8,6 @@ declare(strict_types=1);
namespace NeonXP\Dotenv\Compiler;
-use NeonXP\Dotenv\Types\KeyValue;
-
/**
* Interface CompilerInterface
* @package NeonXP\Dotenv\Compiler
@@ -17,13 +15,13 @@ use NeonXP\Dotenv\Types\KeyValue;
interface CompilerInterface
{
/**
- * @param KeyValue[] $collection
+ * @param array[] $collection
*/
public function setRawCollection(array $collection): void;
/**
- * @param KeyValue $keyValue
- * @return KeyValue
+ * @param array $array
+ * @return array
*/
- public function compileKeyValue(KeyValue $keyValue): KeyValue;
+ public function compile(array $array): array;
} \ No newline at end of file
diff --git a/src/Dotenv.php b/src/Dotenv.php
index 3f6eb90..95dda02 100644
--- a/src/Dotenv.php
+++ b/src/Dotenv.php
@@ -15,7 +15,6 @@ use NeonXP\Dotenv\Loader\FileLoader;
use NeonXP\Dotenv\Loader\LoaderInterface;
use NeonXP\Dotenv\Parser\Parser;
use NeonXP\Dotenv\Parser\ParserInterface;
-use NeonXP\Dotenv\Types\KeyValue;
/**
* Class Dotenv
@@ -75,9 +74,9 @@ class Dotenv implements \ArrayAccess, \IteratorAggregate
$rawData = array_map([$this->parser, 'parseLine'], $lines);
$this->compiler->setRawCollection($rawData);
$this->loadedValues = array_reduce(
- array_map([$this->compiler, 'compileKeyValue'], $rawData),
- function (array $acc, KeyValue $current) {
- $acc[$current->getKey()] = $current->getValue();
+ array_map([$this->compiler, 'compile'], $rawData),
+ function (array $acc, $current) {
+ $acc[$current['key']] = $current['value'];
return $acc;
},
[]
diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php
index 7a5c37b..36410fc 100644
--- a/src/Parser/Parser.php
+++ b/src/Parser/Parser.php
@@ -8,8 +8,6 @@ declare(strict_types=1);
namespace NeonXP\Dotenv\Parser;
-use NeonXP\Dotenv\Types\KeyValue;
-
/**
* Class Parser
* @package NeonXP\Dotenv\Parser
@@ -28,7 +26,7 @@ class Parser implements ParserInterface
const BOOLEAN = '/^(true|false)$/i';
const NUMBER = '/^(\d+)$/';
- public function parseLine(string $line): KeyValue
+ public function parseLine(string $line): array
{
$line = preg_replace(self::REGEX_EXPORT_PREFIX, '', $line);
list($key, $value) = explode('=', $line, 2) + ['', ''];
@@ -50,6 +48,6 @@ class Parser implements ParserInterface
$value = intval($value);
}
- return new KeyValue($key, $value);
+ return ['key' => $key, 'value' => $value];
}
} \ No newline at end of file
diff --git a/src/Parser/ParserInterface.php b/src/Parser/ParserInterface.php
index f67ffb5..4e63b33 100644
--- a/src/Parser/ParserInterface.php
+++ b/src/Parser/ParserInterface.php
@@ -8,8 +8,6 @@ declare(strict_types=1);
namespace NeonXP\Dotenv\Parser;
-use NeonXP\Dotenv\Types\KeyValue;
-
/**
* Interface ParserInterface
* @package NeonXP\Dotenv\Parser
@@ -18,7 +16,7 @@ interface ParserInterface
{
/**
* @param string $line
- * @return KeyValue
+ * @return array
*/
- public function parseLine(string $line): KeyValue;
+ public function parseLine(string $line): array;
} \ No newline at end of file
diff --git a/src/Types/KeyValue.php b/src/Types/KeyValue.php
deleted file mode 100644
index 54b0360..0000000
--- a/src/Types/KeyValue.php
+++ /dev/null
@@ -1,53 +0,0 @@
-<?php
-declare(strict_types=1);
-
-/**
- * @author: Alexander Kiryukhin <alexander@kiryukhin.su>
- * @license: MIT
- */
-
-namespace NeonXP\Dotenv\Types;
-
-/**
- * Class KeyValue
- * @package NeonXP\Dotenv\Types
- */
-class KeyValue
-{
- /**
- * @var string
- */
- private $key;
-
- /**
- * @var string
- */
- private $value;
-
- /**
- * KeyValue constructor.
- * @param string $key
- * @param mixed $value
- */
- public function __construct(string $key, $value)
- {
- $this->key = $key;
- $this->value = $value;
- }
-
- /**
- * @return string
- */
- public function getKey(): string
- {
- return $this->key;
- }
-
- /**
- * @return mixed
- */
- public function getValue()
- {
- return $this->value;
- }
-} \ No newline at end of file