当前位置: 首页>>代码示例>>PHP>>正文


PHP Twig_Environment::tokenize方法代码示例

本文整理汇总了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'));
 }
开发者ID:silentroach,项目名称:twig-const-resolver,代码行数:7,代码来源:ExtensionEvaluatedTest.php

示例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']]);
         }
     }
 }
开发者ID:xamin123,项目名称:platform,代码行数:34,代码来源:EmailTemplateSyntaxValidator.php

示例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);
     }
 }
开发者ID:goetas,项目名称:twital-bundle,代码行数:8,代码来源:TwitalExtractor.php

示例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();
 }
开发者ID:sitesupra,项目名称:sitesupra,代码行数:11,代码来源:TwigProcessor.php

示例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();
 }
开发者ID:tejdeeps,项目名称:tejcs.com,代码行数:10,代码来源:TwigExtractor.php

示例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);
     }
 }
开发者ID:ratasxy,项目名称:symfony,代码行数:13,代码来源:TwigExtractor.php

示例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();
 }
开发者ID:yceruto,项目名称:symfony,代码行数:10,代码来源:TwigExtractor.php

示例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'));
 }
开发者ID:fdelgados,项目名称:building-testable-applications,代码行数:10,代码来源:ExpressionParserTest.php

示例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'));
    }
开发者ID:nizsheanez,项目名称:PolymorphCMS,代码行数:11,代码来源:ExpressionParserTest.php

示例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);
 }
开发者ID:snailwork,项目名称:rayjack,代码行数:10,代码来源:RoutingExtensionTest.php

示例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());
 }
开发者ID:bigbitecreative,项目名称:wordpress-git-content,代码行数:10,代码来源:EnvironmentTest.php

示例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);
 }
开发者ID:Nikola-xiii,项目名称:d8intranet,代码行数:12,代码来源:TwigExtensionTest.php

示例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));
 }
开发者ID:brieucthomas,项目名称:elixir-twig-extension,代码行数:10,代码来源:ElixirExtensionTest.php

示例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 . ')');
     }
 }
开发者ID:Broncko,项目名称:Savant,代码行数:32,代码来源:integrationTest.php

示例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;
 }
开发者ID:amaxlab,项目名称:speller,代码行数:33,代码来源:TwigParser.php


注:本文中的Twig_Environment::tokenize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。