Skip to content
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

Fix broken boolean types on IniFileLoader (propelorm/Propel2#1355) #1356

Merged
merged 1 commit into from
May 26, 2017
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
13 changes: 10 additions & 3 deletions src/Propel/Common/Config/Loader/IniFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
class IniFileLoader extends FileLoader
{

/**
* Separator for nesting levels of configuration data identifiers.
*
Expand Down Expand Up @@ -58,7 +59,7 @@ public function supports($resource, $type = null)
*/
public function load($file, $type = null)
{
$ini = parse_ini_file($this->getPath($file), true);
$ini = parse_ini_file($this->getPath($file), true, INI_SCANNER_RAW);
Copy link
Contributor Author

@gboddin gboddin Mar 12, 2017

Choose a reason for hiding this comment

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

parse_ini_file()'s normal operation mode is parsing true or false to a string containing "0" or "1".

This ensure, we get "true" or "false" so it's not confused with an integerer,

However, with INI_SCANNER_RAW, integers and floats are also casted to strings, this is fixed in parseKey().

parse_ini_file could use INI_SCANNER_TYPED to fix all of this safer, but it would have broke compatibility with PHP 5.5


if (false === $ini) {
throw new InvalidArgumentException("The configuration file '$file' has invalid content.");
Expand All @@ -71,8 +72,8 @@ public function load($file, $type = null)
}

/**
* Parse data from the configuration array, to transform nested sections into associative arrays.
*
* Parse data from the configuration array, to transform nested sections into associative arrays
* and to fix int/float/bool typing
* @param array $data
* @return array
*/
Expand Down Expand Up @@ -163,6 +164,12 @@ private function parseKey($key, $value, array &$config)
}

$this->parseKey($pieces[1], $value, $config[$pieces[0]]);
} else if (is_string($value) && in_array(strtolower($value), array("true", "false"))) {
$config[$key] = (strtolower($value) === "true");
} else if ($value === (string)(int) $value) {
$config[$key] = (int) $value;
} else if ($value === (string)(float) $value) {
$config[$key] = (float) $value;
} else {
$config[$key] = $value;
}
Expand Down