Skip to content

Commit

Permalink
Move some static extension methods to non-static
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 1, 2024
1 parent 71fafb6 commit 283475b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion extra/intl-extra/IntlExtension.php
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion extra/intl-extra/composer.json
Expand Up @@ -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": {
Expand Down
40 changes: 24 additions & 16 deletions src/Extension/CoreExtension.php
Expand Up @@ -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']),

Expand Down Expand Up @@ -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']]),
];
Expand Down Expand Up @@ -416,18 +416,18 @@ 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];
}

if ($date instanceof \DateInterval) {
return $date->format($format);
}

return self::convertDate($env, $date, $timezone)->format($format);
return $this->convertDate($date, $timezone)->format($format);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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.
*
Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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];
}
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/core.php
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Node/Expression/FilterTest.php
Expand Up @@ -69,22 +69,22 @@ 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);
$node = $this->createFilter($date, 'date', [
'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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Node/Expression/FunctionTest.php
Expand Up @@ -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');
Expand Down

0 comments on commit 283475b

Please sign in to comment.