mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-10 09:26:29 +00:00
minor #4044 Move some static extension methods to non-static (fabpot)
This PR was merged into the 3.x branch.
Discussion
----------
Move some static extension methods to non-static
A first step to resolve #4040
Commits
-------
283475b1 Move some static extension methods to non-static
This commit is contained in:
@@ -369,7 +369,7 @@ final class IntlExtension extends AbstractExtension
|
||||
*/
|
||||
public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): string
|
||||
{
|
||||
$date = CoreExtension::convertDate($env, $date, $timezone);
|
||||
$date = $env->getExtension(CoreExtension::class)->convertDate($date, $timezone);
|
||||
|
||||
$formatterTimezone = $timezone;
|
||||
if (null === $formatterTimezone || false === $formatterTimezone) {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.2.5",
|
||||
"twig/twig": "^3.9",
|
||||
"twig/twig": "^3.10",
|
||||
"symfony/intl": "^5.4|^6.4|^7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
@@ -185,11 +185,11 @@ final class CoreExtension extends AbstractExtension
|
||||
{
|
||||
return [
|
||||
// formatting filters
|
||||
new TwigFilter('date', [self::class, 'formatDate'], ['needs_environment' => true]),
|
||||
new TwigFilter('date_modify', [self::class, 'modifyDate'], ['needs_environment' => true]),
|
||||
new TwigFilter('date', [$this, 'formatDate']),
|
||||
new TwigFilter('date_modify', [$this, 'modifyDate']),
|
||||
new TwigFilter('format', [self::class, 'sprintf']),
|
||||
new TwigFilter('replace', [self::class, 'replace']),
|
||||
new TwigFilter('number_format', [self::class, 'formatNumber'], ['needs_environment' => true]),
|
||||
new TwigFilter('number_format', [$this, 'formatNumber']),
|
||||
new TwigFilter('abs', 'abs'),
|
||||
new TwigFilter('round', [self::class, 'round']),
|
||||
|
||||
@@ -241,7 +241,7 @@ final class CoreExtension extends AbstractExtension
|
||||
new TwigFunction('constant', [self::class, 'constant']),
|
||||
new TwigFunction('cycle', [self::class, 'cycle']),
|
||||
new TwigFunction('random', [self::class, 'random'], ['needs_charset' => true]),
|
||||
new TwigFunction('date', [self::class, 'convertDate'], ['needs_environment' => true]),
|
||||
new TwigFunction('date', [$this, 'convertDate']),
|
||||
new TwigFunction('include', [self::class, 'include'], ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['all']]),
|
||||
new TwigFunction('source', [self::class, 'source'], ['needs_environment' => true, 'is_safe' => ['all']]),
|
||||
];
|
||||
@@ -416,10 +416,10 @@ final class CoreExtension extends AbstractExtension
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function formatDate(Environment $env, $date, $format = null, $timezone = null): string
|
||||
public function formatDate($date, $format = null, $timezone = null): string
|
||||
{
|
||||
if (null === $format) {
|
||||
$formats = $env->getExtension(self::class)->getDateFormat();
|
||||
$formats = $this->getDateFormat();
|
||||
$format = $date instanceof \DateInterval ? $formats[1] : $formats[0];
|
||||
}
|
||||
|
||||
@@ -427,7 +427,7 @@ final class CoreExtension extends AbstractExtension
|
||||
return $date->format($format);
|
||||
}
|
||||
|
||||
return self::convertDate($env, $date, $timezone)->format($format);
|
||||
return $this->convertDate($date, $timezone)->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,9 +442,9 @@ final class CoreExtension extends AbstractExtension
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function modifyDate(Environment $env, $date, $modifier)
|
||||
public function modifyDate($date, $modifier)
|
||||
{
|
||||
return self::convertDate($env, $date, false)->modify($modifier);
|
||||
return $this->convertDate($date, false)->modify($modifier);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -460,6 +460,14 @@ final class CoreExtension extends AbstractExtension
|
||||
return sprintf($format ?? '', ...$values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public static function dateConverter(Environment $env, $date, $format = null, $timezone = null): string
|
||||
{
|
||||
return $env->getExtension(CoreExtension::class)->formatDate($date, $format, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an input to a \DateTime instance.
|
||||
*
|
||||
@@ -474,12 +482,12 @@ final class CoreExtension extends AbstractExtension
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function convertDate(Environment $env, $date = null, $timezone = null)
|
||||
public function convertDate($date = null, $timezone = null)
|
||||
{
|
||||
// determine the timezone
|
||||
if (false !== $timezone) {
|
||||
if (null === $timezone) {
|
||||
$timezone = $env->getExtension(self::class)->getTimezone();
|
||||
$timezone = $this->getTimezone();
|
||||
} elseif (!$timezone instanceof \DateTimeZone) {
|
||||
$timezone = new \DateTimeZone($timezone);
|
||||
}
|
||||
@@ -504,14 +512,14 @@ final class CoreExtension extends AbstractExtension
|
||||
$date = 'now';
|
||||
}
|
||||
|
||||
return new \DateTime($date, false !== $timezone ? $timezone : $env->getExtension(self::class)->getTimezone());
|
||||
return new \DateTime($date, false !== $timezone ? $timezone : $this->getTimezone());
|
||||
}
|
||||
|
||||
$asString = (string) $date;
|
||||
if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) {
|
||||
$date = new \DateTime('@'.$date);
|
||||
} else {
|
||||
$date = new \DateTime($date, $env->getExtension(self::class)->getTimezone());
|
||||
$date = new \DateTime($date, $this->getTimezone());
|
||||
}
|
||||
|
||||
if (false !== $timezone) {
|
||||
@@ -565,7 +573,7 @@ final class CoreExtension extends AbstractExtension
|
||||
}
|
||||
|
||||
/**
|
||||
* Number format filter.
|
||||
* Formats a number.
|
||||
*
|
||||
* All of the formatting options can be left null, in that case the defaults will
|
||||
* be used. Supplying any of the parameters will override the defaults set in the
|
||||
@@ -578,9 +586,9 @@ final class CoreExtension extends AbstractExtension
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function formatNumber(Environment $env, $number, $decimal = null, $decimalPoint = null, $thousandSep = null): string
|
||||
public function formatNumber($number, $decimal = null, $decimalPoint = null, $thousandSep = null): string
|
||||
{
|
||||
$defaults = $env->getExtension(self::class)->getNumberFormat();
|
||||
$defaults = $this->getNumberFormat();
|
||||
if (null === $decimal) {
|
||||
$decimal = $defaults[0];
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ function twig_date_format_filter(Environment $env, $date, $format = null, $timez
|
||||
{
|
||||
trigger_deprecation('twig/twig', '3.9', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
|
||||
|
||||
return CoreExtension::formatDate($env, $date, $format, $timezone);
|
||||
return $env->getExtension(CoreExtension::class)->formatDate($date, $format, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,7 +53,7 @@ function twig_date_modify_filter(Environment $env, $date, $modifier)
|
||||
{
|
||||
trigger_deprecation('twig/twig', '3.9', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
|
||||
|
||||
return CoreExtension::modifyDate($env, $date, $modifier);
|
||||
return $env->getExtension(CoreExtension::class)->modifyDate($date, $modifier);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,7 +75,7 @@ function twig_date_converter(Environment $env, $date = null, $timezone = null)
|
||||
{
|
||||
trigger_deprecation('twig/twig', '3.9', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
|
||||
|
||||
return CoreExtension::convertDate($env, $date, $timezone);
|
||||
return $env->getExtension(CoreExtension::class)->convertDate($date, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +108,7 @@ function twig_number_format_filter(Environment $env, $number, $decimal = null, $
|
||||
{
|
||||
trigger_deprecation('twig/twig', '3.9', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
|
||||
|
||||
return CoreExtension::formatNumber($env, $number, $decimal, $decimalPoint, $thousandSep);
|
||||
return $env->getExtension(CoreExtension::class)->formatNumber($number, $decimal, $decimalPoint, $thousandSep);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class FilterTest extends NodeTestCase
|
||||
$node = $this->createFilter($expr, 'upper');
|
||||
$node = $this->createFilter($node, 'number_format', [new ConstantExpression(2, 1), new ConstantExpression('.', 1), new ConstantExpression(',', 1)]);
|
||||
|
||||
$tests[] = [$node, 'Twig\Extension\CoreExtension::formatNumber($this->env, Twig\Extension\CoreExtension::upper($this->env->getCharset(), "foo"), 2, ".", ",")'];
|
||||
$tests[] = [$node, '$this->extensions[\'Twig\Extension\CoreExtension\']->formatNumber(Twig\Extension\CoreExtension::upper($this->env->getCharset(), "foo"), 2, ".", ",")'];
|
||||
|
||||
// named arguments
|
||||
$date = new ConstantExpression(0, 1);
|
||||
@@ -77,14 +77,14 @@ class FilterTest extends NodeTestCase
|
||||
'timezone' => new ConstantExpression('America/Chicago', 1),
|
||||
'format' => new ConstantExpression('d/m/Y H:i:s P', 1),
|
||||
]);
|
||||
$tests[] = [$node, 'Twig\Extension\CoreExtension::formatDate($this->env, 0, "d/m/Y H:i:s P", "America/Chicago")'];
|
||||
$tests[] = [$node, '$this->extensions[\'Twig\Extension\CoreExtension\']->formatDate(0, "d/m/Y H:i:s P", "America/Chicago")'];
|
||||
|
||||
// skip an optional argument
|
||||
$date = new ConstantExpression(0, 1);
|
||||
$node = $this->createFilter($date, 'date', [
|
||||
'timezone' => new ConstantExpression('America/Chicago', 1),
|
||||
]);
|
||||
$tests[] = [$node, 'Twig\Extension\CoreExtension::formatDate($this->env, 0, null, "America/Chicago")'];
|
||||
$tests[] = [$node, '$this->extensions[\'Twig\Extension\CoreExtension\']->formatDate(0, null, "America/Chicago")'];
|
||||
|
||||
// underscores vs camelCase for named arguments
|
||||
$string = new ConstantExpression('abc', 1);
|
||||
|
||||
@@ -76,7 +76,7 @@ class FunctionTest extends NodeTestCase
|
||||
'timezone' => new ConstantExpression('America/Chicago', 1),
|
||||
'date' => new ConstantExpression(0, 1),
|
||||
]);
|
||||
$tests[] = [$node, 'Twig\Extension\CoreExtension::convertDate($this->env, 0, "America/Chicago")'];
|
||||
$tests[] = [$node, '$this->extensions[\'Twig\Extension\CoreExtension\']->convertDate(0, "America/Chicago")'];
|
||||
|
||||
// arbitrary named arguments
|
||||
$node = $this->createFunction('barbar');
|
||||
|
||||
Reference in New Issue
Block a user