Skip to content

Phalcon config

NOTE

All classes are prefixed with Phalcon

Config\Adapter\Grouped

Class Source on GitHub

Reads multiple files (or arrays) and merges them all together.

See Phalcon\Config\ConfigFactory::load To load Config Adapter class using 'adapter' option.

use Phalcon\Config\Adapter\Grouped;

$config = new Grouped(
    [
        "path/to/config.php",
        "path/to/config.dist.php",
    ]
);
use Phalcon\Config\Adapter\Grouped;

$config = new Grouped(
    [
        "path/to/config.json",
        "path/to/config.dist.json",
    ],
    "json"
);
use Phalcon\Config\Adapter\Grouped;

$config = new Grouped(
    [
        [
            "filePath" => "path/to/config.php",
            "adapter"  => "php",
        ],
        [
            "filePath" => "path/to/config.json",
            "adapter"  => "json",
        ],
        [
            "adapter"  => "array",
            "config"   => [
                "property" => "value",
            ],
        ],
    ],
);

Uses Phalcon\Config\Config · Phalcon\Config\ConfigFactory · Phalcon\Config\ConfigInterface · Phalcon\Config\Exception · Phalcon\Config\Exceptions\GroupedAdapterRequiresArray · Phalcon\Factory\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    array $arrayConfig,
    string $defaultAdapter = "php",
    ConfigFactory $factory = null
);

Phalcon\Config\Adapter\Grouped constructor

Config\Adapter\Ini

Class Source on GitHub

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

Given the next configuration file:

[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:

use Phalcon\Config\Adapter\Ini;

$config = new 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:

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

Uses Phalcon\Config\Config · Phalcon\Config\Exception · Phalcon\Config\Exceptions\CannotLoadConfigFile

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $filePath,
    int $mode = 1
);

Ini constructor.

Protected · 4

cast()

protected function cast( mixed $ini ): mixed;

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

Note: this casting is an ini-format compensation and is deliberately specific to this adapter. Ini files carry untyped strings, so on/yes/true, off/no/false, null and numeric strings are decoded here. The json, yaml and php adapters receive natively typed values from their parsers and perform no casting.

castArray()

protected function castArray( array $ini ): array;

parseIniString()

protected function parseIniString(
    string $path,
    mixed $value
): array;

Build multidimensional array from string

phpParseIniFile()

protected function phpParseIniFile(
    string $filename,
    bool $processSections = false,
    int $scannerMode = 1
);

@todo to be removed when we get traits

Config\Adapter\Json

Class Source on GitHub

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

Given the following configuration file:

{"phalcon":{"baseuri":"\/phalcon\/"},"models":{"metadata":"memory"}}

You can read it as follows:

use Phalcon\Config\Adapter\Json;

$config = new Json("path/config.json");

echo $config->phalcon->baseuri;
echo $config->models->metadata;

Uses Phalcon\Config\Config · Phalcon\Config\Exceptions\CannotLoadConfigFile · Phalcon\Support\Helper\Json\Decode

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $filePath );

Phalcon\Config\Adapter\Json constructor

Config\Adapter\Php

Class Source on GitHub

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

Given the next configuration file:

<?php

return [
    "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:

use Phalcon\Config\Adapter\Php;

$config = new Php("path/config.php");

echo $config->phalcon->controllersDir;
echo $config->database->username;

Uses Phalcon\Config\Config · Phalcon\Config\Exceptions\CannotLoadConfigFile

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $filePath );

Phalcon\Config\Adapter\Php constructor

Config\Adapter\Yaml

Class Source on GitHub

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

Given the following configuration file:

phalcon:
  baseuri:        /phalcon/
  controllersDir: !approot  /app/controllers/
models:
  metadata: memory

You can read it as follows:

define(
    "APPROOT",
    dirname(__DIR__)
);

use Phalcon\Config\Adapter\Yaml;

$config = new Yaml(
    "path/config.yaml",
    [
        "!approot" => function($value) {
            return APPROOT . $value;
        },
    ]
);

echo $config->phalcon->controllersDir;
echo $config->phalcon->baseuri;
echo $config->models->metadata;

Uses Phalcon\Config\Config · Phalcon\Config\Exception · Phalcon\Config\Exceptions\CannotLoadConfigFile · Phalcon\Config\Exceptions\MissingYamlExtension

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $filePath,
    array $callbacks = null
);

Phalcon\Config\Adapter\Yaml constructor

Protected · 2

phpExtensionLoaded()

protected function phpExtensionLoaded( string $name ): bool;

phpYamlParseFile()

protected function phpYamlParseFile(
    mixed $filename,
    mixed $pos = 0,
    mixed $ndocs = null,
    mixed $callbacks = []
);

@todo to be removed when we get traits

Config\Config

Class Source on GitHub

Phalcon\Config is designed to simplify the access to, and the use of, configuration data within applications. It provides a nested object property based user interface for accessing this configuration data within application code.

$config = new \Phalcon\Config\Config(
    [
        "database" => [
            "adapter"  => "Mysql",
            "host"     => "localhost",
            "username" => "scott",
            "password" => "cheetah",
            "dbname"   => "test_db",
        ],
        "phalcon" => [
            "controllersDir" => "../app/controllers/",
            "modelsDir"      => "../app/models/",
            "viewsDir"       => "../app/views/",
        ],
    ]
);

Uses Phalcon\Config\Exceptions\InvalidMergeData · Phalcon\Support\Collection

Method Summary

Constants

string DEFAULT_PATH_DELIMITER = "."

Properties

protected string $pathDelimiter = self::DEFAULT_PATH_DELIMITER

Methods

Public · 5

getPathDelimiter()

public function getPathDelimiter(): string;

Gets the default path delimiter

merge()

public function merge( mixed $toMerge ): ConfigInterface;

Merges a configuration into the current one

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

$globalConfig->merge($appConfig);

path()

public function path(
    string $path,
    mixed $defaultValue = null,
    string $delimiter = null
): mixed;

Returns a value from current config using a dot separated path.

echo $config->path("unknown.path", "default", ".");

setPathDelimiter()

public function setPathDelimiter( string $delimiter = null ): ConfigInterface;

Sets the default path delimiter

toArray()

public function toArray(): array;

Converts recursively the object to an array

print_r(
    $config->toArray()
);
Protected · 3

cloneEmpty()

protected function cloneEmpty( array $data = [] ): static;

Builds a new collection with the given data, carrying over the configuration of the current one. Clone-based instead of constructor-based: adapter subclasses (Ini, Json, Php, Yaml, Grouped) define file-loading constructors that are incompatible with the parent's (array data, ...) signature, so filter(), map(), sort() and where() would otherwise fail on any adapter instance.

internalMerge()

final protected function internalMerge(
    array $source,
    array $target
): array;

Performs a merge recursively

setData()

protected function setData(
    mixed $element,
    mixed $value
): void;

Sets the collection data

Array values become nested Config objects carrying the insensitive, strictNull and type flags of this instance. The type guard is applied to leaf values only — arrays are not validated themselves; the nested Config validates its own leaves.

Config\ConfigFactory

Class Source on GitHub

Loads Config Adapter class using 'adapter' option, if no extension is provided it will be added to filePath

use Phalcon\Config\ConfigFactory;

$options = [
    "filePath" => "path/config",
    "adapter"  => "php",
];

$config = (new ConfigFactory())->load($options);

Uses Phalcon\Config\Config · Phalcon\Config\ConfigInterface · Phalcon\Config\Exceptions\ConfigNotArrayOrObject · Phalcon\Config\Exceptions\MissingConfigOption · Phalcon\Config\Exceptions\MissingFileExtension · Phalcon\Factory\AbstractFactory

Method Summary

Methods

Public · 3

__construct()

public function __construct( array $services = [] );

ConfigFactory constructor.

load()

public function load( mixed $config ): ConfigInterface;

Load a config to create a new instance

newInstance()

public function newInstance(
    string $name,
    string $fileName,
    mixed $params = null
): ConfigInterface;

Returns a new Config instance

Protected · 5

getAdapterAliases()

protected function getAdapterAliases(): array;

Adapter name aliases resolved by load() (file extensions that map to a registered adapter)

getExceptionClass()

protected function getExceptionClass(): string;

getExtraArguments()

protected function getExtraArguments(): array;

Adapters accepting an extra constructor argument, with the config option carrying it and its default value. Single source for the parameter-forwarding knowledge used by load() and newInstance().

getServices()

protected function getServices(): array;

Returns the available adapters

parseConfig()

protected function parseConfig( mixed $config ): array;

Config\ConfigInterface

Interface Source on GitHub

Phalcon\Config\ConfigInterface

Interface for Phalcon\Config\Config class

Uses Phalcon\Support\Collection\CollectionInterface

Method Summary

Methods

Public · 4

getPathDelimiter()

public function getPathDelimiter(): string;

merge()

public function merge( mixed $toMerge ): ConfigInterface;

path()

public function path(
    string $path,
    mixed $defaultValue = null,
    string $delimiter = null
): mixed;

setPathDelimiter()

public function setPathDelimiter( string $delimiter = null ): ConfigInterface;

Config\Exception

Class Source on GitHub

Exceptions thrown in Phalcon\Config will use this class

Config\Exceptions\CannotLoadConfigFile

Class Source on GitHub

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 2

__construct()

public function __construct( string $fileName );

getFileName()

public function getFileName(): string;

Config\Exceptions\ConfigNotArrayOrObject

Class Source on GitHub

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Config\Exceptions\GroupedAdapterRequiresArray

Class Source on GitHub

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Config\Exceptions\InvalidMergeData

Class Source on GitHub

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Config\Exceptions\MissingConfigOption

Class Source on GitHub

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 2

__construct()

public function __construct( string $option );

getOption()

public function getOption(): string;

Config\Exceptions\MissingFileExtension

Class Source on GitHub

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Config\Exceptions\MissingYamlExtension

Class Source on GitHub

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();