Cphalcon: [BUG]: Phalcon\Config\Adapter\Ini parse error

Created on 31 Oct 2019  路  6Comments  路  Source: phalcon/cphalcon

[test]
a  = 01
b  = '01'
c  = "01"

output:

Array
(
    [a] => 1
    [b] => '01'
    [c] => 1
)

phalcon 3.4.x
php7.3.4

not a bug unverified

Most helpful comment

The parser uses the native parse_ini_file() function.
INI_SCANNER_RAW is the default mode, which produces the result you got.

https://github.com/phalcon/cphalcon/blob/3c5b00053f29bae16d6c7265cff4b4324cc3a6f3/phalcon/config/adapter/ini.zep#L78-L83

You can change this to INI_SCANNER_NORMAL in the constructor of the config adapter.

For example:

new \Phalcon\Config\Adapter\Ini(
   '/my/config.ini',
   INI_SCANNER_NORMAL
);

All 6 comments

The parser uses the native parse_ini_file() function.
INI_SCANNER_RAW is the default mode, which produces the result you got.

https://github.com/phalcon/cphalcon/blob/3c5b00053f29bae16d6c7265cff4b4324cc3a6f3/phalcon/config/adapter/ini.zep#L78-L83

You can change this to INI_SCANNER_NORMAL in the constructor of the config adapter.

For example:

new \Phalcon\Config\Adapter\Ini(
   '/my/config.ini',
   INI_SCANNER_NORMAL
);

Thnx @zsilbi. @zuozhehao can you confirm this and close the issue if this works for you?

@ruudboon @zsilbi
I tried to add INI_SCANNER_NORMAL, but the result was the same

[test]
a  = 01
b  = '01'
c  = "01"

new \Phalcon\Config\Adapter\Ini('config.ini', INI_SCANNER_NORMAL);

Array
(
    [a] => 1
    [b] => 1
    [c] => 1
)

parse_ini_file('config.ini', true, INI_SCANNER_NORMAL);

Array
(
    [a] => 01
    [b] => 01
    [c] => 01
)

The built-in cast() function makes integers from your numeric strings.
This is a feature, not a bug.

You can use your own adapter and disable this function like this:

<?php
namespace My\Config\Adapter;

class Ini extends \Phalcon\Config\Adapter\Ini 
{

   /**
    * @param mixed $ini
    */
   protected function cast($ini) 
   {
      // Disable casting for numeric strings
      if (is_string($ini) && is_numeric($ini)) {
         return $ini;
      }

      return parent::cast($ini);
   }
}

https://github.com/phalcon/cphalcon/blob/master/phalcon/Config/Adapter/Ini.zep#L146

I don't think it can be forced to change to int type here

@zuozhehao The problem is what to you do with the is_numeric. It will return true even if the actual value is a numeric string. The cast method that @zsilbi posted here https://github.com/phalcon/cphalcon/issues/14501#issuecomment-549070880 can be used to change the behavior if you need it to.

I am going to close this because this is intended behavior.

Was this page helpful?
0 / 5 - 0 ratings