- * {for myarray.items}
- * {@item.title}
- * {/for}
- *
- *
- * Or:
- *
- *
- * {for 0:pages.count; i++}
- * {@i}
- * {/for}
- *
- *
- * @param $cond string The condition string for the loop, to be parsed (e.g. "myarray.items" or "0:pages.count; i++")
- * @param $stack string The child stack for this loop, to be processed for each item.
- * @return string
- * @throws \Exception
- */
- protected function doParseFor($cond, $stack)
- {
- // If this is a simple foreach loop, jump over to parse loop:
- if (strpos($cond, ';') === false) {
- return $this->doParseLoop($cond, $stack);
- }
-
- // Otherwise, process as a for loop:
- $parts = explode(';', $cond);
- $range = explode(':', trim($parts[0]));
-
- // Process range:
- $rangeLeft = $this->getForRangePart($range[0]);
- $rangeRight = $this->getForRangePart($range[1]);
-
- // Process variable & incrementor / decrementor:
- $parts[1] = trim($parts[1]);
-
- $matches = [];
- if (preg_match('/([a-zA-Z0-9_]+)(\+\+|\-\-)/', $parts[1], $matches)) {
- $varName = $matches[1];
- $direction = $matches[2] == '++' ? 'increment' : 'decrement';
- } else {
- throw new \Exception('Syntax error in for loop: ' . $cond);
- }
-
- $rtn = '';
-
- if ($direction == 'increment') {
- for ($i = $rangeLeft; $i < $rangeRight; $i++) {
- $this->parent = $this;
- $this->{$varName} = $i;
- $rtn .= $this->processStack($stack);
- }
- } else {
- for ($i = $rangeLeft; $i > $rangeRight; $i--) {
- $this->parent = $this;
- $this->{$varName} = $i;
- $rtn .= $this->processStack($stack);
- }
- }
-
- return $rtn;
- }
-
- protected function getForRangePart($part)
- {
- if (is_numeric($part)) {
- return intval($part);
- }
-
- $varPart = $this->processVariableName($part);
-
- if (is_numeric($varPart)) {
- return intval($varPart);
- }
-
- throw new \Exception('Invalid range in for loop: ' . $part);
- }
-
- public function processVariableName($varName)
- {
- // Case one - Test for function calls:
- if (substr($varName, 0, 1) == '(' && substr($varName, -1) == ')') {
- $functionCall = substr($varName, 1, -1);
- $parts = explode(' ', $functionCall, 2);
- $functionName = $parts[0];
- $arguments = isset($parts[1]) ? $parts[1] : null;
-
- return $this->executeTemplateFunction($functionName, $arguments);
- }
-
- // Case two - Test if it is just a string:
- if (substr($varName, 0, 1) == '"' && substr($varName, -1) == '"') {
- return substr($varName, 1, -1);
- }
-
- // Case three - Test if it is just a number:
- if (is_numeric($varName)) {
- return $varName;
- }
-
- // Case four - Test for helper calls:
- if (strpos($varName, ':') !== false) {
- list($helper, $property) = explode(':', $varName);
-
- $helper = $this->{$helper}();
-
- if (property_exists($helper, $property) || method_exists($helper, '__get')) {
- return $helper->{$property};
- }
-
- return null;
- }
-
- // Case five - Process as a variable:
- $varPart = explode('.', $varName);
- $thisPart = array_shift($varPart);
-
-
- if (!array_key_exists($thisPart, $this->vars)) {
- return null;
- }
-
- $working = $this->{$thisPart};
-
- while (count($varPart)) {
- $thisPart = array_shift($varPart);
-
- if (is_object($working)) {
- // Check if we're working with an actual property:
- if (property_exists($working, $thisPart)) {
- $working = $working->{$thisPart};
- continue;
- }
-
- // Check if the object has a magic __get method:
- if (method_exists($working, '__get')) {
- $working = $working->{$thisPart};
- continue;
- }
- }
-
-
- if (is_array($working) && array_key_exists($thisPart, $working)) {
- $working = $working[$thisPart];
- continue;
- }
-
- if ($thisPart == 'toLowerCase') {
- $working = strtolower($working);
- continue;
- }
-
- if ($thisPart == 'toUpperCase') {
- $working = strtoupper($working);
- continue;
- }
-
- if ($thisPart == 'isNumeric') {
- return is_numeric($working);
- }
-
- return null;
- }
-
- return $working;
- }
-
- protected function doParseFunction($stack)
- {
- return $this->executeTemplateFunction($stack['function_name'], $stack['cond']);
- }
-
- protected function executeTemplateFunction($function, $args)
- {
- if (array_key_exists($function, self::$templateFunctions)) {
- $handler = self::$templateFunctions[$function];
- $args = $this->processFunctionArguments($args);
-
- return $handler($args, $this);
- }
-
- return null;
- }
-
- protected function processFunctionArguments($args)
- {
- $rtn = [];
- $args = explode(';', $args);
-
- foreach ($args as $arg) {
- $arg = explode(':', $arg);
-
- if (count($arg) == 2) {
- $key = trim($arg[0]);
- $val = trim($arg[1]);
-
- if (strpos($val, ',') !== false) {
- $val = explode(',', $val);
- }
-
- $rtn[$key] = $val;
- }
- }
-
- return $rtn;
- }
-
- public function getVariable($variable)
- {
- return $this->processVariableName($variable);
- }
-
- protected function includeTemplate($args, $view)
- {
- $template = static::createFromFile($view->getVariable($args['template']));
-
- if (isset($args['variables'])) {
- if (!is_array($args['variables'])) {
- $args['variables'] = [$args['variables']];
- }
-
- foreach ($args['variables'] as $variable) {
- $variable = explode('=>', $variable);
- $variable = array_map('trim', $variable);
-
- if (count($variable) == 1) {
- $template->{$variable[0]} = $view->getVariable($variable[0]);
- } else {
- $template->{$variable[1]} = $view->getVariable($variable[0]);
- }
- }
- }
-
- return $template->render();
- }
-
- protected function callHelperFunction($args)
- {
- $helper = $args['helper'];
- $function = $args['method'];
-
- return $this->{$helper}()->{$function}();
- }
-}
diff --git a/src/PHPCensor/Application.php b/src/PHPCensor/Application.php
index a4c97605..f60fdbe9 100644
--- a/src/PHPCensor/Application.php
+++ b/src/PHPCensor/Application.php
@@ -6,7 +6,6 @@ use b8;
use b8\Exception\HttpException;
use b8\Http\Response;
use b8\Http\Response\RedirectResponse;
-use b8\View;
/**
* @author Dan Cryer