{"id":13343293,"url":"https://github.com/awurth/PHP-Config","last_synced_at":"2025-03-12T04:33:07.713Z","repository":{"id":77869038,"uuid":"107671853","full_name":"awurth/PHP-Config","owner":"awurth","description":"A configuration file loader for PHP","archived":true,"fork":false,"pushed_at":"2017-10-30T09:20:06.000Z","size":35,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-24T15:39:05.685Z","etag":null,"topics":["cache","configuration","json","loader","php","yaml"],"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/awurth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-20T11:49:39.000Z","updated_at":"2023-01-28T13:39:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"7a35ebea-3a86-460e-9bde-04025ece7ca2","html_url":"https://github.com/awurth/PHP-Config","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awurth%2FPHP-Config","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awurth%2FPHP-Config/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awurth%2FPHP-Config/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awurth%2FPHP-Config/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/awurth","download_url":"https://codeload.github.com/awurth/PHP-Config/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243158974,"owners_count":20245668,"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":["cache","configuration","json","loader","php","yaml"],"created_at":"2024-07-29T19:31:07.945Z","updated_at":"2025-03-12T04:33:07.700Z","avatar_url":"https://github.com/awurth.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Configuration\n\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/58503b69-21c8-4121-8487-b103140c49a2/mini.png)](https://insight.sensiolabs.com/projects/58503b69-21c8-4121-8487-b103140c49a2) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/awurth/php-config/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/awurth/php-config/?branch=master)\n\nLoads your application's configuration from PHP, YAML or JSON files, and stores it in a cache file for performance.\n\nUses the Symfony Config component.\n\n## Installation\n``` bash\n$ composer require awurth/config\n```\n\nIf you want to be able to load YAML files, you have to install the Symfony YAML component too:\n``` bash\n$ composer require symfony/yaml\n```\n\n## Usage\n### Load without cache\n``` php\n// config.php\nreturn [\n    'database' =\u003e [\n        'name' =\u003e 'database_name',\n        'user' =\u003e 'root',\n        'password' =\u003e 'pass'\n    ]\n];\n```\n\n``` yaml\n# config.yml\ndatabase:\n    name: database_name\n    user: root\n    password: pass\n```\n\n``` json\n// config.json\n{\n    \"database\": {\n        \"name\": \"database_name\",\n        \"user\": \"root\",\n        \"password\": \"pass\"\n    }\n}\n```\n\n``` php\n$loader = new AWurth\\Config\\ConfigurationLoader();\n\n$phpConfig = $loader-\u003eload('path/to/config.php');\n$yamlConfig = $loader-\u003eload('path/to/config.yml');\n$jsonConfig = $loader-\u003eload('path/to/config.json');\n\n// Result:\n$phpConfig = $yamlConfig = $jsonConfig = [\n    'database' =\u003e [\n        'name' =\u003e 'database_name',\n        'user' =\u003e 'root',\n        'password' =\u003e 'pass'\n    ]\n];\n```\n\n### Using the cache (HIGHLY RECOMMENDED!)\n``` php\n$debug = 'prod' !== $environment;\n\n$loader = new AWurth\\Config\\ConfigurationLoader('path/to/cache.php', $debug);\n\n$config = $loader-\u003eload('path/to/config');\n```\n**The cache file should not be versioned**, especially if you store your database credentials in it.\n\nIf the second parameter (`$debug`) is set to `true`, the loader will parse the configuration files and regenerate the cache every time you edit a configuration file (including imports).\n\nIf set to `false` (in production), the loader will read the cache file directly if it exists or generate it if not. The configuration won't be reloaded if you modify configuration files, so if you want to reload the cache, you have to delete the cache file.\n\n## Import files from the configuration\nYou can import other files into a configuration file with the `imports` key. The `imports` array will be removed from the final configuration.\nValid file paths are:\n- Relative paths: `../config.yml`\n- Absolute paths: `/path/to/config.yml`\n- Relative or absolute paths using [placeholders](#using-parameters): `%root_dir%/config/config.%env%.yml`\n\n``` yaml\n# config.dev.yml\nimports:\n    - 'parameters.yml'\n    - 'config.yml'\n\ndatabase: ...\n```\n\n``` php\n// config.dev.php\nreturn [\n    'imports' =\u003e [\n        'parameters.yml', // You can import YAML or JSON files from a PHP configuration file\n        'config.php'\n    ],\n    'database' =\u003e [\n        ...\n    ]\n];\n```\n\n``` json\n// config.dev.json\n{\n    \"imports\": [\n        \"parameters.yml\",\n        \"config.json\"\n    ],\n    \"database\": {}\n}\n```\n\n### Single import\n``` yaml\nimports: 'file.yml'\n```\n\n### Named imports\n##### Without named import\n``` yaml\n# config.yml\nimports:\n    - security.yml\n\n# security.yml\nsecurity:\n    login_path: /login\n    logout_path: /logout\n```\n\n##### With named import\n``` yaml\n# config.yml\nimports:\n    security: security.yml\n\n# security.yml\nlogin_path: /login\nlogout_path: /logout\n```\n\n## Using parameters\n``` yaml\n# parameters.yml\nparameters:\n    database_name: my_db_name\n    database_user: root\n    database_password: my_password\n\n# config.yml\nimports:\n    - 'parameters.yml'\n\nparameters:\n    locale: en\n\ndatabase:\n    name: '%database_name%'\n    user: '%database_user%'\n    password: '%database_password%'\n\ntranslator:\n    fallback: '%locale%'\n\nlogfile: '%root_dir%/cache/%environment%.log'\n\nyour_custom_config: '%your_custom_param%'\n```\n\n``` php\n$loader = new AWurth\\Config\\ConfigurationLoader();\n\n$loader-\u003esetParameters([\n   'root_dir' =\u003e '/path/to/project/root',\n   'environment' =\u003e 'dev',\n   'your_custom_param' =\u003e 'your_custom_value'\n]);\n\n// OR\n\n$loader\n    -\u003esetParameter('root_dir', '/path/to/project/root')\n    -\u003esetParameter('environment', 'dev')\n    -\u003esetParameter('your_custom_param', 'your_custom_value')\n;\n\n$config = $loader-\u003eload(__DIR__.'/config.yml');\n\n// Result:\n$config = [\n    'parameters' =\u003e [\n        'database_name' =\u003e 'my_db_name',\n        'database_user' =\u003e 'root',\n        'database_password' =\u003e 'my_password',\n        'locale' =\u003e 'en'\n    ],\n    'database' =\u003e [\n        'name' =\u003e 'my_db_name',\n        'user' =\u003e 'root',\n        'password' =\u003e 'my_password'\n    ],\n    'translator' =\u003e [\n        'fallback' =\u003e 'en'\n    ],\n    'logfile' =\u003e '/path/to/project/root/cache/dev.log',\n    'your_custom_config' =\u003e 'your_custom_value'\n];\n```\n\n## Using PHP constants in YAML files\nYou can use simple PHP constants (like `PHP_INT_MAX`) or class constants (like `Monolog\\Logger::DEBUG`) by using the YAML tag `!php/const:`\n\n``` yaml\nmonolog:\n    level: !php/const:Monolog\\Logger::ERROR\n```\n\nConstants like `__DIR__` and `__FILE__` don't work, use parameters instead.\n\n## Options\n#### Imports and parameters keys\n``` php\n$loader = new AWurth\\Config\\ConfigurationLoader();\n\n$loader-\u003egetOptions()\n    -\u003esetImportsKey('require')\n    -\u003esetParametersKey('replacements');\n\n$config = $loader-\u003eload(__DIR__.'/config.yml');\n```\n\n``` yaml\n# config.yml\nrequire: # Does the same as 'imports:'\n    - 'parameters.yml'\n    - 'security.yml'\n\nreplacements: # Does the same as 'parameters:'\n    locale: en\n\nparameters:\n    database_user: root\n\ntranslator:\n    fallback: '%locale%' # Will be replaced by 'en'\n\ndatabase:\n    user: '%database_user%' # Will be replaced by null\n```\n\n#### Disable imports / parameters\nImports and parameters features can be disabled with the `setEnableImports` and `setEnableParameters` methods.\nIf your configuration contains an `imports` key, it won't be removed from the final configuration.\nPlaceholders without corresponding parameters won't be replaced by `null`.\n\n``` php\n$loader = new AWurth\\Config\\ConfigurationLoader();\n\n$loader-\u003egetOptions()\n    -\u003esetEnableImports(false)\n    -\u003esetEnableParameters(false);\n```\n\n## Add custom file loaders\nYou can add your custom file loaders with the `addLoader` method. The loaders must implement `Symfony\\Component\\Config\\Loader\\LoaderInterface`.\n``` php\n$loader = new AWurth\\Config\\ConfigurationLoader();\n\n$loader-\u003eaddLoader(new XmlFileLoader());\n$loader-\u003eaddLoader(new CustomFileLoader());\n\n$loader-\u003eload('path/to/xml_file.xml');\n$loader-\u003eload('path/to/file.extension');\n```\n\n# TODO\n- Tests\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fawurth%2FPHP-Config","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fawurth%2FPHP-Config","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fawurth%2FPHP-Config/lists"}