diff options
Diffstat (limited to 'src/Compiler')
-rw-r--r-- | src/Compiler/Compiler.php | 80 | ||||
-rw-r--r-- | src/Compiler/CompilerInterface.php | 10 |
2 files changed, 88 insertions, 2 deletions
diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php new file mode 100644 index 0000000..6a587d2 --- /dev/null +++ b/src/Compiler/Compiler.php @@ -0,0 +1,80 @@ +<?php +declare(strict_types=1); + +/** + * @author: Alexander Kiryukhin <alexander@kiryukhin.su> + * @license: MIT + */ + +namespace NeonXP\Dotenv\Compiler; + +use NeonXP\Dotenv\Exception\RuntimeException; +use NeonXP\Dotenv\Types\KeyValue; + +/** + * Class Compiler + * @package NeonXP\Dotenv\Compiler + */ +class Compiler implements CompilerInterface +{ + const REGEX_VARIABLE = '/\$\{(.+?)\}/'; + + /** + * @var KeyValue[] + */ + protected $collection = []; + + /** + * @var KeyValue[] + */ + protected $cache = []; + + /** + * @inheritdoc + * @param KeyValue[] $collection + */ + public function setRawCollection(array $collection): void + { + $this->collection = []; + $this->cache = []; + foreach ($collection as $keyValue) { + $this->collection[$keyValue->getKey()] = $keyValue; + } + } + + /** + * @inheritdoc + * @param KeyValue $keyValue + * @return KeyValue + */ + public function compileKeyValue(KeyValue $keyValue): KeyValue + { + $newValue = preg_replace_callback(self::REGEX_VARIABLE, function ($variable) use ($keyValue) { + $variable = $variable[1]; + if ($variable === $keyValue->getKey()) { + throw new RuntimeException('Self referencing'); + } + if (isset($this->cache[$variable])) { + return $this->cache[$variable]->getValue(); + } elseif (isset($this->collection[$variable]) && !$this->needToCompile($this->collection[$variable])) { + return $this->collection[$variable]->getValue(); + } elseif (isset($this->collection[$variable]) && $this->needToCompile($this->collection[$variable])) { + return $this->compileKeyValue($this->collection[$variable])->getValue(); + } + return "UNKNOWN VARIABLE {$variable}"; + }, $keyValue->getValue()); + $result = new KeyValue($keyValue->getKey(), $newValue); + $this->cache[$result->getKey()] = $result; + + return $result; + } + + /** + * @param KeyValue $keyValue + * @return bool + */ + protected function needToCompile(KeyValue $keyValue): bool + { + return !!preg_match(self::REGEX_VARIABLE, $keyValue->getValue()); + } +}
\ No newline at end of file diff --git a/src/Compiler/CompilerInterface.php b/src/Compiler/CompilerInterface.php index 042a7d0..15b6823 100644 --- a/src/Compiler/CompilerInterface.php +++ b/src/Compiler/CompilerInterface.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + /** * @author: Alexander Kiryukhin <alexander@kiryukhin.su> * @license: MIT @@ -8,16 +10,20 @@ namespace NeonXP\Dotenv\Compiler; use NeonXP\Dotenv\Types\KeyValue; +/** + * Interface CompilerInterface + * @package NeonXP\Dotenv\Compiler + */ interface CompilerInterface { /** * @param KeyValue[] $collection */ - function setRawCollection(array $collection): void; + public function setRawCollection(array $collection): void; /** * @param KeyValue $keyValue * @return KeyValue */ - function compileKeyValue(KeyValue $keyValue): KeyValue; + public function compileKeyValue(KeyValue $keyValue): KeyValue; }
\ No newline at end of file |