Remove KeyValue object

This commit is contained in:
Александр Кирюхин 2018-01-19 19:03:26 +03:00
parent 47c9d580c6
commit 8ea647570b
12 changed files with 53 additions and 118 deletions

View file

@ -10,7 +10,7 @@
} }
], ],
"require": { "require": {
"php": ">=7.0" "php": ">=7.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View file

@ -9,7 +9,6 @@ declare(strict_types=1);
namespace NeonXP\Dotenv\Compiler; namespace NeonXP\Dotenv\Compiler;
use NeonXP\Dotenv\Exception\RuntimeException; use NeonXP\Dotenv\Exception\RuntimeException;
use NeonXP\Dotenv\Types\KeyValue;
/** /**
* Class Compiler * Class Compiler
@ -20,61 +19,64 @@ class Compiler implements CompilerInterface
const REGEX_VARIABLE = '/\$\{(.+?)\}/'; const REGEX_VARIABLE = '/\$\{(.+?)\}/';
/** /**
* @var KeyValue[] * @var array[]
*/ */
protected $collection = []; protected $collection = [];
/** /**
* @var KeyValue[] * @var array[]
*/ */
protected $cache = []; protected $cache = [];
/** /**
* @inheritdoc * @inheritdoc
* @param KeyValue[] $collection * @param array[] $collection
*/ */
public function setRawCollection(array $collection): void public function setRawCollection(array $collection): void
{ {
$this->collection = []; $this->collection = [];
$this->cache = []; $this->cache = [];
foreach ($collection as $keyValue) { foreach ($collection as $array) {
$this->collection[$keyValue->getKey()] = $keyValue; $this->collection[$array['key']] = $array;
} }
} }
/** /**
* @inheritdoc * @inheritdoc
* @param KeyValue $keyValue * @param array $array
* @return KeyValue * @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]; $variable = $variable[1];
if ($variable === $keyValue->getKey()) { if ($variable === $array['key']) {
throw new RuntimeException('Self referencing'); throw new RuntimeException('Self referencing');
} }
if (isset($this->cache[$variable])) { 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])) { } 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])) { } 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}"; return "UNKNOWN VARIABLE {$variable}";
}, $keyValue->getValue()); }, $array['value']);
$result = new KeyValue($keyValue->getKey(), $newValue); $result = [
$this->cache[$result->getKey()] = $result; 'key' => $array['key'],
'value' => $newValue
];
$this->cache[$result['key']] = $result;
return $result; return $result;
} }
/** /**
* @param KeyValue $keyValue * @param array $array
* @return bool * @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']);
} }
} }

View file

@ -8,8 +8,6 @@ declare(strict_types=1);
namespace NeonXP\Dotenv\Compiler; namespace NeonXP\Dotenv\Compiler;
use NeonXP\Dotenv\Types\KeyValue;
/** /**
* Interface CompilerInterface * Interface CompilerInterface
* @package NeonXP\Dotenv\Compiler * @package NeonXP\Dotenv\Compiler
@ -17,13 +15,13 @@ use NeonXP\Dotenv\Types\KeyValue;
interface CompilerInterface interface CompilerInterface
{ {
/** /**
* @param KeyValue[] $collection * @param array[] $collection
*/ */
public function setRawCollection(array $collection): void; public function setRawCollection(array $collection): void;
/** /**
* @param KeyValue $keyValue * @param array $array
* @return KeyValue * @return array
*/ */
public function compileKeyValue(KeyValue $keyValue): KeyValue; public function compile(array $array): array;
} }

View file

@ -15,7 +15,6 @@ use NeonXP\Dotenv\Loader\FileLoader;
use NeonXP\Dotenv\Loader\LoaderInterface; use NeonXP\Dotenv\Loader\LoaderInterface;
use NeonXP\Dotenv\Parser\Parser; use NeonXP\Dotenv\Parser\Parser;
use NeonXP\Dotenv\Parser\ParserInterface; use NeonXP\Dotenv\Parser\ParserInterface;
use NeonXP\Dotenv\Types\KeyValue;
/** /**
* Class Dotenv * Class Dotenv
@ -75,9 +74,9 @@ class Dotenv implements \ArrayAccess, \IteratorAggregate
$rawData = array_map([$this->parser, 'parseLine'], $lines); $rawData = array_map([$this->parser, 'parseLine'], $lines);
$this->compiler->setRawCollection($rawData); $this->compiler->setRawCollection($rawData);
$this->loadedValues = array_reduce( $this->loadedValues = array_reduce(
array_map([$this->compiler, 'compileKeyValue'], $rawData), array_map([$this->compiler, 'compile'], $rawData),
function (array $acc, KeyValue $current) { function (array $acc, $current) {
$acc[$current->getKey()] = $current->getValue(); $acc[$current['key']] = $current['value'];
return $acc; return $acc;
}, },
[] []

View file

@ -8,8 +8,6 @@ declare(strict_types=1);
namespace NeonXP\Dotenv\Parser; namespace NeonXP\Dotenv\Parser;
use NeonXP\Dotenv\Types\KeyValue;
/** /**
* Class Parser * Class Parser
* @package NeonXP\Dotenv\Parser * @package NeonXP\Dotenv\Parser
@ -28,7 +26,7 @@ class Parser implements ParserInterface
const BOOLEAN = '/^(true|false)$/i'; const BOOLEAN = '/^(true|false)$/i';
const NUMBER = '/^(\d+)$/'; const NUMBER = '/^(\d+)$/';
public function parseLine(string $line): KeyValue public function parseLine(string $line): array
{ {
$line = preg_replace(self::REGEX_EXPORT_PREFIX, '', $line); $line = preg_replace(self::REGEX_EXPORT_PREFIX, '', $line);
list($key, $value) = explode('=', $line, 2) + ['', '']; list($key, $value) = explode('=', $line, 2) + ['', ''];
@ -50,6 +48,6 @@ class Parser implements ParserInterface
$value = intval($value); $value = intval($value);
} }
return new KeyValue($key, $value); return ['key' => $key, 'value' => $value];
} }
} }

View file

@ -8,8 +8,6 @@ declare(strict_types=1);
namespace NeonXP\Dotenv\Parser; namespace NeonXP\Dotenv\Parser;
use NeonXP\Dotenv\Types\KeyValue;
/** /**
* Interface ParserInterface * Interface ParserInterface
* @package NeonXP\Dotenv\Parser * @package NeonXP\Dotenv\Parser
@ -18,7 +16,7 @@ interface ParserInterface
{ {
/** /**
* @param string $line * @param string $line
* @return KeyValue * @return array
*/ */
public function parseLine(string $line): KeyValue; public function parseLine(string $line): array;
} }

View file

@ -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;
}
}

View file

@ -6,7 +6,6 @@ declare(strict_types=1);
* @license: MIT * @license: MIT
*/ */
use NeonXP\Dotenv\Types\KeyValue;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** /**
@ -29,16 +28,16 @@ class CompilerTest extends TestCase
'KEY4' => 'Test VALUE1 VALUE3 => VALUE3', 'KEY4' => 'Test VALUE1 VALUE3 => VALUE3',
]; ];
$compiler = new \NeonXP\Dotenv\Compiler\Compiler(); $compiler = new \NeonXP\Dotenv\Compiler\Compiler();
$collectionOfKeyValues = []; $collectionOfarrays = [];
foreach ($collection as $key => $value) { foreach ($collection as $key => $value) {
$collectionOfKeyValues[] = new KeyValue($key, $value); $collectionOfarrays[] = ['key' => $key, 'value' => $value];
} }
$compiler->setRawCollection($collectionOfKeyValues); $compiler->setRawCollection($collectionOfarrays);
foreach ($tests as $key => $expected) { foreach ($tests as $key => $expected) {
$result = $compiler->compileKeyValue(new KeyValue($key, $collection[$key])); $result = $compiler->compile(['key' => $key, 'value' => $collection[$key]]);
$this->assertEquals($key, $result->getKey()); $this->assertEquals($key, $result['key']);
$this->assertEquals($expected, $result->getValue()); $this->assertEquals($expected, $result['value']);
} }
} }
} }

View file

@ -11,7 +11,6 @@ use NeonXP\Dotenv\Dotenv;
use NeonXP\Dotenv\Exception\RuntimeException; use NeonXP\Dotenv\Exception\RuntimeException;
use NeonXP\Dotenv\Loader\LoaderInterface; use NeonXP\Dotenv\Loader\LoaderInterface;
use NeonXP\Dotenv\Parser\ParserInterface; use NeonXP\Dotenv\Parser\ParserInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** /**
@ -45,12 +44,8 @@ class DotenvTest extends TestCase
{ {
$dotenv = new Dotenv($this->mockLoader, $this->mockParser, $this->mockCompiler); $dotenv = new Dotenv($this->mockLoader, $this->mockParser, $this->mockCompiler);
try { $this->expectException(RuntimeException::class);
$dotenv->get('TEST1'); $dotenv->get('TEST1');
$this->assertTrue(false, 'Dotenv must throws exception if it not loaded');
} catch (RuntimeException $exception) {
$this->assertTrue(true, 'Dotenv must throws exception if it not loaded');
}
$dotenv->load(); $dotenv->load();

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
* @author: Alexander Kiryukhin <alexander@kiryukhin.su> * @author: Alexander Kiryukhin <alexander@kiryukhin.su>
* @license: MIT * @license: MIT
*/ */
use NeonXP\Dotenv\Parser\Parser; use NeonXP\Dotenv\Parser\Parser;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -30,8 +31,8 @@ class ParserTest extends TestCase
foreach ($tests as $test => $expected) { foreach ($tests as $test => $expected) {
$result = $parser->parseLine($test); $result = $parser->parseLine($test);
$this->assertEquals($expected[0], $result->getKey()); $this->assertEquals($expected[0], $result['key']);
$this->assertEquals($expected[1], $result->getValue()); $this->assertEquals($expected[1], $result['value']);
} }
} }
} }

View file

@ -7,7 +7,6 @@ declare(strict_types=1);
*/ */
use NeonXP\Dotenv\Compiler\CompilerInterface; use NeonXP\Dotenv\Compiler\CompilerInterface;
use NeonXP\Dotenv\Types\KeyValue;
/** /**
* Class MockCompiler * Class MockCompiler
@ -16,7 +15,7 @@ class MockCompiler implements CompilerInterface
{ {
/** /**
* @param KeyValue[] $collection * @param array[] $collection
*/ */
function setRawCollection(array $collection): void function setRawCollection(array $collection): void
{ {
@ -24,11 +23,11 @@ class MockCompiler implements CompilerInterface
} }
/** /**
* @param KeyValue $keyValue * @param array $array
* @return KeyValue * @return array
*/ */
function compileKeyValue(KeyValue $keyValue): KeyValue function compile(array $array): array
{ {
return $keyValue; return $array;
} }
} }

View file

@ -7,7 +7,6 @@ declare(strict_types=1);
*/ */
use NeonXP\Dotenv\Parser\ParserInterface; use NeonXP\Dotenv\Parser\ParserInterface;
use NeonXP\Dotenv\Types\KeyValue;
/** /**
* Class MockParser * Class MockParser
@ -17,12 +16,12 @@ class MockParser implements ParserInterface
/** /**
* @param string $line * @param string $line
* @return KeyValue * @return array
*/ */
public function parseLine(string $line): KeyValue public function parseLine(string $line): array
{ {
list($key, $value) = explode("=", $line); list($key, $value) = explode("=", $line);
return new KeyValue($key, $value); return ['key' => $key, 'value' => $value];
} }
} }