\ConfigForge
ConfigForge class for managing configuration files
This class provides functionality to load, edit, and save configuration files
based on a specification file.
available types: TEXT, CHECKBOX, RADIO, SELECT, TRIGGER, PASSWORD, SLIDER
available patterns for TEXT type:
- alpha: only Latin letters [a-zA-Z] (e.g., "abcDEF")
- alphanumeric: only Latin letters and numbers [a-zA-Z0-9] (e.g., "abc123")
- digits: only digits [0-9] (e.g., "12345")
- email: valid email address format (e.g., "user@domain.com")
- finance: decimal numbers with optional decimal point (e.g., "123.45", "100", "0.99")
- float: floating point numbers (e.g., "123.45", "0.001")
- fullpath: absolute Unix-style paths starting with / (e.g., "/var/www/html")
- geo: geographic coordinates (e.g., "40.7143528,-74.0059731")
- ip: IPv4 address format (e.g., "192.168.1.1")
- login: username format with letters, numbers and underscore (e.g., "user_123")
- mac: MAC address format with : or - separator (e.g., "00:1A:2B:3C:4D:5E")
- mobile: phone number with optional country code (e.g., "+380501234567")
- net-cidr: network CIDR notation, mask can't be /31 (e.g., "192.168.1.0/24")
- filepath: relative or absolute Unix-style paths (e.g., "dir/file.txt", "/etc/config")
- dirpath: relative or absolute Unix-style directories paths (e.g., "dir/", "/etc/")
- pathorurl: URLs with optional ports or paths (e.g., "http://example.com:8080", "some/dir/")
- sigint: signed integers (e.g., "-123", "456")
- url: HTTP/HTTPS URLs with optional port numbers (e.g., "http://example.com:8080")
Specification file example:
[sectionname]
LABEL="Option label"
OPTION=SOME_OPTION
TYPE=CHECKBOX
DEFAULT=0
[sectionname2]
LABEL="Your sex?"
OPTION=SEX
TYPE=SELECT
VALUES="male,female,unknown"
DEFAULT="unknown"
SAVEFILTER="gigasafe"
[sectionname3]
LABEL="Option label 2"
OPTION=ANOTHER_OPTION
TYPE=TEXT
PATTERN="mac"
VALIDATOR="IsMacValid"
ONINVALID="This mac address is invalid"
DEFAULT="14:88:92:94:94:61"
[sectionname4]
LABEL="Volume level"
OPTION=VOLUME
VALUES="0..100"
TYPE=SLIDER
DEFAULT=50
class usage example:
$configPath = 'config/test.ini';
$specPath = 'config/test.spec';
$forge = new ConfigForge($configPath, $specPath);
$processResult = $forge->process();
if (!empty($processResult)) {
show_error($processResult);
} elseif (ubRouting::post(ConfigForge::FORM_SUBMIT_KEY)==$forge->getInstanceId()) {
ubRouting::nav('?module=testing');
}
show_window(__('Config Forge'), $forge->renderEditor());
Synopsis
- // constants
- const FORM_SUBMIT_KEY = 'configforge_submit';
- // members
- protected array $currentConfig = ;
- protected string $configPath = '';
- protected array $parsedConfig = ;
- protected array $lineComments = ;
- protected string $specPath = '';
- protected string $instanceId = '';
- protected string $formClass = 'glamour';
- // methods
- public void __construct()
- protected void loadConfig()
- protected void extractComments()
- protected string getConfigAsText()
- protected string saveConfig()
- protected mixed getValue()
- protected bool setValue()
- public string getInstanceId()
- public void setFormClass()
- public string renderEditor()
- protected string getConfigText()
- public string process()
Constants
| Name | Value |
|---|---|
| FORM_SUBMIT_KEY | 'configforge_submit' |
Members
protected
- $configPath
—
string
Path to the config file - $currentConfig
—
array
Contains current config lines as index=>line - $formClass
—
string
Form CSS class - $instanceId
—
string
Unique identifier for this instance - $lineComments
—
array
Contains comments for each config line - $parsedConfig
—
array
Contains parsed config data as section=>key=>value - $specPath
—
string
Path to the spec file
Methods
protected
- extractComments() — Extracts comments from config lines
- getConfigAsText() — Returns the current config as text for debugging
- getConfigText() — Returns the text representation of the edited config
- getValue() — Gets config value by key
- loadConfig() — Loads config file content into protected properties
- saveConfig() — Saves current config back to file preserving comments
- setValue() — Sets config value for key
public
- __construct() — Creates new ConfigForge instance
- getInstanceId() — Returns instance identifier
- process() — Process config editing request Handles form submission and config saving in one place
- renderEditor() — Renders form editor for config file based on spec file
- setFormClass() — Sets form CSS class