本文整理汇总了PHP中Twig_Compiler::getEnvironment方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Compiler::getEnvironment方法的具体用法?PHP Twig_Compiler::getEnvironment怎么用?PHP Twig_Compiler::getEnvironment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Compiler
的用法示例。
在下文中一共展示了Twig_Compiler::getEnvironment方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: compile
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() . '(')->raw($function->needsEnvironment() ? '$this->env' : '');
if ($function->needsContext()) {
$compiler->raw($function->needsEnvironment() ? ', $context' : '$context');
}
$first = true;
foreach ($this->getNode('arguments') as $node) {
if (!$first) {
$compiler->raw(', ');
} else {
if ($function->needsEnvironment() || $function->needsContext()) {
$compiler->raw(', ');
}
$first = false;
}
$compiler->subcompile($node);
}
$compiler->raw(')');
}
示例2: compile
public function compile(Twig_Compiler $compiler)
{
$name = $this->getAttribute('name');
$testMap = $compiler->getEnvironment()->getTests();
if (!isset($testMap[$name])) {
$message = sprintf('The test "%s" does not exist', $name);
if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getTests()))) {
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
}
throw new Twig_Error_Syntax($message, $this->getLine());
}
$name = $this->getAttribute('name');
$node = $this->getNode('node');
$compiler->raw($testMap[$name]->compile() . '(')->subcompile($node);
if (null !== $this->getNode('arguments')) {
$compiler->raw(', ');
$max = count($this->getNode('arguments')) - 1;
foreach ($this->getNode('arguments') as $i => $arg) {
$compiler->subcompile($arg);
if ($i != $max) {
$compiler->raw(', ');
}
}
}
$compiler->raw(')');
}
示例3: compile
public function compile(Twig_Compiler $compiler)
{
$filterMap = $compiler->getEnvironment()->getFilters();
$name = $this->getNode('filter')->getAttribute('value');
if (!isset($filterMap[$name])) {
throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine());
}
$filter = $filterMap[$name];
// The default filter is intercepted when the filtered value
// is a name (like obj) or an attribute (like obj.attr)
// In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }}
if ('default' === $name && ($this->getNode('node') instanceof Twig_Node_Expression_Name || $this->getNode('node') instanceof Twig_Node_Expression_GetAttr)) {
$compiler->raw('(');
if ($this->getNode('node') instanceof Twig_Node_Expression_Name) {
$testMap = $compiler->getEnvironment()->getTests();
$compiler->raw($testMap['defined']->compile() . '(')->repr($this->getNode('node')->getAttribute('name'))->raw(', $context)');
} elseif ($this->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
$this->getNode('node')->setAttribute('is_defined_test', true);
$compiler->subcompile($this->getNode('node'));
}
$compiler->raw(' ? ');
$this->compileFilter($compiler, $filter);
$compiler->raw(' : ');
$compiler->subcompile($this->getNode('arguments')->getNode(0));
$compiler->raw(')');
} else {
$this->compileFilter($compiler, $filter);
}
}
示例4: compileClassHeader
protected function compileClassHeader(Twig_Compiler $compiler)
{
$compiler->write("<?php\n\n")->write("/* " . str_replace('*/', '* /', $this->getAttribute('filename')) . " */\n")->write('class ' . $compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename')))->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))->write("{\n")->indent();
if (null !== $this->getNode('parent')) {
$compiler->write("protected \$parent;\n\n");
}
}
示例5: compile
public function compile(Twig_Compiler $compiler)
{
$name = $this->getNode('filter')->getAttribute('value');
if (false === ($filter = $compiler->getEnvironment()->getFilter($name))) {
$message = sprintf('The filter "%s" does not exist', $name);
if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFilters()))) {
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
}
throw new Twig_Error_Syntax($message, $this->getLine());
}
$this->compileFilter($compiler, $filter);
}
示例6: compile
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('))');
}
}
}
示例7: compile
public function compile(Twig_Compiler $compiler)
{
$function = $compiler->getEnvironment()->getFunction($this->getNode('name')->getAttribute('name'));
if (!$function) {
throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getNode('name')->getAttribute('name')), $this->getLine());
}
$compiler
->raw($function->compile().'(')
->raw($function->needsEnvironment() ? '$this->env, ' : '')
->raw($function->needsContext() ? '$context, ' : '')
;
$first = true;
foreach ($this->getNode('arguments') as $node) {
if (!$first) {
$compiler->raw(', ');
} else {
$first = false;
}
$compiler->subcompile($node);
}
$compiler->raw(')');
}
示例8: compile
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(')');
}
示例9: compile
public function compile(Twig_Compiler $compiler)
{
$testMap = $compiler->getEnvironment()->getTests();
if (!isset($testMap[$this->getAttribute('name')])) {
throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
}
$name = $this->getAttribute('name');
$node = $this->getNode('node');
// defined is a special case
if ('defined' === $name) {
$compiler->subcompile($node);
return;
}
$compiler->raw($testMap[$name]->compile() . '(')->subcompile($node);
if (null !== $this->getNode('arguments')) {
$compiler->raw(', ');
$max = count($this->getNode('arguments')) - 1;
foreach ($this->getNode('arguments') as $i => $arg) {
$compiler->subcompile($arg);
if ($i != $max) {
$compiler->raw(', ');
}
}
}
$compiler->raw(')');
}
示例10: compile
public function compile(Twig_Compiler $compiler)
{
$testMap = $compiler->getEnvironment()->getTests();
if (!isset($testMap[$this->getAttribute('name')])) {
throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
}
// defined is a special case
if ('defined' === $this->getAttribute('name')) {
if ($this->getNode('node') instanceof Twig_Node_Expression_Name) {
$compiler->raw($testMap['defined']->compile() . '(')->repr($this->getNode('node')->getAttribute('name'))->raw(', $context)');
} elseif ($this->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
$this->getNode('node')->setAttribute('is_defined_test', true);
$compiler->raw('(null !== ')->subcompile($this->getNode('node'))->raw(')');
} else {
throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
}
return;
}
$compiler->raw($testMap[$this->getAttribute('name')]->compile() . '(')->subcompile($this->getNode('node'));
if (null !== $this->getNode('arguments')) {
$compiler->raw(', ');
$max = count($this->getNode('arguments')) - 1;
foreach ($this->getNode('arguments') as $i => $node) {
$compiler->subcompile($node);
if ($i != $max) {
$compiler->raw(', ');
}
}
}
$compiler->raw(')');
}
示例11: compile
public function compile(Twig_Compiler $compiler)
{
$name = $this->getNode('filter')->getAttribute('value');
if (false === ($filter = $compiler->getEnvironment()->getFilter($name))) {
throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine());
}
$this->compileFilter($compiler, $filter);
}
示例12: compile
public function compile(Twig_Compiler $compiler)
{
$name = $this->getAttribute('name');
$test = $compiler->getEnvironment()->getTest($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'test');
$this->setAttribute('callable', $test->getCallable());
$this->compileCallable($compiler);
}
示例13: compile
/**
* Compile the node.
*
* @param \Twig_Compiler $compiler A Twig compiler instance.
*/
public function compile(\Twig_Compiler $compiler)
{
$name = $this->getAttribute('name');
$shortcode = $compiler->getEnvironment()->getShortcode($name);
$filter = $compiler->getEnvironment()->getShortcodeFilter($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'shortcode');
$this->setAttribute('thing', $shortcode);
$this->setAttribute('needs_environment', $shortcode->needsEnvironment());
$this->setAttribute('needs_context', $shortcode->needsContext());
$this->setAttribute('arguments', $shortcode->getArguments());
if ($shortcode instanceof \Twig_FunctionCallableInterface || $shortcode instanceof GenericShortcode) {
$instance = ShortcodesTrait::getShortcodesClass();
$this->setAttribute('callable', $shortcode->getCallable());
}
if ($this->hasNode('node')) {
$body = $this->getNode('node');
if (!is_array($body)) {
$body = [$body];
}
$compiler->addDebugInfo($this)->write("ob_start();\n");
foreach ($body as $key => $node) {
$compiler->subcompile($node);
}
$compiler->write("\$body = ob_get_clean();\n");
}
if ($this->hasNode('arguments') && null !== $this->getNode('arguments')) {
$compiler->write("\$arguments = array(");
foreach ($this->getNode('arguments') as $key => $node) {
$compiler->string($key)->raw(" => ")->subcompile($node)->raw(", ");
}
$compiler->raw(");\n");
}
$compiler->write('$compiled = $context["__shortcodes"](')->string($this->tag)->raw(", \$body, \$arguments);\n")->write($filter ? '$compiled = ' : 'echo ');
$this->compileCallable($compiler);
$compiler->raw(";\n");
// Filter shortcode, if registered filters are present
if ($filter) {
$compiler->write('echo $context["__shortcodes_filter"](')->string($name)->raw(', $compiled, $context, $this->env)');
}
$compiler->raw(";\n")->write('unset($body, $arguments, $compiled);')->raw("\n");
}
示例14: compile
public function compile(Twig_Compiler $compiler)
{
$name = $this->getAttribute('name');
$test = $compiler->getEnvironment()->getTest($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'Test');
$this->setAttribute('thing', $test);
if ($test instanceof Twig_TestCallableInterface || $test instanceof Twig_SimpleTest) {
$this->setAttribute('callable', $test->getCallable());
}
$this->compileCallable($compiler);
}
示例15: compile
public function compile(Twig_Compiler $compiler)
{
$name = $this->getNode('filter')->getAttribute('value');
$filter = $compiler->getEnvironment()->getFilter($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'filter');
$this->setAttribute('needs_environment', $filter->needsEnvironment());
$this->setAttribute('needs_context', $filter->needsContext());
$this->setAttribute('arguments', $filter->getArguments());
$this->setAttribute('callable', $filter->getCallable());
$this->compileCallable($compiler);
}