diff --git a/extra/intl-extra/IntlExtension.php b/extra/intl-extra/IntlExtension.php index 1e632a3a9..e87fd086e 100644 --- a/extra/intl-extra/IntlExtension.php +++ b/extra/intl-extra/IntlExtension.php @@ -369,7 +369,7 @@ public function formatNumberStyle(string $style, $number, array $attrs = [], str */ 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) { diff --git a/extra/intl-extra/composer.json b/extra/intl-extra/composer.json index a46eada31..b3c3ceff3 100644 --- a/extra/intl-extra/composer.json +++ b/extra/intl-extra/composer.json @@ -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": { diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index c193a7f0e..79e6cd763 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -185,11 +185,11 @@ public function getFilters(): array { 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 @@ public function getFunctions(): array 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 @@ public static function random(string $charset, $values = null, $max = null) * * @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 @@ public static function formatDate(Environment $env, $date, $format = null, $time return $date->format($format); } - return self::convertDate($env, $date, $timezone)->format($format); + return $this->convertDate($date, $timezone)->format($format); } /** @@ -442,9 +442,9 @@ public static function formatDate(Environment $env, $date, $format = null, $time * * @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 @@ public static function sprintf($format, ...$values): string 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 @@ public static function sprintf($format, ...$values): string * * @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 @@ public static function convertDate(Environment $env, $date = null, $timezone = n $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 @@ public static function round($value, $precision = 0, $method = 'common') } /** - * 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 @@ public static function round($value, $precision = 0, $method = 'common') * * @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]; } diff --git a/src/Resources/core.php b/src/Resources/core.php index 0d4cfc919..18a441fae 100644 --- a/src/Resources/core.php +++ b/src/Resources/core.php @@ -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); } /** diff --git a/tests/Node/Expression/FilterTest.php b/tests/Node/Expression/FilterTest.php index e28dd73d8..f99d6242f 100644 --- a/tests/Node/Expression/FilterTest.php +++ b/tests/Node/Expression/FilterTest.php @@ -69,7 +69,7 @@ protected function foobar() $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 @@ protected function foobar() '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); diff --git a/tests/Node/Expression/FunctionTest.php b/tests/Node/Expression/FunctionTest.php index aeabfc3c6..aa40e3555 100644 --- a/tests/Node/Expression/FunctionTest.php +++ b/tests/Node/Expression/FunctionTest.php @@ -76,7 +76,7 @@ public function getTests() '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');