From f603c80180a7145e027e34442e383e7c44ea5004 Mon Sep 17 00:00:00 2001 From: ArthurHlt Date: Sat, 14 Mar 2015 14:27:31 +0100 Subject: [PATCH] Add possibility to browse injected var with %myvar.path% (not only for browse current yaml file) --- src/Arhframe/Yamlarh/Yamlarh.php | 56 +++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/src/Arhframe/Yamlarh/Yamlarh.php b/src/Arhframe/Yamlarh/Yamlarh.php index fcd169c..63da97d 100755 --- a/src/Arhframe/Yamlarh/Yamlarh.php +++ b/src/Arhframe/Yamlarh/Yamlarh.php @@ -116,28 +116,60 @@ public function insertVar($value, &$arrayToReturn, $completeArray) foreach ($varArray as $var) { $finalVar = $finalVar[$var]; } + if (empty($finalVar)) { + $finalVar = $this->getVar($varArray, $arrayToReturn); + } $startValue = preg_replace('#%' . preg_quote($value) . '%#', $finalVar, $startValue); continue; } - $var = $arrayToReturn[$value]; - global $$value; - $varFromFile = $$value; - if (!empty($varFromFile)) { - $var = $varFromFile; - } - if (defined($value)) { - $var = constant($value); - } - if (!empty($this->accessibleVariable[$value])) { - $var = $this->accessibleVariable[$value]; - } + $var = $this->getVar($value, $arrayToReturn); $startValue = preg_replace('#%' . preg_quote($value) . '%#', $var, $startValue); } return $startValue; } + private function getVar($value, &$arrayToReturn) + { + $allValues = null; + if (is_array($value)) { + $allValues = $value; + $value = $value[0]; + } + $var = $arrayToReturn[$value]; + global $$value; + $varFromFile = $$value; + if (!empty($varFromFile)) { + $var = $varFromFile; + } + if (defined($value)) { + $var = constant($value); + } + if (!empty($this->accessibleVariable[$value])) { + $var = $this->accessibleVariable[$value]; + } + + $var = $this->getComplexeVar($var, $allValues); + return $var; + } + + private function getComplexeVar($var, $values) + { + if (empty($values)) { + return $var; + } + $values = array_slice($values, 1); + if (is_object($var) && !empty($values)) { + $get = "get" . ucfirst($values[0]); + return $this->getComplexeVar($var->$get(), $values); + } + if (is_array($var) && !empty($values)) { + return $this->getComplexeVar($var[$values[0]], $values); + } + return $var; + } + private function parseFile($file) { $parsedYml = Yaml::parse($file->getContent());