Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 50 | All time: 10,706 This week: 206 |
Version | License | PHP version | Categories | |||
dotenvwriter 1.0 | MIT/X Consortium ... | 7 | PHP 5, Configuration |
<?php |
A PHP library to write values to .env (DotEnv) files
DotEnvWriter can be installed with Composer.
composer require mirazmac/dotenvwriter
$writer = new \MirazMac\DotEnv\Writer(__DIR__ . '/' . '.env');
$writer
->set('APP_NAME', 'My App')
->set('APP_URL', 'https://laravel.com')
->set('APP_DIR', '${BASE_DIR}/app')
// Third parameter set to TRUe to force quote a single word value
->set('APP_BUCKET', 's3-bucket', true);
// Write the values to file
$writer->write();
`__construct(string $path)
`Constructs a new instance.
Arguments
string `$path
` The environment file path
` set(string $key, string $value, bool $forceQuote = false) : self
`Set the value of an environment variable, updated if exists, added if doesn't. The values must be passed as string, even if you are setting values like `TRUE
`
FALSE``.
This method is chainable.
Arguments
string `$key
` The key
string `$value
` The value
bool `$forceQuote
` By default the whether the value is wrapped in double quotes is determined automatically. However, you may wish to force quote a value.
` delete(string $key) : self
`Delete an environment variable if present. This method is chainable.
Arguments
string `$key
` The key
`hasChanged() : bool
`States if one or more values has changed
`getContent() : string
`Returns the current content
`write(bool $force = false) : bool
`Write the contents to the `.env
` file.
Arguments
bool `$force
` By default we only write when something has changed, but you can force to write the file by setting this to TRUE.
`DotEnvWriter
is a PHP library that can write (add, update, delete) values to
.env
` files.
Well.. I'm glad you asked.. it uses some bleeding edge AI algorithm.. just kidding, RegEx.. it uses RegEx to replace the values with proper quoting and escaping and writes them back to the filesystem. While RegEx is not the best solution but it gets the job done.
Well, it shouldn't. It uses proper quoting and escaping before writing the values. If you want to make sure it generates a valid file, well, that's why tests are for. You can clone the repo and run `composer install
and then
phpunit
` to run the tests.
The goal of this library is to generate a `.env
` file that can be parsed without any errors using: vlucas/phpdotenv
`.env
` files?It doesn't. You see, it doesn't concern itself with validation of existing values. It simply replaces them via RegEx, and makes sure the changes that it has made is valid. But if you have an existing invalid file, it can do nothing to validate or fix that.
Yes, you can. But you really shouldn't. Thus they aren't documented. This library is for writing to the file. To properly read and load values from `.env
` files, use something robust like: vlucas/phpdotenv
.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
The library is inspired by and uses some RegEx from the msztorc/laravel-env package and is based on the same MIT license.
Files (9) |
File | Role | Description | ||
---|---|---|---|---|
src (1 file) | ||||
tests (2 files) | ||||
usage (2 files) | ||||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files (9) | / | tests |
File | Role | Description |
---|---|---|
.env.example | Data | Auxiliary data |
WriterTest.php | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.