/** * Get test * * @param Apishka_Templater_Parser $parser * @param mixed $line * * @return mixed */ private function getTest(Apishka_Templater_Parser $parser, $line) { $stream = $parser->getStream(); $name = $stream->expect(Apishka_Templater_Token::NAME_TYPE)->getValue(); $env = $parser->getEnvironment(); if ($test = $env->getTest($name)) { return $test; } if ($stream->test(Apishka_Templater_Token::NAME_TYPE)) { // try 2-words tests $name = $name . ' ' . $parser->getCurrentToken()->getValue(); if ($test = $env->getTest($name)) { $parser->getStream()->next(); return $test; } } $e = new Apishka_Templater_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $parser->getFilename()); $e->addSuggestions($name, array_keys($env->getTests())); throw $e; }
private function getFilterNodeClass($name, $line) { $env = $this->parser->getEnvironment(); if (false === ($filter = $env->getFilter($name))) { $e = new Apishka_Templater_Error_Syntax(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getFilename()); $e->addSuggestions($name, array_keys($env->getFilters())); throw $e; } if ($filter->isDeprecated()) { $message = sprintf('Twig Filter "%s" is deprecated', $filter->getName()); if (!is_bool($filter->getDeprecatedVersion())) { $message .= sprintf(' since version %s', $filter->getDeprecatedVersion()); } if ($filter->getAlternative()) { $message .= sprintf('. Use "%s" instead', $filter->getAlternative()); } $message .= sprintf(' in %s at line %d.', $this->parser->getFilename(), $line); @trigger_error($message, E_USER_DEPRECATED); } return $filter->getNodeClass(); }
public function subparse($test, $dropNeedle = false) { $lineno = $this->getCurrentToken()->getLine(); $rv = array(); while (!$this->stream->isEOF()) { switch ($this->getCurrentToken()->getType()) { case Apishka_Templater_Token::TEXT_TYPE: $token = $this->stream->next(); $rv[] = Apishka_Templater_Node_Text::apishka($token->getValue(), $token->getLine()); break; case Apishka_Templater_Token::VAR_START_TYPE: $token = $this->stream->next(); $expr = $this->expressionParser->parseExpression(); $this->stream->expect(Apishka_Templater_Token::VAR_END_TYPE); $rv[] = Apishka_Templater_Node_Print::apishka($expr, $token->getLine()); break; case Apishka_Templater_Token::BLOCK_START_TYPE: $this->stream->next(); $token = $this->getCurrentToken(); if ($token->getType() !== Apishka_Templater_Token::NAME_TYPE) { throw new Apishka_Templater_Error_Syntax('A block must start with a tag name.', $token->getLine(), $this->getFilename()); } if (null !== $test && $test($token)) { if ($dropNeedle) { $this->stream->next(); } if (1 === count($rv)) { return $rv[0]; } return Apishka_Templater_Node::apishka($rv, array(), $lineno); } if (!isset($this->handlers[$token->getValue()])) { if (null !== $test) { $e = new Apishka_Templater_Error_Syntax(sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->getFilename()); if (is_array($test) && isset($test[0]) && $test[0] instanceof Apishka_Templater_TokenParserInterface) { $e->appendMessage(sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]->getTag(), $lineno)); } } else { $e = new Apishka_Templater_Error_Syntax(sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->getFilename()); $e->addSuggestions($token->getValue(), array_keys($this->env->getTags())); } throw $e; } $this->stream->next(); $subparser = $this->handlers[$token->getValue()]; $node = $subparser->parse($token); if (null !== $node) { $rv[] = $node; } break; default: throw new Apishka_Templater_Error_Syntax('Lexer or parser ended up in unsupported state.', 0, $this->getFilename()); } } if (1 === count($rv)) { return $rv[0]; } return Apishka_Templater_Node::apishka($rv, array(), $lineno); }