內容目录

上一个主题

< Class Phalcon\Config

下一个主题

Class Phalcon\Config\Adapter\Json >

Phalcon玩家群:150237524Phalcon玩家

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:

<?php

[database]
adapter = Mysql
host = localhost
username = scott
password = cheetah
dbname = test_db

[phalcon]
controllersDir = "../app/controllers/"
modelsDir = "../app/models/"
viewsDir = "../app/views/"

You can read it as follows:

<?php

$config = new \Phalcon\Config\Adapter\Ini("path/config.ini");

echo $config->phalcon->controllersDir;
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:

<?php

$config = new \Phalcon\Config\Adapter\Ini(
    "path/config-with-constants.ini",
    INI_SCANNER_NORMAL
);

Methods

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

Phalcon\Config\Adapter\Ini constructor

protected _parseIniString (mixed $path, mixed $value)

Build multidimensional array from string

<?php

$this->_parseIniString("path.hello.world", "value for last key");

// result
[
     "path" => [
         "hello" => [
             "world" => "value for last key",
         ],
     ],
];

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

<?php

var_dump(
    isset($config["database"])
);

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

<?php

echo $config->get("controllersDir", "../app/controllers/");

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

Gets an attribute using the array-syntax

<?php

print_r(
    $config["database"]
);

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

Sets an attribute using the array-syntax

<?php

$config["database"] = [
    "type" => "Sqlite",
];

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

Unsets an attribute using the array-syntax

<?php

unset($config["database"]);

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

Merges a configuration into the current one

<?php

$appConfig = new \Phalcon\Config(
    [
        "database" => [
            "host" => "localhost",
        ],
    ]
);

$globalConfig->merge($appConfig);

public toArray () inherited from Phalcon\Config

Converts recursively the object to an array

<?php

print_r(
    $config->toArray()
);

public count () inherited from Phalcon\Config

Returns the count of properties set in the config

<?php

print count($config);

or

<?php

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)