/** * @covers Phramework\Testphase\Expression::getExpression */ public function testGetExpression() { $types = [Expression::EXPRESSION_TYPE_PLAIN, Expression::EXPRESSION_TYPE_REPLACE, Expression::EXPRESSION_TYPE_INLINE_REPLACE]; foreach ($types as $type) { $expression = Expression::getExpression($type); $this->assertInternalType('string', $expression); //Check if expression is correct (no errors or exceptions fired) preg_match($expression, 'my-key'); } }
/** * Parse the template. The process here is basically to create sub-templates * for some of the language constructs, and tie them in with placeholders in * their parent template. Later, when we call render(), we can put the * output of sub-templates back into their parents. */ private function parse() { if ($this->cache) { $this->cacheFile = $this->CACHE_DIR . '/' . $this->name . $this->checksum(); // Make sure we have a cache directory to write to if (!is_dir($this->CACHE_DIR)) { mkdir($this->CACHE_DIR); } // If our cache file already exists, load it up if (file_exists($this->cacheFile)) { $this->cacheTemplate = unserialize(file_get_contents($this->cacheFile)); return; } } $count = count($this->strings); for ($i = 0; $i < $count; $i++) { $matches = null; // Remove all comments $n = preg_match(self::RE_COM_BEG, $this->strings[$i], $matches); if ($n > 0) { $this->parseRecursive($i, array(self::RE_COM_BEG, self::RE_COM_END), 'comment'); $this->strings[$i] = ''; } // Find all the variables. If the variable doesn't exist in our // dictionary, stub it out. preg_match_all(self::RE_VAR, $this->strings[$i], $matches); if (!empty($matches[0])) { foreach ($matches[1] as $match) { if (!isset($this->vars[$match])) { $this->vars[$match] = ''; } } } // Find all foreach-loops. Every time we find one, make a new // template with the contents of that loop, and put a placeholder // where it used to be. $n = preg_match(self::RE_FOR_BEG, $this->strings[$i], $matches); if ($n > 0) { $forName = $matches[1]; if (!isset($this->forTemplates[$forName])) { $this->forTemplates[$forName] = array(); } $n = count($this->forTemplates[$forName]); $this->forTemplates[$forName][$n] = $this->parseRecursive($i, array(self::RE_FOR_BEG, self::RE_FOR_END), "{$this->name}_for_{$forName}_{$n}"); $this->strings[$i] = '{for:' . $forName . ':' . $n . '}'; } // Do the same general process for our if statement. $n = preg_match(self::RE_IF_BEG, $this->strings[$i], $matches); if ($n > 0) { $ifExpr = new expression($matches[1]); $exprId = $ifExpr->getExpressionId(); if (!isset($this->expressions[$exprId])) { $this->expressions[$exprId] = $ifExpr; } if (!isset($this->ifTemplates[$exprId])) { $this->ifTemplates[$exprId] = array(); } $n = count($this->ifTemplates[$exprId]); $this->ifTemplates[$exprId][$n] = $this->parseRecursive($i, array(self::RE_IF_BEG, self::RE_IF_END), "{$this->name}_if_{$exprId}_{$n}"); $this->strings[$i] = '{if:' . $exprId . ':' . $n . '}'; } // Currently, we have the same general process for includes. But.. // there are problems with this approach. In the future, this could // be refactored. // // TODO: Move this block of code to the beginning, and instead of // creating a template, just replace the include statement with // the file contents of the include. preg_match_all(self::RE_INC, $this->strings[$i], $matches); if (!empty($matches[0])) { foreach ($matches[1] as $match) { if (!isset($this->incTemplates[$match])) { $info = pathinfo($match); if ($info['extension'] == 'php') { ob_start(); $this->require_file($match); $output = array(ob_get_clean()); $this->incTemplates[$match] = new Scurvy($output, $this->template_dir, false, $this->name); } else { $this->incTemplates[$match] = new Scurvy($match, $this->template_dir, false, $this->name); } } } } // Find all expressions. We could probably get rid of this feature // because expressions probably won't be used outside of // if-statements. preg_match_all(self::RE_EXPR, $this->strings[$i], $matches); if (!empty($matches[0])) { foreach ($matches[1] as $match) { $expr = new Expression($match); $exprId = $expr->getExpressionId(); if (!isset($this->expressions[$exprId])) { $this->expressions[$exprId] = $expr; } $this->strings[$i] = preg_replace('/\\{' . preg_quote($expr->getExpression(), '/') . '\\}/', '{' . $exprId . '}', $this->strings[$i]); } } } // If we've gotten here, we must need to cache this file if ($this->cache) { file_put_contents($this->cacheFile, serialize($this)); $this->cacheTemplate = $this; } }