dana / shellconf
Shell-style config-file encoder/decoder
Installs: 3 376
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ^6.1
This package is not auto-updated.
Last update: 2022-12-23 11:46:21 UTC
README
\Dana\ShellConf
is a PHP project which provides methods for parsing and
manipulating shell-style (specifically bash-style) variable assignments, which
can be useful for simple configuration needs. (But see Rationale below.)
Usage
Install via Composer:
composer require dana/shellconf
Given a config.sh
like this:
# comment here MYVAR1=foo # another comment here MYVAR2='foo bar' MYVAR3="foo \"bar\" baz"
And a config.php
like this:
<?php require_once __DIR__ . '/vendor/autoload.php'; $conf = new \Dana\ShellConf\ShellConf(); $conf->load('config.sh'); echo json_encode($conf->toArray(), \JSON_PRETTY_PRINT), "\n";
php config.php
produces:
{ "MYVAR1": "foo", "MYVAR2": "foo bar", "MYVAR3": "foo \"bar\" baz" }
Useful methods:
load($file)
— Load config file$file
parse($string)
— Parse config string$string
reset()
— Reset any loaded variables betweenparse()
callsset($name, $value)
— Create and set variable$name
to$value
unset($name)
— Unset variable$name
get($name, $default)
— Get the value of variable$name
toArray()
— Get all loaded variables and their values as an arraytoString()
— Get all loaded variables and their values as a bash-compatible config string (to write to a file for example)
Rationale
This project is extremely similar to a number of others designed to read 'dotenv' files, but i had some specific goals in mind that didn't necessarily align with theirs:
-
Most of them are focussed on loading files into environment variables, which isn't what i needed. I just want to parse the file, and maybe write it back.
-
Some of them don't seem to handle escaping and unescaping in exactly the same way as bash.
-
Many of them are overly clever for my purposes, supporting things like bash-incompatible extensions, variable interpolation, even command substition, which i don't want.
-
I was interested in some finer-grained methods related to escaping as well as writing bash-compatible lines back out.
For all of those reasons, i decided to write my own implementation. But if you don't care about any of that, you should absolutely check out one of these, much more widely used, projects:
To do
More tests.