{"id":15296325,"url":"https://github.com/simplecomplex/php-config","last_synced_at":"2025-07-15T12:07:05.594Z","repository":{"id":57051258,"uuid":"93727595","full_name":"simplecomplex/php-config","owner":"simplecomplex","description":"PSR-16 Simple Cache like configuration interfaces; ini-file based and cached","archived":false,"fork":false,"pushed_at":"2020-05-02T07:05:29.000Z","size":165,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T05:43:32.410Z","etag":null,"topics":["config","configuration","ini","php","php7","psr"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simplecomplex.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-08T08:50:29.000Z","updated_at":"2019-04-08T08:08:50.000Z","dependencies_parsed_at":"2022-08-24T03:41:04.156Z","dependency_job_id":null,"html_url":"https://github.com/simplecomplex/php-config","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/simplecomplex/php-config","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-config","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-config/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-config/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-config/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simplecomplex","download_url":"https://codeload.github.com/simplecomplex/php-config/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-config/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265434815,"owners_count":23764593,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","configuration","ini","php","php7","psr"],"created_at":"2024-09-30T18:10:04.316Z","updated_at":"2025-07-15T12:07:05.542Z","avatar_url":"https://github.com/simplecomplex.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Config ##\n\n- [Installation](#installation)\n- [Requirements](#requirements)\n\n### Simple and sectioned configuration interfaces ###\n\n**``` ConfigInterface ```**  \nis basically PSR-16 Simple Cache without time-to-live.  \nThere's a key and a value.\n\nThat principle isn't optimal for general use, unless you like real long and complex key names.  \n\n**``` SectionedConfigInterface ```**  \nSplits the item key into 'section' plus 'key', facilitating a more organized structure.\n\nThe immediate benefit is that you can namespace all items that belong to a particular library.\n\nThe section-key pattern can be implemented and utilized in two manners:\n\n- concatenation: the configuration setters and getters simply concatenate section and key\n- listing: the section is a list of keys, you can access the whole section as well as a particular key\n\n``` SectionedConfigInterface ``` also allows for short-term keeping items in memory,\nvia methods ``` remember() ``` and ``` forget() ```.\n\n### Implementations ###\n\n#### Environment variable based ####\n\n**``` EnvConfig ```**  \nis a simple abstraction of server environment variables.\n\n**``` EnvSectionedConfig ```**  \na sectioned implementation, using concatenation.\n\n#### Ini-files as source, cache as store ####\n\nThe ini-file based classes parse ini-files, and save to cache stores.\n\nThey read recursively from their ini-file paths.\nThat allows one to clone and use ini-files from multiple version control repositories.\n\n\u003e Ini-files are so old-school...\n\nAye, but the ini format is less error-prone than JSON, YAML, what-not.  \nThe syntax is so simple it's hard to make mistakes. And operations people are used to ini-files.\n\n##### Cache layer #####\nis [SimpleComplex Cache](https://github.com/simplecomplex/php-cache) **``` PersistentFileCache ```**.  \nCache store names are prefixed with **``` 'config.' ```**  \nBeware of conflict; do not prefix other cache stores that way. \n\n##### Types of ini-based configuration #####\n\n**``` IniConfig ```**  \nis not sectioned. Simple but probably not that useful.  \n``` $value = $config-\u003eget('key') ```\n\n**``` IniSectionedConfig ```**  \nis a powerful general usage implementation.  \n``` $value = $config-\u003eget('section', 'key') ```\n\nReads ini-files from a _base_ path and an _override_ path.  \nKeep development/production invariant variables (ini-files) in the _base_ path.  \nLet operations keep production variables in the _override_ path.\n\nUsing the list-principle - and fully supporting ``` remember() ``` and ``` forget() ``` -\n``` IniSectionedConfig ``` is optimal for accessing many/all keys of a section within a limited procedure.\n\n**``` IniSectionedFlatConfig ```**  \na sectioned implementation, using concatenation.\n\nOptimal for types of configuration one expects to access keys of diverse sections in an unpredictable manner,\nbut still want the organisational benefit of sections; many but exact cache reads.  \n[SimpleComplex Locale](https://github.com/simplecomplex/php-locale) uses this config class for localized texts.\n\n### Abstraction ###\n\nThe **``` Config ```** class is an abstraction of sectioned configuration.\n\nIn this package it extends ``` IniSectionedConfig ```.  \nIn an extending package it could  be some other sectioned config implementation.\n\n### CLI interface ###\n\n**``` CliConfig ```**  delivers CLI commands for setting, getting and deleting config items.  \nAnd commands for refreshing and exporting full configuration stores.\n\nIt exposes ``` IniSectionedConfig ``` instances, via the ``` Config ``` class.  \nThe other config classes are not accessible via CLI.\n\n### Global config ###\n\n``` Config ``` defaults to deliver an instance named 'global'.\n\nA typical system could probably benefit from a single config instance for the bulk of items.  \nSince the whole thing _runtime_ is cache based, there's no performance reason for using multiple instances.\n\n#### Dependency injection container ID: config ####\n\nRecommendation: access (and thus instantiate) the global config via DI container ID 'config'.  \nSee [SimpleComplex Utils](https://github.com/simplecomplex/php-utils) ``` Dependency ```.\n\n### Example ###\n\n```php\n// Bootstrap.\nDependency::genericSetMultiple([\n    'cache-broker' =\u003e function () {\n        return new \\SimpleComplex\\Cache\\CacheBroker();\n    },\n    'config' =\u003e function() {\n        return new \\SimpleComplex\\Config\\Config('global');\n    },\n]);\n// ...\n// Use.\n/** @var \\Psr\\Container\\ContainerInterface $container */\n$container = Dependency::container();\n/**\n * Create or re-initialize the 'global' config store;\n * based on ini-files placed in base and override paths,\n * cached by a PSR-16 Simple Cache cache store.\n *\n * @var \\SimpleComplex\\Config\\IniSectionedConfig $config\n */\n$config = $container-\u003eget('config');\n/** @var mixed $whatever */\n$whatever = $config-\u003eget('some-section', 'some-key', 'the default value');\n```\n\n### CLI commands ###\n\n```bash\n# List all config commands and their help.\nphp cli.php config -h\n# One command's help.\nphp cli.php config-xxx -h\n\n# List existing config stores.\nphp cli.php config-list-stores\n\n# Display/get value of a config item.\nphp cli.php config-get store section key\n\n# Set a config item.\nphp cli.php config-set store section key value\n\n# Delete a config item.\nphp cli.php config-delete store section key\n\n# Refresh a config store from .ini file sources.\n# The fresh store gets applied atomically, when fully built.\nphp cli.php config-refresh store\n\n# Export a config store as JSON to a file.\nphp cli.php config-export store target-path-and-file\n```\n\n### Installation ###\n\nCreate a 'conf' directory alongside the document root dir.\n\nLike:  \n```/var/www/my-host/```**```http```**  \n```/var/www/my-host/```**```conf```**\n\nCreate 'base' and 'override' paths within the 'conf', like:  \n```conf/```**```ini/base```**  \n```conf/```**```ini/override```**  \n\nFor the 'global' config store, place or symlink or git clone your system's  \n.ini configuration files under the 'base' and 'override' paths  \nusing file extension ```global.ini``` (= ```[store name].ini```).\n\nLike:  \n```conf/ini/base/```**```some-ding.global.ini```**  \n```conf/ini/override/```**```some-ding.prod.global.ini```**  \n\nIf that directory structure isn't suitable, do either:\n- supply **```Config```** constructor with a 'paths' argument\n- extend **```Config```** and override it's class constant **```PATH_DEFAULTS```**\n\n\n### Requirements ###\n\n- PHP \u003e=7.0\n- [SimpleComplex Cache](https://github.com/simplecomplex/php-cache)\n- [SimpleComplex Utils](https://github.com/simplecomplex/php-utils)\n\n##### Suggestions #####\n\n- [SimpleComplex Inspect](https://github.com/simplecomplex/inspect)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplecomplex%2Fphp-config","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimplecomplex%2Fphp-config","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplecomplex%2Fphp-config/lists"}