/** * Compiles the node to PHP. * * @param Twig_Compiler A Twig_Compiler instance */ public function compile(Twig_Compiler $compiler) { $compiler->addDebugInfo($this); list($msg, $vars) = $this->compileString($this->nodes['body']); if (null !== $this->nodes['plural']) { list($msg1, $vars1) = $this->compileString($this->nodes['plural']); $vars = array_merge($vars, $vars1); } $function = null === $this->nodes['plural'] ? '__' : '__n'; if ($vars) { $compiler->write('echo strtr(' . $function . '(')->subcompile($msg); if (null !== $this->nodes['plural']) { $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->nodes['count'])->raw(')'); } $compiler->raw(', true), array('); // modified: cakephp $return flag foreach ($vars as $var) { if ('count' === $var->getAttribute('name')) { $compiler->string('%count%')->raw(' => abs(')->subcompile($this->nodes['count'])->raw('), '); } else { $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', '); } } $compiler->raw("));\n"); } else { $compiler->write('echo ' . $function . '(')->subcompile($msg); if (null !== $this->nodes['plural']) { $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->nodes['count'])->raw(')'); } $compiler->raw(', true);'); // modified: cakephp $return flag } }
/** * Compiles the node to PHP. * * @param Twig_Compiler A Twig_Compiler instance */ public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); list($msg, $vars) = $this->compileString($this->getNode('body')); if (null !== $this->getNode('plural')) { list($msg1, $vars1) = $this->compileString($this->getNode('plural')); $vars = array_merge($vars, $vars1); } $function = null === $this->getNode('plural') ? '\\Skeleton\\I18n\\Translation::translate' : '\\Skeleton\\I18n\\Translation::translate_plural'; if ($vars) { $compiler->write('echo strtr(' . $function . '(')->subcompile($msg); if (null !== $this->getNode('plural')) { $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')'); } $compiler->raw(', $context[\'env\'][\'translation\']), array('); foreach ($vars as $var) { if ('count' === $var->getAttribute('name')) { $compiler->string('%count%')->raw(' => abs(')->subcompile($this->getNode('count'))->raw('), '); } else { $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', '); } } $compiler->raw("));\n"); } else { $compiler->write('echo ' . $function . '(')->subcompile($msg); if (null !== $this->getNode('plural')) { $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')'); } $compiler->raw(", " . '$context[\'env\'][\'translation\']' . ");\n"); } }
/** * Compiles the node to PHP. * * @param Twig_Compiler A Twig_Compiler instance */ public function compile(Twig_Compiler $compiler) { $compiler->addDebugInfo($this); list($msg, $vars) = $this->compileString($this->getNode('body')); if (null !== $this->getNode('plural')) { list($msg1, $vars1) = $this->compileString($this->getNode('plural')); $vars = array_merge($vars, $vars1); } $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext'; if ($vars) { $compiler->write('echo strtr(' . $function . '(')->subcompile($msg); if (null !== $this->getNode('plural')) { $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')'); } $compiler->raw('), array('); foreach ($vars as $var) { if ('count' === $var->getAttribute('name')) { $compiler->string('%count%')->raw(' => abs(')->subcompile($this->getNode('count'))->raw('), '); } else { $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', '); } } $compiler->raw("));\n"); } else { $compiler->write('echo ' . $function . '(')->subcompile($msg); if (null !== $this->getNode('plural')) { $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')'); } $compiler->raw(");\n"); } }
public function compile(Twig_Compiler $compiler) { $function = $compiler->getEnvironment()->getFunction($this->getAttribute('name')); $compiler->raw($function->compile() . '('); $first = true; if ($function->needsEnvironment()) { $compiler->raw('$this->env'); $first = false; } if ($function->needsContext()) { if (!$first) { $compiler->raw(', '); } $compiler->raw('$context'); $first = false; } foreach ($function->getArguments() as $argument) { if (!$first) { $compiler->raw(', '); } $compiler->string($argument); $first = false; } foreach ($this->getNode('arguments') as $node) { if (!$first) { $compiler->raw(', '); } $compiler->subcompile($node); $first = false; } $compiler->raw(')'); }
/** * Compiles the node to PHP. * * @param Twig_Compiler $compiler A Twig_Compiler instance */ public function compile(Twig_Compiler $compiler) { $compiler->addDebugInfo($this); list($msg, $vars) = $this->compileString($this->getNode('body')); $compiler->write('echo __(trim(')->subcompile($msg)->write('), array('); foreach ($vars as $var) { $compiler->string(':' . $var['name'])->raw(' => ')->subcompile($var)->raw(', '); } $compiler->raw("));\n"); }
protected function compileFilter(Twig_Compiler $compiler, Twig_FilterInterface $filter) { $compiler->raw($filter->compile() . '(')->raw($filter->needsEnvironment() ? '$this->env, ' : '')->raw($filter->needsContext() ? '$context, ' : ''); foreach ($filter->getArguments() as $argument) { $compiler->string($argument)->raw(', '); } $compiler->subcompile($this->getNode('node')); foreach ($this->getNode('arguments') as $node) { $compiler->raw(', ')->subcompile($node); } $compiler->raw(')'); }
/** * {@inheritdoc} */ public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); $options = $this->getNode('options'); list($singular, $tokens) = $this->compileString($this->getNode('body')); $plural = NULL; if (NULL !== $this->getNode('plural')) { list($plural, $pluralTokens) = $this->compileString($this->getNode('plural')); $tokens = array_merge($tokens, $pluralTokens); } // Start writing with the function to be called. $compiler->write('echo ' . (empty($plural) ? 't' : '\\Drupal::translation()->formatPlural') . '('); // Move the count to the beginning of the parameters list. if (!empty($plural)) { $compiler->raw('abs(')->subcompile($this->getNode('count'))->raw('), '); } // Write the singular text parameter. $compiler->subcompile($singular); // Write the plural text parameter, if necessary. if (!empty($plural)) { $compiler->raw(', ')->subcompile($plural); } // Write any tokens found as an associative array parameter, otherwise just // leave as an empty array. $compiler->raw(', array('); foreach ($tokens as $token) { $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', '); } $compiler->raw(')'); // Write any options passed. if (!empty($options)) { $compiler->raw(', ')->subcompile($options); } // Write function closure. $compiler->raw(')'); // Append translation debug markup, if necessary. if ($compiler->getEnvironment()->isDebug()) { $compiler->raw(" . '\n<!-- TRANSLATION: "); $compiler->subcompile($singular); if (!empty($plural)) { $compiler->raw(', PLURAL: ')->subcompile($plural); } if (!empty($options)) { foreach ($options->getKeyValuePairs() as $pair) { $compiler->raw(', ' . Unicode::strtoupper($pair['key']->getAttribute('value')) . ': ')->subcompile($pair['value']); } } $compiler->raw(" -->\n'"); } // End writing. $compiler->raw(";\n"); }
/** * Compiles the node to PHP. * * @param Twig_Compiler $compiler A Twig_Compiler instance */ public function compile(Twig_Compiler $compiler) { $compiler->addDebugInfo($this); list($msg, $vars) = $this->compileString($this->getNode('body')); if (null !== $this->getNode('plural')) { list($msg1, $vars1) = $this->compileString($this->getNode('plural')); $vars = array_merge($vars, $vars1); } $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext'; if (null !== ($notes = $this->getNode('notes'))) { $message = trim($notes->getAttribute('data')); // line breaks are not allowed cause we want a single line comment $message = str_replace(array("\n", "\r"), ' ', $message); $compiler->write("// notes: {$message}\n"); } if ($vars) { $compiler->write('echo strtr(' . $function . '(')->subcompile($msg); if (null !== $this->getNode('plural')) { $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')'); } $compiler->raw('), array('); foreach ($vars as $var) { if ('count' === $var->getAttribute('name')) { $compiler->string('%count%')->raw(' => abs(')->subcompile($this->getNode('count'))->raw('), '); } else { $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', '); } } $compiler->raw("));\n"); } else { $compiler->write('echo ' . $function . '(')->subcompile($msg); if (null !== $this->getNode('plural')) { $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')'); } $compiler->raw(");\n"); } }
protected function compileArguments(Twig_Compiler $compiler) { $compiler->raw('('); $first = true; if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) { $compiler->raw('$this->env'); $first = false; } if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) { if (!$first) { $compiler->raw(', '); } $compiler->raw('$context'); $first = false; } if ($this->hasAttribute('arguments')) { foreach ($this->getAttribute('arguments') as $argument) { if (!$first) { $compiler->raw(', '); } $compiler->string($argument); $first = false; } } if ($this->hasNode('node')) { if (!$first) { $compiler->raw(', '); } $compiler->subcompile($this->getNode('node')); $first = false; } if ($this->hasNode('arguments') && null !== $this->getNode('arguments')) { $callable = $this->hasAttribute('callable') ? $this->getAttribute('callable') : null; $arguments = $this->getArguments($callable, $this->getNode('arguments')); foreach ($arguments as $node) { if (!$first) { $compiler->raw(', '); } $compiler->subcompile($node); $first = false; } } $compiler->raw(')'); }
/** * {@inheritdoc} */ public function compile(\Twig_Compiler $compiler) { $compiler->write("if (\$this->env->isDebug()) {\n")->indent(); if (!$this->hasNode('values')) { // remove embedded templates (macros) from the context $compiler->write(sprintf('$%svars = array();' . "\n", $this->varPrefix))->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {' . "\n", $this->varPrefix))->indent()->write(sprintf('if (!$%sval instanceof \\Twig_Template) {' . "\n", $this->varPrefix))->indent()->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;' . "\n", $this->varPrefix))->outdent()->write("}\n")->outdent()->write("}\n")->addDebugInfo($this)->write(sprintf('\\Symfony\\Component\\VarDumper\\VarDumper::dump($%svars);' . "\n", $this->varPrefix)); } elseif (($values = $this->getNode('values')) && 1 === $values->count()) { $compiler->addDebugInfo($this)->write('\\Symfony\\Component\\VarDumper\\VarDumper::dump(')->subcompile($values->getNode(0))->raw(");\n"); } else { $compiler->addDebugInfo($this)->write('\\Symfony\\Component\\VarDumper\\VarDumper::dump(array(' . "\n")->indent(); foreach ($values as $node) { $compiler->addIndentation(); if ($node->hasAttribute('name')) { $compiler->string($node->getAttribute('name'))->raw(' => '); } $compiler->subcompile($node)->raw(",\n"); } $compiler->outdent()->write("));\n"); } $compiler->outdent()->write("}\n"); }
/** * {@inheritdoc} */ public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); $options = $this->getNode('options'); list($singular, $tokens) = $this->compileString($this->getNode('body')); $plural = NULL; if (NULL !== $this->getNode('plural')) { list($plural, $pluralTokens) = $this->compileString($this->getNode('plural')); $tokens = array_merge($tokens, $pluralTokens); } // Start writing with the function to be called. $compiler->write('echo ' . (empty($plural) ? 't' : '\\Drupal::translation()->formatPlural') . '('); // Move the count to the beginning of the parameters list. if (!empty($plural)) { $compiler->raw('abs(')->subcompile($this->getNode('count'))->raw('), '); } // Write the singular text parameter. $compiler->subcompile($singular); // Write the plural text parameter, if necessary. if (!empty($plural)) { $compiler->raw(', ')->subcompile($plural); } // Write any tokens found as an associative array parameter, otherwise just // leave as an empty array. $compiler->raw(', array('); foreach ($tokens as $token) { $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', '); } $compiler->raw(')'); // Write any options passed. if (!empty($options)) { $compiler->raw(', ')->subcompile($options); } // Write function closure. $compiler->raw(')'); // @todo Add debug output, see https://www.drupal.org/node/2512672 // End writing. $compiler->raw(";\n"); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); if (false === ($function = $compiler->getEnvironment()->getFunction($name))) { $message = sprintf('The function "%s" does not exist', $name); if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFunctions()))) { $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives)); } throw new Twig_Error_Syntax($message, $this->getLine()); } $compiler->raw($function->compile() . '('); $first = true; if ($function->needsEnvironment()) { $compiler->raw('$this->env'); $first = false; } if ($function->needsContext()) { if (!$first) { $compiler->raw(', '); } $compiler->raw('$context'); $first = false; } foreach ($function->getArguments() as $argument) { if (!$first) { $compiler->raw(', '); } $compiler->string($argument); $first = false; } foreach ($this->getNode('arguments') as $node) { if (!$first) { $compiler->raw(', '); } $compiler->subcompile($node); $first = false; } $compiler->raw(')'); }
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("))"); }
/** * Compile arguments to be used by the node. * * @param \Twig_Compiler $compiler A Twig compiler instance. */ protected function compileArguments(\Twig_Compiler $compiler) { $compiler->raw('('); $first = true; if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) { $compiler->raw('$this->env'); $first = false; } if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) { if (!$first) { $compiler->raw(', '); } $compiler->raw('$context'); $first = false; } if ($this->hasAttribute('arguments')) { foreach ($this->getAttribute('arguments') as $argument) { if (!$first) { $compiler->raw(', '); } $compiler->string($argument); $first = false; } } if ($this->hasNode('node')) { if (!$first) { $compiler->raw(', '); } $compiler->raw('$compiled'); $first = false; } $compiler->raw(')'); }