Substitutions in strings specified in config files #122

Closed
opened 2018-04-18 05:24:13 +00:00 by jamie · 8 comments
jamie commented 2018-04-18 05:24:13 +00:00 (Migrated from git.hazaar.io)

I would like to be able to specify some path constants that get substituted in config file so to extend this I would like to be able to specify many constants in config files in a consistent manner.

Such as:

{
  "production": {
    "local": {
      "type": "local",
      "name": "Local Filesystem",
      "root": "%runtime%/subdirectory"
    }
  }
}

Basically I would like to be able to support any application constants such as RUNTIME_PATH, APPLICATION_BASE, APPLICATION_ENV, etc. Alternatively I would like to have a way to reference other config values as well.

I would like to be able to specify some path constants that get substituted in config file so to extend this I would like to be able to specify many constants in config files in a consistent manner. Such as: ```json { "production": { "local": { "type": "local", "name": "Local Filesystem", "root": "%runtime%/subdirectory" } } } ``` Basically I would like to be able to support any application constants such as RUNTIME_PATH, APPLICATION_BASE, APPLICATION_ENV, etc. Alternatively I would like to have a way to reference other config values as well.
jamie commented 2018-06-13 02:14:36 +00:00 (Migrated from git.hazaar.io)
created branch [`122-substitutions-in-strings-specified-in-config-files`](https://git.hazaarlabs.com/hazaar/hazaar-mvc/compare/master...122-substitutions-in-strings-specified-in-config-files)
jamie commented 2018-06-13 02:14:37 +00:00 (Migrated from git.hazaar.io)

mentioned in merge request !38

mentioned in merge request !38
jamie commented 2018-06-13 05:45:53 +00:00 (Migrated from git.hazaar.io)

mentioned in commit ae07bc783fd02d900127e4b19e3df8e3746b7da5

mentioned in commit ae07bc783fd02d900127e4b19e3df8e3746b7da5
jamie commented 2018-06-13 05:50:42 +00:00 (Migrated from git.hazaar.io)

So here's how it works. ANY constant is immediately accessible. This of course includes all the Hazaar constants such as APPLICATION_PATH, APPLICATION_ENV and as above, RUNTIME_PATH.

I have also enhanced this to include some special PHP superglobals, such as $_POST, $_SERVER, etc. The allowed variables definition is:

$allowed_values = array(
    'GLOBALS' => &$GLOBALS,
    '_SERVER' => &$_SERVER,
    '_GET' => &$_GET,
    '_POST' => &$_POST,
    '_FILES' => &$_FILES,
    '_COOKIE' => &$_COOKIE,
    '_SESSION' => &$_SESSION,
    '_REQUEST' => &$_REQUEST,
    '_ENV' => &$_ENV,
    '_APP' => &\Hazaar\Application::getInstance()->GLOBALS
);

This is all PHP superglobals listed in http://php.net/manual/en/language.variables.superglobals.php with the addition of _APP which is a reference to the Hazaar\Application::$globals variable.

Config references can be defined as:

{
    "production": {
        "app": {
            "keyname": "%_SERVER[REQUEST_URI]%"
        }
    }
}

The above will output the $_SERVER['REQUEST_URI'] value. It is also possible to have multiple and mix 'n match them. So this is valid:

{
    "production": {
        "app": {
            "keyname": "%RUNTIME_PATH%%DIRECTORY_SEPARATOR%temp_dir%DIRECTORY_SEPARATOR%%_SERVER[REQUEST_URI]%"
        }
    }
}
So here's how it works. ANY constant is immediately accessible. This of course includes all the Hazaar constants such as APPLICATION_PATH, APPLICATION_ENV and as above, RUNTIME_PATH. I have also enhanced this to include some special PHP superglobals, such as $_POST, $_SERVER, etc. The allowed variables definition is: ```php $allowed_values = array( 'GLOBALS' => &$GLOBALS, '_SERVER' => &$_SERVER, '_GET' => &$_GET, '_POST' => &$_POST, '_FILES' => &$_FILES, '_COOKIE' => &$_COOKIE, '_SESSION' => &$_SESSION, '_REQUEST' => &$_REQUEST, '_ENV' => &$_ENV, '_APP' => &\Hazaar\Application::getInstance()->GLOBALS ); ``` This is all PHP superglobals listed in http://php.net/manual/en/language.variables.superglobals.php with the addition of ```_APP``` which is a reference to the ```Hazaar\Application::$globals``` variable. Config references can be defined as: ```json { "production": { "app": { "keyname": "%_SERVER[REQUEST_URI]%" } } } ``` The above will output the ```$_SERVER['REQUEST_URI']``` value. It is also possible to have multiple and mix 'n match them. So this is valid: ```json { "production": { "app": { "keyname": "%RUNTIME_PATH%%DIRECTORY_SEPARATOR%temp_dir%DIRECTORY_SEPARATOR%%_SERVER[REQUEST_URI]%" } } } ```
jamie commented 2018-06-13 05:54:11 +00:00 (Migrated from git.hazaar.io)

mentioned in commit 06a3e751d0

mentioned in commit 06a3e751d029f3e243e77aa0348887fd6b647429
jamie commented 2018-06-13 05:54:21 +00:00 (Migrated from git.hazaar.io)

mentioned in commit 3ef86dffaf

mentioned in commit 3ef86dffaf49d0f476606afab927a23a69e428a8
jamie commented 2018-06-13 05:54:21 +00:00 (Migrated from git.hazaar.io)

closed via merge request !38

closed via merge request !38
jamie commented 2018-06-29 02:24:28 +00:00 (Migrated from git.hazaar.io)

changed milestone to %4

changed milestone to %4
jamie self-assigned this 2025-09-04 01:15:47 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: hazaar/framework#122
No description provided.