Skip to content

Commit

Permalink
Merge pull request #4 from stupidlysimple/nightly
Browse files Browse the repository at this point in the history
Nightly
  • Loading branch information
Fariz Luqman committed Mar 25, 2017
2 parents 83669b8 + d5ab2aa commit 8ef52c7
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/Viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

/**
Expand Down Expand Up @@ -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);

Expand All @@ -105,15 +109,40 @@ static private function render($file, $data){
$input = ob_get_contents();
ob_end_clean();

$output = preg_replace_callback('!\{\{(\w+)\}\}!', 'Viewer::replace', $input);
$output = preg_replace_callback('!\{\{(.*?)\}\}!', 'Viewer::replace', $input);


echo($output);
}

static private function replace($matches) {
if(isset(self::$hive[$matches[1]])){
return self::$hive[$matches[1]];
// If '.' is found in the $matches[1], assume it is an object
// which have a property

// 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, 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]];
}
}

}

}
}

0 comments on commit 8ef52c7

Please sign in to comment.