Class Phalcon\Config\Adapter\Ini

extends class Phalcon\Config

implements Countable, ArrayAccess

Source on GitHub

Reads ini files and converts them to Phalcon\Config objects.

Given the next configuration file:

  1. <?php
  2. [database]
  3. adapter = Mysql
  4. host = localhost
  5. username = scott
  6. password = cheetah
  7. dbname = test_db
  8. [phalcon]
  9. controllersDir = "../app/controllers/"
  10. modelsDir = "../app/models/"
  11. viewsDir = "../app/views/"

You can read it as follows:

  1. <?php
  2. $config = new \Phalcon\Config\Adapter\Ini("path/config.ini");
  3. echo $config->phalcon->controllersDir;
  4. echo $config->database->username;

PHP constants may also be parsed in the ini file, so if you define a constant as an ini value before calling the constructor, the constant’s value will be integrated into the results. To use it this way you must specify the optional second parameter as INI_SCANNER_NORMAL when calling the constructor:

  1. <?php
  2. $config = new \Phalcon\Config\Adapter\Ini(
  3. "path/config-with-constants.ini",
  4. INI_SCANNER_NORMAL
  5. );

Methods

public __construct (mixed $filePath, [mixed $mode])

Phalcon\Config\Adapter\Ini constructor

protected _parseIniString (mixed $path, mixed $value)

Build multidimensional array from string

  1. <?php
  2. $this->_parseIniString("path.hello.world", "value for last key");
  3. // result
  4. [
  5. "path" => [
  6. "hello" => [
  7. "world" => "value for last key",
  8. ],
  9. ],
  10. ];

protected _cast (mixed $ini)

We have to cast values manually because parse_ini_file() has a poor implementation.

public offsetExists (mixed $index) inherited from Phalcon\Config

Allows to check whether an attribute is defined using the array-syntax

  1. <?php
  2. var_dump(
  3. isset($config["database"])
  4. );

public get (mixed $index, [mixed $defaultValue]) inherited from Phalcon\Config

Gets an attribute from the configuration, if the attribute isn’t defined returns null If the value is exactly null or is not defined the default value will be used instead

  1. <?php
  2. echo $config->get("controllersDir", "../app/controllers/");

public offsetGet (mixed $index) inherited from Phalcon\Config

Gets an attribute using the array-syntax

  1. <?php
  2. print_r(
  3. $config["database"]
  4. );

public offsetSet (mixed $index, mixed $value) inherited from Phalcon\Config

Sets an attribute using the array-syntax

  1. <?php
  2. $config["database"] = [
  3. "type" => "Sqlite",
  4. ];

public offsetUnset (mixed $index) inherited from Phalcon\Config

Unsets an attribute using the array-syntax

  1. <?php
  2. unset($config["database"]);

public merge (Phalcon\Config $config) inherited from Phalcon\Config

Merges a configuration into the current one

  1. <?php
  2. $appConfig = new \Phalcon\Config(
  3. [
  4. "database" => [
  5. "host" => "localhost",
  6. ],
  7. ]
  8. );
  9. $globalConfig->merge($appConfig);

public toArray () inherited from Phalcon\Config

Converts recursively the object to an array

  1. <?php
  2. print_r(
  3. $config->toArray()
  4. );

public count () inherited from Phalcon\Config

Returns the count of properties set in the config

  1. <?php
  2. print count($config);

or

  1. <?php
  2. print $config->count();

public static __set_state (array $data) inherited from Phalcon\Config

Restores the state of a Phalcon\Config object

final protected Config merged config _merge (Config $config, [mixed $instance]) inherited from Phalcon\Config

Helper method for merge configs (forwarding nested config instance)