本文整理汇总了PHP中Twig_Environment::getFilters方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::getFilters方法的具体用法?PHP Twig_Environment::getFilters怎么用?PHP Twig_Environment::getFilters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::getFilters方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: leaveNode
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
{
if ($node instanceof Twig_Node_Expression_Constant) {
// constants are marked safe for all
$this->setSafe($node, array('all'));
} elseif ($node instanceof Twig_Node_Expression_Conditional) {
// instersect safeness of both operands
$safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
$this->setSafe($node, $safe);
} elseif ($node instanceof Twig_Node_Expression_Filter) {
// filter expression is safe when the filter is safe
$filterMap = $env->getFilters();
$name = $node->getNode('filter')->getAttribute('value');
$args = $node->getNode('arguments');
if (isset($filterMap[$name])) {
$this->setSafe($node, $filterMap[$name]->getSafe($args));
} else {
$this->setSafe($node, array());
}
} elseif ($node instanceof Twig_Node_Expression_Function) {
// function expression is safe when the function is safe
$name = $node->getNode('name')->getAttribute('name');
$args = $node->getNode('arguments');
$function = $env->getFunction($name);
if (null !== $function) {
$this->setSafe($node, $function->getSafe($args));
} else {
$this->setSafe($node, array());
}
} else {
$this->setSafe($node, array());
}
return $node;
}
示例2: listFilters
/**
* Get all filters
*
* @return \Twig_FilterInterface[]
*/
public function listFilters() : array
{
/**
* If Twig gives us a way to read this information, we'll use that
* instead.
*/
/** @noinspection PhpInternalEntityUsedInspection */
$filters = $this->twigEnv->getFilters();
return \array_keys($filters);
}
示例3: testRemoveExtension
public function testRemoveExtension()
{
$twig = new Twig_Environment(new Twig_Loader_String());
$twig->addExtension(new Twig_Tests_EnvironmentTest_Extension());
$twig->removeExtension('test');
$this->assertFalse(array_key_exists('test', $twig->getTags()));
$this->assertFalse(array_key_exists('foo_filter', $twig->getFilters()));
$this->assertFalse(array_key_exists('foo_function', $twig->getFunctions()));
$this->assertFalse(array_key_exists('foo_test', $twig->getTests()));
$this->assertFalse(array_key_exists('foo_unary', $twig->getUnaryOperators()));
$this->assertFalse(array_key_exists('foo_binary', $twig->getBinaryOperators()));
$this->assertFalse(array_key_exists('foo_global', $twig->getGlobals()));
$this->assertCount(2, $twig->getNodeVisitors());
}
示例4: escapeNode
protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type)
{
if (false === $type) {
return $node;
}
$expression = $node instanceof Twig_Node_Print ? $node->expr : $node;
if ($expression instanceof Twig_Node_Expression_Filter) {
// don't escape if the primary node of the filter is not a variable
if (!$expression->node instanceof Twig_Node_Expression_GetAttr && !$expression->node instanceof Twig_Node_Expression_Name) {
return $node;
}
// don't escape if there is already an "escaper" in the filter chain
$filterMap = $env->getFilters();
for ($i = 0; $i < count($expression->filters); $i += 2) {
$name = $expression->filters->{$i}['value'];
if (isset($filterMap[$name]) && $filterMap[$name]->isEscaper()) {
return $node;
}
}
} elseif (!$expression instanceof Twig_Node_Expression_GetAttr && !$expression instanceof Twig_Node_Expression_Name) {
// don't escape if the node is not a variable
return $node;
}
// escape
if ($expression instanceof Twig_Node_Expression_Filter) {
// escape all variables in filters arguments
for ($i = 0; $i < count($expression->filters); $i += 2) {
foreach ($expression->filters->{$i + 1} as $j => $n) {
$expression->filters->{$i + 1}->{$j} = $this->escapeNode($n, $env, $type);
}
}
$filter = $this->getEscaperFilter($type, $expression->getLine());
$expression->prependFilter($filter[0], $filter[1]);
return $node;
} elseif ($node instanceof Twig_Node_Print) {
return new Twig_Node_Print(
new Twig_Node_Expression_Filter($expression, new Twig_Node($this->getEscaperFilter($type, $node->getLine())), $node->getLine())
, $node->getLine()
);
} else {
return new Twig_Node_Expression_Filter($node, new Twig_Node($this->getEscaperFilter($type, $node->getLine())), $node->getLine());
}
}
示例5: preEscapeFilterNode
protected function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env)
{
$filterMap = $env->getFilters();
$name = $filter->getNode('filter')->getAttribute('value');
if (isset($filterMap[$name])) {
$type = $filterMap[$name]->getPreEscape();
if (null === $type) {
return $filter;
}
$node = $filter->getNode('node');
if ($this->isSafeFor($type, $node, $env)) {
return $filter;
}
$filter->setNode('node', $this->getEscaperFilter($type, $node));
return $filter;
}
return $filter;
}
示例6: escapeNode
protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type)
{
if (false === $type) {
return $node;
}
$expression = $node instanceof Twig_Node_Print ? $node->getExpression() : $node;
if ($expression instanceof Twig_Node_Expression_Filter) {
// don't escape if the primary node of the filter is not a variable
$nodes = $expression->getNodes();
if (!$nodes[0] instanceof Twig_Node_Expression_Name) {
return $node;
}
// don't escape if there is already an "escaper" in the filter chain
$filterMap = $env->getFilters();
foreach ($expression->getFilters() as $filter) {
if (isset($filterMap[$filter[0]]) && $filterMap[$filter[0]]->isEscaper()) {
return $node;
}
}
} elseif (!$expression instanceof Twig_Node_Expression_GetAttr && !$expression instanceof Twig_Node_Expression_Name) {
// don't escape if the node is not a variable
return $node;
}
// escape
if ($expression instanceof Twig_Node_Expression_Filter) {
// escape all variables in filters arguments
$filters = $expression->getFilters();
foreach ($filters as $i => $filter) {
foreach ($filter[1] as $j => $argument) {
$filters[$i][1][$j] = $this->escapeNode($argument, $env, $type);
}
}
$expression->setFilters($filters);
$expression->prependFilter($this->getEscaperFilter($type));
return $node;
} elseif ($node instanceof Twig_Node_Print) {
return new Twig_Node_Print(new Twig_Node_Expression_Filter($expression, array($this->getEscaperFilter($type)), $node->getLine()), $node->getLine());
} else {
return new Twig_Node_Expression_Filter($node, array($this->getEscaperFilter($type)), $node->getLine());
}
}
示例7: testAddExtension
public function testAddExtension()
{
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->addExtension(new Twig_Tests_EnvironmentTest_Extension());
$this->assertArrayHasKey('test', $twig->getTags());
$this->assertArrayHasKey('foo_filter', $twig->getFilters());
$this->assertArrayHasKey('foo_function', $twig->getFunctions());
$this->assertArrayHasKey('foo_test', $twig->getTests());
$this->assertArrayHasKey('foo_unary', $twig->getUnaryOperators());
$this->assertArrayHasKey('foo_binary', $twig->getBinaryOperators());
$this->assertArrayHasKey('foo_global', $twig->getGlobals());
$visitors = $twig->getNodeVisitors();
$this->assertEquals('Twig_Tests_EnvironmentTest_NodeVisitor', get_class($visitors[2]));
}
示例8: testRemoveExtension
/**
* @group legacy
*/
public function testRemoveExtension()
{
$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
$twig->addExtension(new Twig_Tests_EnvironmentTest_Extension_WithDeprecatedName());
$twig->removeExtension('environment_test');
$this->assertFalse(array_key_exists('test', $twig->getTags()));
$this->assertFalse(array_key_exists('foo_filter', $twig->getFilters()));
$this->assertFalse(array_key_exists('foo_function', $twig->getFunctions()));
$this->assertFalse(array_key_exists('foo_test', $twig->getTests()));
$this->assertFalse(array_key_exists('foo_unary', $twig->getUnaryOperators()));
$this->assertFalse(array_key_exists('foo_binary', $twig->getBinaryOperators()));
$this->assertFalse(array_key_exists('foo_global', $twig->getGlobals()));
$this->assertCount(2, $twig->getNodeVisitors());
}