mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-31 12:37:15 +00:00
Add the notion of a Twig callable
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# 3.12.0 (2024-XX-XX)
|
||||
|
||||
* Add the notion of Twig callables (functions, filters, and tests)
|
||||
* Bump minimum PHP version to 8.0
|
||||
* Fix integration tests when a test has more than on data/expect section and deprecations
|
||||
* Add the `enum_cases` function
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
abstract class AbstractTwigCallable implements TwigCallableInterface
|
||||
{
|
||||
protected $options;
|
||||
|
||||
private $name;
|
||||
private $callable;
|
||||
private $arguments;
|
||||
|
||||
public function __construct(string $name, $callable = null, array $options = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->callable = $callable;
|
||||
$this->arguments = [];
|
||||
$this->options = array_merge([
|
||||
'needs_environment' => false,
|
||||
'needs_context' => false,
|
||||
'needs_charset' => false,
|
||||
'is_variadic' => false,
|
||||
'deprecated' => false,
|
||||
'deprecating_package' => '',
|
||||
'alternative' => null,
|
||||
], $options);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getCallable()
|
||||
{
|
||||
return $this->callable;
|
||||
}
|
||||
|
||||
public function getNodeClass(): string
|
||||
{
|
||||
return $this->options['node_class'];
|
||||
}
|
||||
|
||||
public function needsCharset(): bool
|
||||
{
|
||||
return $this->options['needs_charset'];
|
||||
}
|
||||
|
||||
public function needsEnvironment(): bool
|
||||
{
|
||||
return $this->options['needs_environment'];
|
||||
}
|
||||
|
||||
public function needsContext(): bool
|
||||
{
|
||||
return $this->options['needs_context'];
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments): void
|
||||
{
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
{
|
||||
return $this->arguments;
|
||||
}
|
||||
|
||||
public function isVariadic(): bool
|
||||
{
|
||||
return $this->options['is_variadic'];
|
||||
}
|
||||
|
||||
public function isDeprecated(): bool
|
||||
{
|
||||
return (bool) $this->options['deprecated'];
|
||||
}
|
||||
|
||||
public function getDeprecatingPackage(): string
|
||||
{
|
||||
return $this->options['deprecating_package'];
|
||||
}
|
||||
|
||||
public function getDeprecatedVersion(): string
|
||||
{
|
||||
return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated'];
|
||||
}
|
||||
|
||||
public function getAlternative(): ?string
|
||||
{
|
||||
return $this->options['alternative'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
interface TwigCallableInterface
|
||||
{
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* @return callable|array{class-string, string}|null
|
||||
*/
|
||||
public function getCallable();
|
||||
|
||||
public function getNodeClass(): string;
|
||||
|
||||
public function needsCharset(): bool;
|
||||
|
||||
public function needsEnvironment(): bool;
|
||||
|
||||
public function needsContext(): bool;
|
||||
|
||||
public function setArguments(array $arguments): void;
|
||||
|
||||
public function getArguments(): array;
|
||||
|
||||
public function isVariadic(): bool;
|
||||
|
||||
public function isDeprecated(): bool;
|
||||
|
||||
public function getDeprecatingPackage(): string;
|
||||
|
||||
public function getDeprecatedVersion(): string;
|
||||
|
||||
public function getAlternative(): ?string;
|
||||
}
|
||||
+4
-86
@@ -21,79 +21,22 @@ use Twig\Node\Node;
|
||||
*
|
||||
* @see https://twig.symfony.com/doc/templates.html#filters
|
||||
*/
|
||||
final class TwigFilter
|
||||
final class TwigFilter extends AbstractTwigCallable
|
||||
{
|
||||
private $name;
|
||||
private $callable;
|
||||
private $options;
|
||||
private $arguments = [];
|
||||
|
||||
/**
|
||||
* @param callable|array{class-string, string}|null $callable A callable implementing the filter. If null, you need to overwrite the "node_class" option to customize compilation.
|
||||
*/
|
||||
public function __construct(string $name, $callable = null, array $options = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->callable = $callable;
|
||||
parent::__construct($name, $callable, $options);
|
||||
|
||||
$this->options = array_merge([
|
||||
'needs_environment' => false,
|
||||
'needs_context' => false,
|
||||
'needs_charset' => false,
|
||||
'is_variadic' => false,
|
||||
'is_safe' => null,
|
||||
'is_safe_callback' => null,
|
||||
'pre_escape' => null,
|
||||
'preserves_safety' => null,
|
||||
'node_class' => FilterExpression::class,
|
||||
'deprecated' => false,
|
||||
'deprecating_package' => '',
|
||||
'alternative' => null,
|
||||
], $options);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the callable to execute for this filter.
|
||||
*
|
||||
* @return callable|array{class-string, string}|null
|
||||
*/
|
||||
public function getCallable()
|
||||
{
|
||||
return $this->callable;
|
||||
}
|
||||
|
||||
public function getNodeClass(): string
|
||||
{
|
||||
return $this->options['node_class'];
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments): void
|
||||
{
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
{
|
||||
return $this->arguments;
|
||||
}
|
||||
|
||||
public function needsCharset(): bool
|
||||
{
|
||||
return $this->options['needs_charset'];
|
||||
}
|
||||
|
||||
public function needsEnvironment(): bool
|
||||
{
|
||||
return $this->options['needs_environment'];
|
||||
}
|
||||
|
||||
public function needsContext(): bool
|
||||
{
|
||||
return $this->options['needs_context'];
|
||||
], $this->options);
|
||||
}
|
||||
|
||||
public function getSafe(Node $filterArgs): ?array
|
||||
@@ -118,29 +61,4 @@ final class TwigFilter
|
||||
{
|
||||
return $this->options['pre_escape'];
|
||||
}
|
||||
|
||||
public function isVariadic(): bool
|
||||
{
|
||||
return $this->options['is_variadic'];
|
||||
}
|
||||
|
||||
public function isDeprecated(): bool
|
||||
{
|
||||
return (bool) $this->options['deprecated'];
|
||||
}
|
||||
|
||||
public function getDeprecatingPackage(): string
|
||||
{
|
||||
return $this->options['deprecating_package'];
|
||||
}
|
||||
|
||||
public function getDeprecatedVersion(): string
|
||||
{
|
||||
return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated'];
|
||||
}
|
||||
|
||||
public function getAlternative(): ?string
|
||||
{
|
||||
return $this->options['alternative'];
|
||||
}
|
||||
}
|
||||
|
||||
+4
-86
@@ -21,77 +21,20 @@ use Twig\Node\Node;
|
||||
*
|
||||
* @see https://twig.symfony.com/doc/templates.html#functions
|
||||
*/
|
||||
final class TwigFunction
|
||||
final class TwigFunction extends AbstractTwigCallable
|
||||
{
|
||||
private $name;
|
||||
private $callable;
|
||||
private $options;
|
||||
private $arguments = [];
|
||||
|
||||
/**
|
||||
* @param callable|array{class-string, string}|null $callable A callable implementing the function. If null, you need to overwrite the "node_class" option to customize compilation.
|
||||
*/
|
||||
public function __construct(string $name, $callable = null, array $options = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->callable = $callable;
|
||||
parent::__construct($name, $callable, $options);
|
||||
|
||||
$this->options = array_merge([
|
||||
'needs_environment' => false,
|
||||
'needs_context' => false,
|
||||
'needs_charset' => false,
|
||||
'is_variadic' => false,
|
||||
'is_safe' => null,
|
||||
'is_safe_callback' => null,
|
||||
'node_class' => FunctionExpression::class,
|
||||
'deprecated' => false,
|
||||
'deprecating_package' => '',
|
||||
'alternative' => null,
|
||||
], $options);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the callable to execute for this function.
|
||||
*
|
||||
* @return callable|array{class-string, string}|null
|
||||
*/
|
||||
public function getCallable()
|
||||
{
|
||||
return $this->callable;
|
||||
}
|
||||
|
||||
public function getNodeClass(): string
|
||||
{
|
||||
return $this->options['node_class'];
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments): void
|
||||
{
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
{
|
||||
return $this->arguments;
|
||||
}
|
||||
|
||||
public function needsCharset(): bool
|
||||
{
|
||||
return $this->options['needs_charset'];
|
||||
}
|
||||
|
||||
public function needsEnvironment(): bool
|
||||
{
|
||||
return $this->options['needs_environment'];
|
||||
}
|
||||
|
||||
public function needsContext(): bool
|
||||
{
|
||||
return $this->options['needs_context'];
|
||||
], $this->options);
|
||||
}
|
||||
|
||||
public function getSafe(Node $functionArgs): ?array
|
||||
@@ -106,29 +49,4 @@ final class TwigFunction
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function isVariadic(): bool
|
||||
{
|
||||
return (bool) $this->options['is_variadic'];
|
||||
}
|
||||
|
||||
public function isDeprecated(): bool
|
||||
{
|
||||
return (bool) $this->options['deprecated'];
|
||||
}
|
||||
|
||||
public function getDeprecatingPackage(): string
|
||||
{
|
||||
return $this->options['deprecating_package'];
|
||||
}
|
||||
|
||||
public function getDeprecatedVersion(): string
|
||||
{
|
||||
return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated'];
|
||||
}
|
||||
|
||||
public function getAlternative(): ?string
|
||||
{
|
||||
return $this->options['alternative'];
|
||||
}
|
||||
}
|
||||
|
||||
+10
-59
@@ -20,83 +20,34 @@ use Twig\Node\Expression\TestExpression;
|
||||
*
|
||||
* @see https://twig.symfony.com/doc/templates.html#test-operator
|
||||
*/
|
||||
final class TwigTest
|
||||
final class TwigTest extends AbstractTwigCallable
|
||||
{
|
||||
private $name;
|
||||
private $callable;
|
||||
private $options;
|
||||
private $arguments = [];
|
||||
|
||||
/**
|
||||
* @param callable|array{class-string, string}|null $callable A callable implementing the test. If null, you need to overwrite the "node_class" option to customize compilation.
|
||||
*/
|
||||
public function __construct(string $name, $callable = null, array $options = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->callable = $callable;
|
||||
parent::__construct($name, $callable, $options);
|
||||
|
||||
$this->options = array_merge([
|
||||
'is_variadic' => false,
|
||||
'node_class' => TestExpression::class,
|
||||
'deprecated' => false,
|
||||
'deprecating_package' => '',
|
||||
'alternative' => null,
|
||||
'one_mandatory_argument' => false,
|
||||
], $options);
|
||||
], $this->options);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
public function needsCharset(): bool
|
||||
{
|
||||
return $this->name;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the callable to execute for this test.
|
||||
*
|
||||
* @return callable|array{class-string, string}|null
|
||||
*/
|
||||
public function getCallable()
|
||||
public function needsEnvironment(): bool
|
||||
{
|
||||
return $this->callable;
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getNodeClass(): string
|
||||
public function needsContext(): bool
|
||||
{
|
||||
return $this->options['node_class'];
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments): void
|
||||
{
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
{
|
||||
return $this->arguments;
|
||||
}
|
||||
|
||||
public function isVariadic(): bool
|
||||
{
|
||||
return (bool) $this->options['is_variadic'];
|
||||
}
|
||||
|
||||
public function isDeprecated(): bool
|
||||
{
|
||||
return (bool) $this->options['deprecated'];
|
||||
}
|
||||
|
||||
public function getDeprecatingPackage(): string
|
||||
{
|
||||
return $this->options['deprecating_package'];
|
||||
}
|
||||
|
||||
public function getDeprecatedVersion(): string
|
||||
{
|
||||
return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated'];
|
||||
}
|
||||
|
||||
public function getAlternative(): ?string
|
||||
{
|
||||
return $this->options['alternative'];
|
||||
return false;
|
||||
}
|
||||
|
||||
public function hasOneMandatoryArgument(): bool
|
||||
|
||||
Reference in New Issue
Block a user