本文整理汇总了PHP中Twig_Environment::tokenize方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::tokenize方法的具体用法?PHP Twig_Environment::tokenize怎么用?PHP Twig_Environment::tokenize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::tokenize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testEmptyConstant
public function testEmptyConstant()
{
$stream = $this->environment->parse($this->environment->tokenize('{{ constant("ExtensionTest::SOME_EMPTY_VALUE") }}', 'index'));
$node = $stream->getNode('body')->getNode(0);
$this->assertEquals(Twig_Node_Print::class, get_class($node));
$this->assertEquals(ExtensionTest::SOME_EMPTY_VALUE, $node->getNode('expr')->getAttribute('value'));
}
示例2: validate
/**
* @param EmailTemplate $value
* @param Constraint|EmailTemplateSyntax $constraint
*/
public function validate($value, Constraint $constraint)
{
// prepare templates to be validated
$itemsToValidate = [['field' => 'subject', 'locale' => null, 'template' => $value->getSubject()], ['field' => 'content', 'locale' => null, 'template' => $value->getContent()]];
$translations = $value->getTranslations();
foreach ($translations as $trans) {
if (in_array($trans->getField(), ['subject', 'content'])) {
$itemsToValidate[] = ['field' => $trans->getField(), 'locale' => $trans->getLocale(), 'template' => $trans->getContent()];
}
}
/** @var \Twig_Extension_Sandbox $sandbox */
$sandbox = $this->twig->getExtension('sandbox');
$sandbox->enableSandbox();
// validate templates' syntax
$errors = [];
foreach ($itemsToValidate as &$item) {
try {
$this->twig->parse($this->twig->tokenize($item['template']));
} catch (\Twig_Error_Syntax $e) {
$errors[] = ['field' => $item['field'], 'locale' => $item['locale'], 'error' => $e->getMessage()];
}
}
$sandbox->disableSandbox();
// add violations for found errors
if (!empty($errors)) {
foreach ($errors as $error) {
$this->context->addViolation($constraint->message, ['{{ field }}' => $this->getFieldLabel(ClassUtils::getClass($value), $error['field']), '{{ locale }}' => $this->getLocaleName($error['locale']), '{{ error }}' => $error['error']]);
}
}
}
示例3: visitFile
function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
{
if ($file->getExtension() == 'twital' && ($adapter = $this->twitalLoader->getSourceAdapter((string) $file))) {
$source = $this->twitalLoader->getTwital()->compile($adapter, file_get_contents((string) $file));
$ast = $this->twig->parse($this->twig->tokenize($source, (string) $file));
$this->visitTwigFile($file, $catalogue, $ast);
}
}
示例4: getPlaces
/**
* {@inheritDoc}
*/
public function getPlaces($layoutSrc)
{
$tokenStream = $this->twig->tokenize($this->twig->getLoader()->getSource($layoutSrc));
$collector = new PlaceHolderNodeCollector();
$traverser = new \Twig_NodeTraverser($this->twig, array($collector));
$traverser->traverse($this->twig->parse($tokenStream));
return $collector->getCollectedNames();
}
示例5: extractTemplate
protected function extractTemplate($template, MessageCatalogue $catalogue)
{
$visitor = $this->twig->getExtension('translator')->getTranslationNodeVisitor();
$visitor->enable();
$this->twig->parse($this->twig->tokenize($template));
foreach ($visitor->getMessages() as $message) {
$catalogue->set(trim($message[0]), $this->prefix . trim($message[0]), $message[1] ? $message[1] : $this->defaultDomain);
}
$visitor->disable();
}
示例6: extract
/**
* {@inheritDoc}
*/
public function extract($directory, MessageCatalogue $catalogue)
{
// load any existing translation files
$finder = new Finder();
$files = $finder->files()->name('*.twig')->in($directory);
foreach ($files as $file) {
$tree = $this->twig->parse($this->twig->tokenize(file_get_contents($file->getPathname())));
$this->crawlNode($tree, $catalogue);
}
}
示例7: extractTemplate
protected function extractTemplate($template, MessageCatalogue $catalogue)
{
$visitor = $this->twig->getExtension('Symfony\\Bridge\\Twig\\Extension\\TranslationExtension')->getTranslationNodeVisitor();
$visitor->enable();
$this->twig->parse($this->twig->tokenize(new \Twig_Source($template, '')));
foreach ($visitor->getMessages() as $message) {
$catalogue->set(trim($message[0]), $this->prefix . trim($message[0]), $message[1] ?: $this->defaultDomain);
}
$visitor->disable();
}
示例8: testStringExpression
/**
* @dataProvider getTestsForString
*/
public function testStringExpression($template, $expected)
{
$env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$stream = $env->tokenize($template, 'index');
$parser = new Twig_Parser($env);
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
}
示例9: testArraySyntaxError
/**
* @expectedException Twig_Error_Syntax
* @dataProvider getFailingTestsForArray
*/
public function testArraySyntaxError($template)
{
$env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
$parser = new Twig_Parser($env);
$parser->parse($env->tokenize($template, 'index'));
}
示例10: testEscaping
/**
* @dataProvider getEscapingTemplates
*/
public function testEscaping($template, $mustBeEscaped)
{
$twig = new \Twig_Environment(null, array('debug' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0));
$twig->addExtension(new RoutingExtension($this->getMock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface')));
$nodes = $twig->parse($twig->tokenize($template));
$this->assertSame($mustBeEscaped, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof \Twig_Node_Expression_Filter);
}
示例11: testLegacyTokenizeSignature
/**
* @group legacy
*/
public function testLegacyTokenizeSignature()
{
$env = new Twig_Environment();
$stream = $env->tokenize('{{ foo }}', 'foo');
$this->assertEquals('{{ foo }}', $stream->getSource());
$this->assertEquals('foo', $stream->getFilename());
}
示例12: testEscaping
/**
* Tests the escaping
*
* @dataProvider providerTestEscaping
*/
public function testEscaping($template, $expected)
{
$twig = new \Twig_Environment(NULL, array('debug' => TRUE, 'cache' => FALSE, 'autoescape' => TRUE, 'optimizations' => 0));
$twig->addExtension((new TwigExtension())->setGenerators($this->getMock('Drupal\\Core\\Routing\\UrlGeneratorInterface')));
$nodes = $twig->parse($twig->tokenize($template));
$this->assertSame($expected, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof \Twig_Node_Expression_Filter);
}
示例13: testCompile
/**
* @dataProvider fixtureProvider
*/
public function testCompile($source, $expected)
{
$twig = new \Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), ['cache' => false, 'autoescape' => false, 'optimizations' => 0]);
$twig->addExtension(new ElixirExtension('public', 'build'));
$nodes = $twig->parse($twig->tokenize($source));
$this->assertEquals($expected, $nodes->getNode('body')->getNode(0));
}
示例14: testIntegration
/**
* @dataProvider getTests
*/
public function testIntegration($file, $test, $message, $templates)
{
$loader = new Twig_Loader_Array($templates);
$twig = new Twig_Environment($loader, array('trim_blocks' => true, 'cache' => false));
$twig->addExtension(new Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
try {
$template = $twig->loadTemplate('index.twig');
} catch (Twig_SyntaxError $e) {
$e->setFilename($file);
throw $e;
} catch (Exception $e) {
throw new Twig_Error($e->getMessage() . ' (in ' . $file . ')');
}
preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\\-\\-DATA\\-\\-|$)/s', $test, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$output = trim($template->render(eval($match[1] . ';')), "\n ");
$expected = trim($match[2], "\n ");
if ($expected != $output) {
echo 'Compiled template that failed:';
foreach (array_keys($templates) as $name) {
echo "Template: {$name}\n";
$source = $loader->getSource($name);
echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
}
}
$this->assertEquals($expected, $output, $message . ' (in ' . $file . ')');
}
}
示例15: parseFile
/**
* @param string $path
*
* @return array|Word[]
*/
public function parseFile($path)
{
$words = array();
$twig = new \Twig_Environment();
// TODO should it be configured dynamically?
$lines = file($path);
if (count($lines) <= 0) {
// TODO handle and log error
return array();
}
$tokenStream = $twig->tokenize(implode('', $lines));
while (!$tokenStream->isEOF()) {
$token = $tokenStream->next();
if ($token->getType() === \Twig_Token::TEXT_TYPE) {
$lineNumber = $token->getLine();
//split line by line
foreach (preg_split("/((\r?\n)|(\r\n?))/", $token->getValue()) as $line) {
$parts = array();
preg_match_all('/(([[:alpha:]]+[\'-])*[[:alpha:]]+\'?)/u', strip_tags($line), $parts, PREG_SET_ORDER);
foreach ($parts as $part) {
$words[] = new Word(trim($part[0]), $path, $lines[$lineNumber - 1], $lineNumber);
}
++$lineNumber;
}
}
}
return $words;
}