{"id":20170749,"url":"https://github.com/pear/config_lite","last_synced_at":"2025-08-22T07:31:16.169Z","repository":{"id":57037315,"uuid":"1005475","full_name":"pear/Config_Lite","owner":"pear","description":"a lightweight package for ini style configuration/settings text files.","archived":false,"fork":false,"pushed_at":"2021-05-05T16:24:56.000Z","size":161,"stargazers_count":40,"open_issues_count":0,"forks_count":15,"subscribers_count":20,"default_branch":"master","last_synced_at":"2024-12-07T16:33:43.876Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://pear.php.net/package/Config_Lite","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pear.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-10-19T10:19:04.000Z","updated_at":"2023-05-08T10:06:46.000Z","dependencies_parsed_at":"2022-08-23T21:00:29.562Z","dependency_job_id":null,"html_url":"https://github.com/pear/Config_Lite","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pear%2FConfig_Lite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pear%2FConfig_Lite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pear%2FConfig_Lite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pear%2FConfig_Lite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pear","download_url":"https://codeload.github.com/pear/Config_Lite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230568591,"owners_count":18246378,"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":[],"created_at":"2024-11-14T01:20:40.954Z","updated_at":"2024-12-20T10:09:00.354Z","avatar_url":"https://github.com/pear.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Config_Lite\n===========\n\n[![Build Status](https://secure.travis-ci.org/pear/Config_Lite.png?branch=master)](https://travis-ci.org/pear/Config_Lite)\n\n\nDescription\n-----------\n\na simple, lightweight and fast class for ini style configuration files, \nwith the native PHP function `parse_ini_file' under the covers.\n\nConfig_Lite is inspired by Pythons ConfigParser.\n\n\nA \"Config_Lite\" file consists of global key/value pair (KVP) entries and optional sections, \n\"[section]\", followed by \"name = value\" (KVP) entries.\n\n\nInstallation\n------------\n\n*Pear*\n\n`pear install Config_Lite-beta`\n\n\n*Composer*\n\n__Create a composer.json file and run `composer install`__  \n```\n{\n    \"repositories\": [\n        {\n            \"type\": \"pear\",\n            \"url\": \"pear.php.net\"\n        }\n    ],\n    \"require\": {\n        \"pear-pear.php.net/config_lite\": \"*\"\n    }\n}\n\n```\n\n__Composers autoload example usage__\n```php\n\u003c?php\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$config = new Config_Lite;\n```\n\n\n\n\nExamples\n--------\n\n__A simple configuration file: `test.ini'__\n\n```\n\npublic_key_file =  \"~/.ssh/id_rsa.pub\"\ndebug = yes\n\n[general]\nlang = \"en\"\n\n[db]\nuser = \"dionysis\"\npassword = \"c2oiVnY!f8sf\"\n```\n\n\n__Read Configuration file:__\n```php\n\u003c?php\n\nrequire_once 'Config/Lite.php';\n\n$config = new Config_Lite('test.ini');\n\necho $config-\u003eget('db', 'user'); // dionysis\necho $config-\u003eget(null, 'public_key_file'); // ~/.ssh/id_rsa.pub\n\nif (true === $config-\u003egetBool(null, 'debug', true)) {\n\techo $config;\n}\n\n// read with ArrayAccess\necho $config['db']['password']; // c2oiVnY!f8sf\n```\n\n__Save Configuration file:__\n\n```php\n\u003c?php\n\nrequire_once 'Config/Lite.php';\n\n// write with file locking\n$config = new Config_Lite('test.ini', LOCK_EX);\n\n$config-\u003eset('db', 'user', 'JohnDoe')\n\t-\u003eset('db', 'password', 'd0g1tcVs$HgIn1');\n\n// set with ArrayAccess\n$config['public_key_file'] = '~/.ssh/id_rsa.pub';\n$config['general'] = array('lang' =\u003e 'de');\n\n// save object to file\n$config-\u003esave();\n```\n\n__Create configuration file:__\n\n```php\n\n$config = new Config_Lite('test.ini');\n$config-\u003eset('db', 'user', 'JohnDoe')\n\t-\u003eset('db', 'password', 'd0g1tcVs$HgIn1');\n\n// set global bool 'debug' \n$config-\u003eset(null, 'debug', false);\n\n// save object to file\ntry {\n\t$config-\u003esave();\n} catch (Config_Lite_Exception $e) {\n    echo \"\\n\", 'Exception Message: ', $e-\u003egetMessage();\n}\n```\n\n\n__Alternative file creation with write:__\n\n```php\n\u003c?php\n\nrequire_once 'Config/Lite.php';\n\n$filename = 'test.ini';\n\n$config = new Config_Lite();\n\ntry {\n\t$config-\u003ewrite($filename, array(\n\t\t\t'public_key_file' =\u003e  \"~/.ssh/id_rsa.pub\",\n\t\t\t'general' =\u003e array(\n\t\t\t\t'lang' =\u003e 'fr'\n\t\t\t),\n\t\t\t'db' =\u003e array(\n\t\t\t\t'user' =\u003e 'dionysis',\n\t\t\t\t'password' =\u003e 'd0g1tcVs$HgIn1'\n\t\t\t)\n\t\t)\n\t);\n} catch (Config_Lite_Exception $exception) {\n    printf(\"Failed to write file: %s.\\n\", $filename);\n    printf(\"Exception Message: %s\\n\", $exception-\u003egetMessage());\n    printf(\"Exception Stracktrace: %s\\n\", $exception-\u003egetTraceAsString());\n}\n```\n\n__Config without File - Streams, Filter or stdout:__\n\n```php\n\u003c?php\n\nrequire_once 'Config/Lite.php';\n\n$config = new Config_Lite();\n\n$filename = sprintf(\n    \"php://filter/write=string.rot13/resource=%s\", \"test.ini\"\n);\n\n$config-\u003ewrite($filename, array(\n\t    'public_key_file' =\u003e  \"~/.ssh/id_rsa.pub\",\n\t    'general' =\u003e array(\n\t    'lang' =\u003e 'fr'\n\t),\n\t'db' =\u003e array(\n\t\t'user' =\u003e 'dionysis',\n\t\t'password' =\u003e 'd0g1tcVs$HgIn1'\n\t\t)\n\t)\n);\n\n// Writing to stdout\n$config-\u003ewrite(\"php://stdout\", array(\n\t    'public_key_file' =\u003e  \"~/.ssh/id_rsa.pub\",\n\t    'general' =\u003e array(\n\t    'lang' =\u003e 'fr'\n\t),\n\t'db' =\u003e array(\n\t\t'user' =\u003e 'dionysis',\n\t\t'password' =\u003e 'd0g1tcVs$HgIn1'\n\t\t)\n\t)\n);\n\n```\n\n\n\n\n\n\n__global Configuration options (without sections) :__\n\n```php\n\n$config-\u003eset(null, 'private_key_file', '~/.ssh/id_rsa');\n// set with arrayaccess\n$config['public_key_file'] = '~/.ssh/id_rsa.pub';\n\n$config-\u003esync();\n\necho $config-\u003eget(null, 'public_key_file');\n// get with arrayaccess\necho $config['private_key_file'];\n```\n\n__implemented IteratorAggregate, allows iteratation of the object with foreach:__\n```php\n\n$config = new Config_Lite($filename);\n\nforeach ($config as $section =\u003e $name) {\n\tif (is_array($name)) {\n\t\t$s .= sprintf(\"[%s]\\n\", $section);\n\t\tforeach ($name as $key =\u003e $val) {\n\t\t\t$s .= sprintf(\"\\t%s = %s\\n\", $key, $val);\n\t\t}\n\t} else {\n\t\t$s .= sprintf(\"%s=%s\\n\", $section, $name);\n\t}\n}\n\necho $s;\n\n```\n\n\nOptions\n-------\n\n __setProcessSections(bool)__\n\nSets whether or not sections should be processed\nIf true, values for each section will be placed into\na sub-array for the section. If false, all values will\nbe placed in the global scope.\n\n __setQuoteStrings(bool)__\n\nSets whether or not to doubleQuote\nIf true, everything but bool and numeric \nvalues get doublequoted.\n\n\n\nNotes \u0026 Limitations\n-------------------\n\n* Config_Lite is an OO frontend to `parse_ini_file` and writing ini files, \nbut you can also use the public method `write` if you only want to write an array as ini file \n* Use getString and setString to save and read Strings with double _and_ single-quotes \n* Use getBool if you need a real bool type, eg. for strict equality comparison \n* The methods `set` and `get` keep values untouched, but the write method \nnormalize \"bool\" values to a human readable representation, \ndoublequotes strings and numeric values without any quotes \n* newline chars defaults to \"\\n\", editable with `setLinebreak' \n* comments get dropped when writing after reading  \n* backslashes  in doublequoted values get parsed, to omit this, use `$config-\u003eread('/test.cfg', INI_SCANNER_RAW);`\n* no support of comments and multiline strings, because reading with `parse_ini_file` does not support it \n\nIf you want to save userinput like images, i'd recommend to use `get` with `base64_decode` and `set` with `base64_encode`.\nFor a regex one could also use `setSingleTickDelimiter()`.\n\n\n__Save regex (as global option) base64 encoded :__\n\n```php\n\u003c?php\n\nrequire_once 'Config/Lite.php';\n\n$config = new Config_Lite('regex-test.ini');\n\n$regex = '/Hello \\\"(.*?)\\\"/';\n$config-\u003eset(null, 'regex', base64_encode($regex));\n// save object, here sync to read it back, just to test\n$config-\u003esync();\n// in 'regex-test.ini': regex = \"L0hlbGxvIFwiKC4qPylcIi8=\"\n$regex = base64_decode($config-\u003eget(null, 'regex'));\nif (preg_match($regex, 'Hello \"World\"!')) {\n    printf(\"matched. regex:%s\", $regex);\n} else {\n    printf(\"no match found. regex:%s\", $regex);\n}\n```\n\nIDEAS\n------\n\n* Config_Lite_Ini ( https://github.com/pce/Config_Lite_Ini ) with metainfos to supports global sections, \n* support of comments and multiline strings (both supported by Pear::Config)  \n\n\nCredits\n-------\n\n- @see [contributors](https://github.com/pce/config_lite/graphs/contributors)\n- [mfonda](https://github.com/mfonda) ensured toString and output are equal, added accessors and options\n- [CloCkWeRX](https://github.com/CloCkWeRX) improvements and Jenkins Integration  \n- [cweiske](https://github.com/cweiske) for various suggestions\n\n\nContributing\n------------\n\n*Patches are Welcome!*\n\nCreate an Issue with a Link to your forked branch.\n\nhttps://github.com/pce/config_lite\n\nor report bugs at: \nhttp://pear.php.net/package/Config_Lite\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpear%2Fconfig_lite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpear%2Fconfig_lite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpear%2Fconfig_lite/lists"}