Substitution of values in Zend_Config
When the configuration file may need to use substitution of values, official documentation nedvuznachno alludes to the use of, IMHO, not the most successful practice — use constants in the INI file, like this:
I have written a small class that inherits and extends the functionality of Zend_Config, allowing you to use this syntax:
Here is an example of its use:
Article based on information from habrahabr.ru
[production]
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
I have written a small class that inherits and extends the functionality of Zend_Config, allowing you to use this syntax:
[production]
path.application = "${path.root}/application"
path.configs = "${path.application}/configs"
includePaths.library = "${path.root}/library"
Here is an example of its use:
// the environment is determined in advance
$environment = 'production';
// opredelen the path to the root of the project
$rootPath = dirname(__FILE__);
// define the path to the configuration relative to the root of the project
$configPath = rootPath . '/application/configs/application.ini';
// load the "raw" config without substitution values
// and the mode disabled read-only
$configRaw = new Zend_Config_Ini($configPath, $environment, true);
// assign the path to the project root config variable
$configRaw->path->root = $rootPath;
// perform the substitution of values, turning the "raw" config "processed"
$config = Inf_Config_Placeholder($configRaw);
// then use the config as usual
* This source code was highlighted with Source Code Highlighter.
One of the limitations of class is no check on infinite recursion, like this:
[production]
foo = "${bar}"
bar = "${foo}"
Please avoid such structures, since the execution of an infinite recursion in 7 seconds even on modern servers.
Class code Inf_Config_Placeholder available on GitHub.
PS the format of the repository on github is a bit weird, because I haven't found a better format to use as a submodule in the directory of your library project. Maybe have any ideas?
Комментарии
Отправить комментарий