From d5ab2aae2557dda3709cb31f05947e45b693d2ec Mon Sep 17 00:00:00 2001 From: Fariz Luqman Date: Sat, 25 Mar 2017 23:38:51 +0800 Subject: [PATCH] Viewer Commenting --- src/Viewer.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Viewer.php b/src/Viewer.php index a1b618f..bcb4582 100755 --- a/src/Viewer.php +++ b/src/Viewer.php @@ -41,6 +41,8 @@ */ class Viewer { + // the hive is where all data is stored, which is then usable from all template + // files private static $hive = []; /** @@ -97,6 +99,8 @@ static private function render($file, $data){ if(Sharer::get() !== null){ extract(Sharer::get()); } + + // Merge data into the hive self::$hive = array_merge(self::$hive, get_defined_vars()); unset($data); @@ -112,21 +116,27 @@ static private function render($file, $data){ } static private function replace($matches) { - // !TODO if '.' is found in the string, assume it is an object - // and return the property of the object + // If '.' is found in the $matches[1], assume it is an object + // which have a property - // else, return the value of the variable + // else, assume it is a variable if (strpos($matches[1], '.') !== false) { + // explode the part before and after '.' + // the part before '.' is an object, while the part after '.' is a property list($object, $property) = explode('.', $matches[1]); - // if a '()' is found in $property, change it to callable + // if a '()' is found in $property, we will then assume it to be a callable + // method. if (strpos($property, '()') !== false) { + // remove paranthesis list($function, $parenthesis) = explode('()', $property); + + // return the callable method of the object from the hive return(self::$hive[$object]->$function()); }else{ + // return the property of the object from the hive return(self::$hive[$object]->$property); } - }else{ if(isset(self::$hive[$matches[1]])){ return self::$hive[$matches[1]];