Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Use a switch to select the appropriate env processor (optim) #25628

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

Closed
wants to merge 1 commit into from
Closed
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
95 changes: 45 additions & 50 deletions 95 src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,71 +85,66 @@ public function getEnv($prefix, $name, \Closure $getEnv)
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix));
}

if ('string' === $prefix) {
return (string) $env;
}
switch ($prefix) {
Copy link
Member

Choose a reason for hiding this comment

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

I recommend to add a help comment here to avoid some people in the future refactoring it into some if to "improve code quality". Something like this:

// used a 'switch' instead of 'if' conditions to enable a PHP micro-optimization

case 'string':
return (string) $env;

if ('bool' === $prefix) {
return (bool) self::phpize($env);
}
case 'bool':
return (bool) self::phpize($env);

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

return (int) $env;
}
return (int) $env;

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

return (float) $env;
}
return (float) $env;

if ('const' === $prefix) {
if (!defined($env)) {
throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env));
}
case 'const':
if (!defined($env)) {
throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env));
}

return constant($name);
}
return constant($name);

if ('base64' === $prefix) {
return base64_decode($env);
}
case 'base64':
return base64_decode($env);

if ('json' === $prefix) {
$env = json_decode($env, true);
case 'json':
$env = json_decode($env, true);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
}
if (JSON_ERROR_NONE !== json_last_error()) {
throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
}

if (!is_array($env)) {
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, gettype($env)));
}
if (!is_array($env)) {
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, gettype($env)));
}

return $env;
}
return $env;

if ('resolve' === $prefix) {
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
if (!isset($match[1])) {
return '%';
}
$value = $this->container->getParameter($match[1]);
if (!is_scalar($value)) {
throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, gettype($value)));
}
case 'resolve':
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
if (!isset($match[1])) {
return '%';
}
$value = $this->container->getParameter($match[1]);
if (!is_scalar($value)) {
throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, gettype($value)));
}

return $value;
}, $env);
}
return $value;
}, $env);

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

private static function phpize($value)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.