addTokenParser() public méthode

Registers a Token Parser.
public addTokenParser ( Twig_TokenParserInterface $parser )
$parser Twig_TokenParserInterface A Twig_TokenParserInterface instance
 private function getEnv()
 {
     $env = new \Twig_Environment();
     $env->addTokenParser(new TwigJsTokenParser());
     $env->setLoader(new \Twig_Loader_String());
     return $env;
 }
Exemple #2
0
 public function testParseIsReentrant()
 {
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('autoescape' => false, 'optimizations' => 0));
     $twig->addTokenParser(new TestTokenParser());
     $parser = new Twig_Parser($twig);
     $parser->parse(new Twig_TokenStream(array(new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', 1), new Twig_Token(Twig_Token::NAME_TYPE, 'test', 1), new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', 1), new Twig_Token(Twig_Token::VAR_START_TYPE, '', 1), new Twig_Token(Twig_Token::NAME_TYPE, 'foo', 1), new Twig_Token(Twig_Token::VAR_END_TYPE, '', 1), new Twig_Token(Twig_Token::EOF_TYPE, '', 1))));
     $this->assertNull($parser->getParent());
 }
 /**
  * @dataProvider getTestsForRender
  */
 public function testCompile($source, $expected)
 {
     $env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
     $env->addTokenParser(new RenderTokenParser());
     $stream = $env->tokenize($source);
     $parser = new \Twig_Parser($env);
     $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
 }
 /**
  * @dataProvider getTestsForFormTheme
  */
 public function testCompile($source, $expected)
 {
     $env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
     $env->addTokenParser(new FormThemeTokenParser());
     $stream = $env->tokenize(new \Twig_Source($source, ''));
     $parser = new \Twig_Parser($env);
     $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
 }
 public function testParseIsReentrant()
 {
     $twig = new Twig_Environment(null, ['autoescape' => false, 'optimizations' => 0]);
     $twig->addTokenParser(new TestTokenParser());
     $parser = new Twig_Parser($twig);
     $parser->parse(new Twig_TokenStream([new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', 1), new Twig_Token(Twig_Token::NAME_TYPE, 'test', 1), new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', 1), new Twig_Token(Twig_Token::VAR_START_TYPE, '', 1), new Twig_Token(Twig_Token::NAME_TYPE, 'foo', 1), new Twig_Token(Twig_Token::VAR_END_TYPE, '', 1), new Twig_Token(Twig_Token::EOF_TYPE, '', 1)]));
     $this->assertNull($parser->getParent());
 }
 /**
  * @return \Twig_Parser
  * @throws \Exception
  * @throws \Twig_Error_Syntax
  */
 private function getParser()
 {
     $stream = new \Twig_TokenStream(array(new \Twig_Token(\Twig_Token::TEXT_TYPE, '<!DOCTYPE html><html><body>', 1), new \Twig_Token(\Twig_Token::BLOCK_START_TYPE, '', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'form', 2), new \Twig_Token(\Twig_Token::PUNCTUATION_TYPE, '(', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'method', 2), new \Twig_Token(\Twig_Token::OPERATOR_TYPE, '=', 2), new \Twig_Token(\Twig_Token::STRING_TYPE, 'post', 2), new \Twig_Token(\Twig_Token::PUNCTUATION_TYPE, ',', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'action', 2), new \Twig_Token(\Twig_Token::OPERATOR_TYPE, '=', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'request_uri', 2), new \Twig_Token(\Twig_Token::PUNCTUATION_TYPE, '|', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'e', 2), new \Twig_Token(\Twig_Token::PUNCTUATION_TYPE, ')', 2), new \Twig_Token(\Twig_Token::BLOCK_END_TYPE, '', 2), new \Twig_Token(\Twig_Token::TEXT_TYPE, '<input type="submit" value="Submit">', 3), new \Twig_Token(\Twig_Token::BLOCK_START_TYPE, '', 4), new \Twig_Token(\Twig_Token::NAME_TYPE, 'endform', 4), new \Twig_Token(\Twig_Token::BLOCK_END_TYPE, '', 4), new \Twig_Token(\Twig_Token::TEXT_TYPE, '</body></html>', 5), new \Twig_Token(\Twig_Token::EOF_TYPE, '', 5)));
     $env = new \Twig_Environment();
     $env->addTokenParser(new FormTokenParser());
     $parser = new \Twig_Parser($env);
     $parser->parse($stream);
     return $parser;
 }
 /**
  * @dataProvider getTestsForRender
  */
 public function testCompile($enabled, $source, $expected)
 {
     $translator = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
     $env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
     $env->addTokenParser(new TemplateBoxTokenParser($enabled, $translator));
     $stream = $env->tokenize($source);
     $parser = new \Twig_Parser($env);
     $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
 }
 /**
  * @dataProvider getCompileTestData
  *
  * @param ContentEditableNode $expectedNode
  * @param string              $source
  * @param string              $message
  */
 public function testCompile(ContentEditableNode $expectedNode, $source, $message = '')
 {
     $env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), ['cache' => false, 'autoescape' => false, 'optimizations' => 0]);
     $env->addTokenParser(new ContentEditableTokenParser());
     $parser = new \Twig_Parser($env);
     $stream = $env->tokenize($source);
     $node = $parser->parse($stream)->getNode('body')->getNode(0);
     $this->assertEquals($expectedNode, $node, $message);
 }
 protected function _initializeTwig($loader)
 {
     $twig = new Twig_Environment($loader);
     $engine = new \Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine();
     $extension = new \Aptoma\Twig\Extension\MarkdownExtension($engine);
     $twig->addExtension($extension);
     $tokenParser = new \Aptoma\Twig\TokenParser\MarkdownTokenParser($engine);
     $twig->addTokenParser($tokenParser);
     return $twig;
 }
Exemple #10
0
 /**
  * Create twig environment with the options specified in the curry cms configuration.
  *
  * @param Twig_LoaderInterface $loader
  * @return Twig_Environment
  */
 private static function createTwigEnvironment(Twig_LoaderInterface $loader)
 {
     $options = Curry_Core::$config->curry->template->options->toArray();
     $twig = new Twig_Environment($loader, $options);
     $twig->setParser(new Curry_Twig_Parser($twig));
     $twig->addTokenParser(new Curry_Twig_TokenParser_Placeholder());
     $twig->addTokenParser(new Curry_Twig_TokenParser_Ia());
     $twig->addFunction('url', new Twig_Function_Function('url'));
     $twig->addFunction('L', new Twig_Function_Function('L'));
     $twig->addFunction('round', new Twig_Function_Function('round'));
     $twig->addFilter('ldate', new Twig_Filter_Function('Curry_Twig_Template::ldate'));
     $twig->addFilter('dump', new Twig_Filter_Function('var_dump'));
     return $twig;
 }
Exemple #11
0
 /**
  * @dataProvider getTests
  */
 public function testGrammar($tag, $grammar, $template, $output, $exception)
 {
     $twig = new Twig_Environment(new Twig_Loader_String(), array('cache' => false));
     $twig->addTokenParser(new SimpleTokenParser($tag, $grammar));
     $ok = true;
     try {
         $template = $twig->loadTemplate($template);
     } catch (Exception $e) {
         $ok = false;
         if (false === $exception) {
             $this->fail('Exception not expected');
         } else {
             $this->assertEquals($exception, get_class($e));
         }
     }
     if ($ok) {
         if (false !== $exception) {
             $this->fail(sprintf('Exception "%s" expected', $exception));
         }
         $actual = $template->render(array());
         $this->assertEquals($output, $actual);
     }
 }
Exemple #12
0
 /**
  * Function returning the Twig environnment
  * 
  * @return \Twig_Environment
  */
 private function getTwigEnvironment()
 {
     $arrcache = array();
     if (TEMPLATE_CACHE) {
         $cachepath = CACHEPATH . DS . 'twig';
         $arrcache['cache'] = $cachepath;
         $arrcache['auto_reload'] = AUTORELOADCACHE;
     }
     if (DEVELOPMENT_ENVIRONMENT) {
         $arrcache['debug'] = true;
     }
     $twigloader = new \Twig_Loader_Filesystem($this->getPathArray());
     $twigenv = new \Twig_Environment($twigloader, $arrcache);
     $twigenv->addExtension(new \GL\Core\Twig\TwigHelper($this->_container));
     // add shared TwigHelper
     $config = new \GL\Core\Config\Config('twig');
     $value = $config->load();
     foreach ($value as $name => $th) {
         $class = $th['class'];
         $twigenv->addExtension(new $class($this->_container));
     }
     //$twigenv->addExtension(new \Application\Shared\SharedTwigHelper($this->_container));
     if (DEVELOPMENT_ENVIRONMENT) {
         $twigenv->addExtension(new \GL\Core\Debug\TwigDebugBar($this->_container));
         $twigenv->addExtension(new \Twig_Extension_Debug());
         $this->_profile = new \Twig_Profiler_Profile();
         $twigenv->addExtension(new \Twig_Extension_Profiler($this->_profile));
     }
     $twigenv->addTokenParser(new \GL\Core\Twig\TwigRenderToken());
     $twigenv->addTokenParser(new \GL\Core\Twig\TwigRouteToken());
     return $twigenv;
 }
Exemple #13
0
 /**
  * @return \Twig_Environment
  */
 public function get()
 {
     $class = $this->injector->getInstance(FormTokenParser::class);
     $this->twig->addTokenParser($class);
     return $this->twig;
 }
Exemple #14
0
 public function boot()
 {
     // Create services
     $this->singleton('logger', array($this, 'getLogger'));
     $this->singleton('cache', array($this, 'getCache'));
     $this->singleton('index', array($this, 'getIndex'));
     $this->singleton('autoloader', array($this, 'getAutoloader'));
     // some more
     $app = $this;
     $this->singleton('dispatcher', function () use($app) {
         return new EventDispatcher();
     });
     $this->singleton('resolver', function () use($app) {
         return new ControllerResolver($app->logger);
     });
     $this->singleton('kernel', function () use($app) {
         return new HttpKernel($app->dispatcher, $app->resolver, $app->requestStack);
     });
     $this->singleton('requestStack', function () use($app) {
         return new RequestStack();
     });
     $this->singleton('backend', function () use($app) {
         return new Backend($app);
     });
     $this->singleton('whoopsHandler', function () use($app) {
         if (PHP_SAPI === 'cli') {
             return new PlainTextHandler();
         } else {
             if ($this['developmentMode']) {
                 return new PrettyPageHandler();
             } else {
                 return new CallbackHandler(array($app, 'showException'));
             }
         }
     });
     $this->singleton('whoops', function () use($app) {
         $whoops = new \Whoops\Run();
         $whoops->pushHandler($app->whoopsHandler);
         // Send error mail
         if ($app['errorNotification']) {
             $whoops->pushHandler(array($app, 'sendErrorNotification'));
         }
         // Add error to log
         $whoops->pushHandler(function (\Exception $e) use($app) {
             $app->logger->error($e->getMessage(), array('exception' => $e));
             return Handler::DONE;
         });
         return $whoops;
     });
     $this->singleton('twig', function () use($app) {
         $loader = new \Twig_Loader_Filesystem($app['template.root']);
         $options = $app['template.options'];
         $twig = new \Twig_Environment($loader, $options);
         $twig->setParser(new \Curry_Twig_Parser($twig));
         $twig->addTokenParser(new \Curry_Twig_TokenParser_Placeholder());
         $twig->addTokenParser(new \Curry_Twig_TokenParser_Ia());
         $twig->addFunction('url', new \Twig_Function_Function('url'));
         $twig->addFunction('L', new \Twig_Function_Function('L'));
         $twig->addFunction('round', new \Twig_Function_Function('round'));
         $twig->addFilter('ldate', new \Twig_Filter_Function('\\Curry\\App::ldate'));
         $twig->addFilter('dump', new \Twig_Filter_Function('var_dump'));
         return $twig;
     });
     $this->whoops->register();
     if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
         $this->logger->warning('Magic quotes gpc is enabled, please disable!');
     }
     // TODO: remove this!
     $this->globals = (object) array('ProjectName' => $this['name'], 'BaseUrl' => $this['baseUrl'], 'DevelopmentMode' => $this['developmentMode']);
     // Try to set utf-8 locale
     $arguments = (array) $this['locale'];
     array_unshift($arguments, LC_ALL);
     $locale = call_user_func_array('setlocale', $arguments);
     $this->logger->debug($locale ? 'Set default locale to ' . $locale : 'Unable to set default locale');
     // Set default umask
     if ($this['umask'] !== false) {
         umask($this['umask']);
     }
     self::initErrorHandling();
     self::initPropel();
     URL::setDefaultBaseUrl($this['baseUrl']);
     URL::setDefaultSecret($this['secret']);
     if ($this['autoPublish']) {
         $this->autoPublish();
     }
     if ($this['sharedController']) {
         $this->logger->notice('Using php routing for curry shared folder');
         $this->dispatcher->addSubscriber(new StaticContent('/shared/', $app['basePath'] . '/shared'));
     }
     if ($app['backend.basePath']) {
         $this->dispatcher->addSubscriber($app->backend);
     }
     if (class_exists('Page')) {
         $this->dispatcher->addSubscriber(new Frontend($this));
     }
     $this->dispatcher->addSubscriber(new FileNotFound($this));
     $this->dispatcher->addSubscriber(new Generator\ModuleProfiler($app->logger));
     $this->dispatcher->addSubscriber(new Generator\ModuleCacher($app->cache));
     $this->dispatcher->addSubscriber(new Generator\ModuleHtmlHead());
     $this->dispatcher->addSubscriber(new Generator\LiveEdit($this));
 }