8000 [DI] use filter_var() instead of XmlUtils::phpize() in EnvVarProcessor by nicolas-grekas · Pull Request #29042 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] use filter_var() instead of XmlUtils::phpize() in EnvVarProcessor #29042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\DependencyInjection;

use Symfony\Component\Config\Util\XmlUtils;
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Expand Down Expand Up @@ -112,19 +111,19 @@ public function getEnv($prefix, $name, \Closure $getEnv)
}

if ('bool' === $prefix) {
return (bool) self::phpize($env);
return (bool) (filter_var($env, FILTER_VALIDATE_BOOLEAN) ?: filter_var($env, FILTER_VALIDATE_INT) ?: filter_var($env, FILTER_VALIDATE_FLOAT));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why allow ints or floats to be casted as bool, and not limit to bools only ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that's supported+tested today, and we shouldn't break BC.

}

if ('int' === $prefix) {
if (!is_numeric($env = self::phpize($env))) {
if (false === $env = filter_var($env, FILTER_VALIDATE_INT) ?: filter_var($env, FILTER_VALIDATE_FLOAT)) {
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
}

return (int) $env;
}

if ('float' === $prefix) {
if (!is_numeric($env = self::phpize($env))) {
if (false === $env = filter_var($env, FILTER_VALIDATE_FLOAT)) {
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
}

Expand Down Expand Up @@ -177,13 +176,4 @@ public function getEnv($prefix, $name, \Closure $getEnv)

throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
}

private static function phpize($value)
{
if (!class_exists(XmlUtils::class)) {
throw new LogicException('The Symfony Config component is required to cast env vars to "bool", "int" or "float".');
}

return XmlUtils::phpize($value);
}
}
0