/** * Compiles the node to PHP. * * @param \Twig_Compiler $compiler A Twig_Compiler instance */ public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); $vars = $this->getNode('vars'); $defaults = new \Twig_Node_Expression_Array(array(), -1); if ($vars instanceof \Twig_Node_Expression_Array) { $defaults = $this->getNode('vars'); $vars = null; } list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars); $method = null === $this->getNode('count') ? 'trans' : 'transChoice'; $compiler->write('echo $this->env->getExtension(\'translator\')->getTranslator()->' . $method . '(')->subcompile($msg); $compiler->raw(', '); if (null !== $this->getNode('count')) { $compiler->subcompile($this->getNode('count'))->raw(', '); } if (null !== $vars) { $compiler->raw('array_merge(')->subcompile($defaults)->raw(', ')->subcompile($this->getNode('vars'))->raw(')'); } else { $compiler->subcompile($defaults); } $compiler->raw(', '); if (null === $this->getNode('domain')) { $compiler->repr('messages'); } else { $compiler->subcompile($this->getNode('domain')); } if (null !== $this->getNode('locale')) { $compiler->raw(', ')->subcompile($this->getNode('locale')); } $compiler->raw(");\n"); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler->addDebugInfo($this); if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif ($this->isSpecial()) { $compiler->raw($this->specialVars[$name]); } elseif ($this->getAttribute('always_defined')) { $compiler->raw('$context[')->string($name)->raw(']'); } else { if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : null)'); } else { // When Twig will require PHP 7.0, the Template::notFound() method // will be removed and the code inlined like this: // (function () { throw new Exception(...); })(); $compiler->raw('(isset($context[')->string($name)->raw(']) || array_key_exists(')->string($name)->raw(', $context) ? $context[')->string($name)->raw('] : $this->notFound(')->string($name)->raw(', ')->repr($this->lineno)->raw('))'); } } }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif ($this->isSpecial()) { $compiler->raw($this->specialVars[$name]); } elseif ($this->getAttribute('always_defined')) { $compiler->raw('$context[')->string($name)->raw(']'); } else { // remove the non-PHP 5.4 version when PHP 5.3 support is dropped // as the non-optimized version is just a workaround for slow ternary operator // when the context has a lot of variables if (version_compare(phpversion(), '5.4.0RC1', '>=')) { // PHP 5.4 ternary operator performance was optimized $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } else { $compiler->raw('$this->getContext($context, ')->string($name); if ($this->getAttribute('ignore_strict_check')) { $compiler->raw(', true'); } $compiler->raw(')'); } } }
protected function compileDefaults(\Twig_Compiler $compiler, \Twig_Node_Expression_Array $defaults) { $compiler->raw('array('); foreach ($defaults as $name => $default) { $compiler->repr($name)->raw(' => ')->subcompile($default)->raw(', '); } $compiler->raw(')'); }
private function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset) { $compiler->repr($asset->getTargetPath()); if (file_exists(PUB_DIR . DS . $asset->getTargetPath())) { return; } $writer = new \Assetic\AssetWriter(PUB_DIR . DS); $writer->writeAsset($asset); }
/** * Compiles the node to PHP. * * @param Twig_Compiler A Twig_Compiler instance */ public function compile(Twig_Compiler $compiler) { $compiler->raw('array('); $first = true; foreach ($this->nodes as $name => $node) { if (!$first) { $compiler->raw(', '); } $first = false; $compiler->repr($name)->raw(' => ')->subcompile($node); } $compiler->raw(')'); }
public function testReprNumericValueWithLocale() { $compiler = new Twig_Compiler(new Twig_Environment()); $locale = setlocale(LC_NUMERIC, 0); if (false === $locale) { $this->markTestSkipped('Your platform does not support locales.'); } $required_locales = ['fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252']; if (false === setlocale(LC_NUMERIC, $required_locales)) { $this->markTestSkipped('Could not set any of required locales: ' . implode(", ", $required_locales)); } $this->assertEquals('1.2', $compiler->repr(1.2)->getSource()); $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0))); setlocale(LC_NUMERIC, $locale); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif ($this->isSpecial()) { $compiler->raw($this->specialVars[$name]); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw(')'); } }
public function compile(Twig_Compiler $compiler) { $compiler->raw('$this->env->view->getHelper('); $compiler->repr($this->getAttribute('helper')); $compiler->raw(')->' . $this->getAttribute('helper') . '('); $first = true; foreach ($this->getNode('args') as $arg) { if (!$first) { $compiler->raw(', '); } $first = false; $compiler->subcompile($arg); } $compiler->raw(")"); }
public function compile(Twig_Compiler $compiler) { static $specialVars = array('_self' => '$this', '_context' => '$context', '_charset' => '$this->env->getCharset()'); $name = $this->getAttribute('name'); if ($this->hasAttribute('is_defined_test')) { if (isset($specialVars[$name])) { $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif (isset($specialVars[$name])) { $compiler->raw($specialVars[$name]); } else { $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name)); } }
protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name) { if (!($vars = $asset->getVars())) { $compiler->repr($asset->getTargetPath()); return; } $compiler->raw("strtr(")->string($asset->getTargetPath())->raw(", array("); $first = true; foreach ($vars as $var) { if (!$first) { $compiler->raw(", "); } $first = false; $compiler->string("{" . $var . "}")->raw(" => \$context['assetic']['vars']['{$var}']"); } $compiler->raw("))"); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler->addDebugInfo($this); if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { if ('_self' === $name) { @trigger_error(sprintf('Global variable "_self" is deprecated in %s at line %d', '?', $this->getLine()), E_USER_DEPRECATED); } $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif ($this->isSpecial()) { if ('_self' === $name) { @trigger_error(sprintf('Global variable "_self" is deprecated in %s at line %d', '?', $this->getLine()), E_USER_DEPRECATED); } $compiler->raw($this->specialVars[$name]); } elseif ($this->getAttribute('always_defined')) { $compiler->raw('$context[')->string($name)->raw(']'); } else { // remove the non-PHP 5.4 version when PHP 5.3 support is dropped // as the non-optimized version is just a workaround for slow ternary operator // when the context has a lot of variables if (PHP_VERSION_ID >= 50400) { // PHP 5.4 ternary operator performance was optimized $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } else { $compiler->raw('$this->getContext($context, ')->string($name); if ($this->getAttribute('ignore_strict_check')) { $compiler->raw(', true'); } $compiler->raw(')'); } } }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler->addDebugInfo($this); if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif ($this->isSpecial()) { $compiler->raw($this->specialVars[$name]); } elseif ($this->getAttribute('always_defined')) { $compiler->raw('$context[')->string($name)->raw(']'); } else { if (PHP_VERSION_ID >= 70000) { // use PHP 7 null coalescing operator $compiler->raw('($context[')->string($name)->raw('] ?? '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } elseif (PHP_VERSION_ID >= 50400) { // PHP 5.4 ternary operator performance was optimized $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } else { $compiler->raw('$this->getContext($context, ')->string($name); if ($this->getAttribute('ignore_strict_check')) { $compiler->raw(', true'); } $compiler->raw(')'); } } }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler->addDebugInfo($this); if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif ($this->isSpecial()) { $compiler->raw($this->specialVars[$name]); } elseif ($this->getAttribute('always_defined')) { $compiler->raw('$context[')->string($name)->raw(']'); } else { $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif ($this->isSpecial()) { $compiler->raw($this->specialVars[$name]); } else { if (version_compare(phpversion(), '5.4.0RC1', '>=') && ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables())) { // PHP 5.4 ternary operator performance was optimized $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name); if ($this->getAttribute('ignore_strict_check')) { $compiler->raw(', true'); } $compiler->raw(')'); } } }
protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name) { $compiler->repr($asset->getTargetPath()); }
public function compile(Twig_Compiler $compiler) { $compiler->repr($this->getAttribute('value')); }