Skip to content

Commit

Permalink
Adding updates from daftspunk configs
Browse files Browse the repository at this point in the history
  • Loading branch information
tnylea committed May 9, 2024
1 parent 41dba77 commit 3f21b3d
Show file tree
Hide file tree
Showing 41 changed files with 683 additions and 2,922 deletions.
Binary file added .DS_Store
Binary file not shown.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

162 changes: 27 additions & 135 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,168 +1,60 @@
# Laravel Config Writer

[![Version](https://img.shields.io/github/v/release/devdojo/config-writer?sort=semver&style=flat-square)](https://github.com/devdojo/config-writer/releases)
[![Tests](https://img.shields.io/github/actions/workflow/status/devdojo/config-writer/tests.yaml?&label=tests&style=flat-square)](https://github.com/devdojo/config-writer/actions)
[![License](https://img.shields.io/github/license/devdojo/config-writer?label=open%20source&style=flat-square)](https://packagist.org/packages/devdojo/config-writer)
[![Discord](https://img.shields.io/discord/816852513684193281?label=discord&style=flat-square)](https://discord.gg/D5MFSPH6Ux)
Write to Laravel Config files and maintain file integrity.

A utility to easily create and modify Laravel-style PHP configuration files and environment files whilst maintaining the formatting and comments contained within. This utility works by parsing the configuration files using the [PHP Parser library](https://github.com/nikic/php-parser) to convert the configuration into an abstract syntax tree, then carefully modifying the configuration values as required.
This library is an extension of the Config component used by Laravel. It adds the ability to write to configuration files.

## Installation
You can rewrite array values inside a basic configuration file that returns a single array definition (like a Laravel config file) whilst maintaining the file integrity, leaving comments and advanced settings intact.

```
composer require devdojo/config-writer
```

## Usage

### PHP array files

You can modify Laravel-style PHP configuration files - PHP files that return a single array - by using the `Devdojo\ConfigWriter\ArrayFile` class. Use the `open` method to open an existing file for modification, or to create a new config file.

```php
use Devdojo\ConfigWriter\ArrayFile;

$config = ArrayFile::open(base_path('config/app.php'));
```

You can set values using the `set` method. This method can be used fluently, or can be called with a single key and value or an array of keys and values.

```php
$config->set('name', 'Winter CMS');

$config
->set('locale', 'en_US')
->set('fallbackLocale', 'en');

$config->set([
'trustedHosts' => true,
'trustedProxies' => '*',
]);
```
The following value types are supported for writing: strings, integers, booleans and single-dimension arrays.

You can also set deep values in an array value by specifying the key in dot notation, or as a nested array.
## Support

```php
$config->set('connections.mysql.host', 'localhost');

$config->set([
'connections' => [
'sqlite' => [
'database' => 'database.sqlite',
'driver' => 'sqlite',
'foreign_key_constraints' => true,
'prefix' => '',
'url' => null,
],
],
]);
```
This provider is designed to be used in Laravel from `5.4` version.

To finalise all your changes, use the `write` method to write the changes to the open file.
## Setup

```php
$config->write();
Install through composer:
```

If desired, you may also write the changes to another file altogether.

```php
$config->write('path/to/newfile.php');
composer require "daftspunk/laravel-config-writer"
```

Or you can simply render the changes as a string.
Add this to `app/config/app.php` under the 'providers' key:

```php
$config->render();
October\Rain\Config\ServiceProvider::class,
```

#### Function calls as values
### Lumen case

Function calls can be added to your configuration file by using the `function` method. The first parameter of the `function` method defines the function to call, and the second parameter accepts an array of parameters to provide to the function.
Add this to `bootstrap/app.php` in the 'service providers' section declaration:

```php
$config->set('name', $config->function('env', ['APP_NAME', 'Winter CMS']));
$app->register(October\Rain\Config\ServiceProvider::class);
```

#### Constants as values

Constants can be added to your configuration file by using the `constant` method. The only parameter required is the name of the constant.

```php
$config->set('foo.bar', $config->constant('My\Class::CONSTANT'));
```

#### Sorting the configuration file
## Usage

You can sort the configuration keys alphabetically by using the `sort` method. This will sort all current configuration values.
You can write to config files like this:

```php
$config->sort();
```

By default, this will sort the keys alphabetically in ascending order. To sort in the opposite direction, include the `ArrayFile::SORT_DESC` parameter.
Config::write('app.url', 'http://domain.com');

```php
$config->sort(ArrayFile::SORT_DESC);
app('config')->write('app.url', 'http://domain.com');
```

### Environment files

This utility library also allows manipulation of environment files, typically found as `.env` files in a project. The `Devdojo\ConfigWriter\EnvFile::open()` method allows you to open or create an environment file for modification.

```php
use Devdojo\ConfigWriter\EnvFile;

$env = EnvFile::open(base_path('.env'));
```
### Outside Laravel

You can set values using the `set` method. This method can be used fluently, or can be called with a single key and value or an array of keys and values.
The `Rewrite` class can be used anywhere.

```php
$env->set('APP_NAME', 'Winter CMS');

$env
->set('APP_URL', 'https://wintercms.com')
->set('APP_ENV', 'production');

$env->set([
'DB_CONNECTION' => 'sqlite',
'DB_DATABASE' => 'database.sqlite',
$writeConfig = new October\Rain\Config\DataWriter\Rewrite;
$writeConfig->toFile('path/to/config.php', [
'item' => 'new value',
'nested.config.item' => 'value',
'arrayItem' => ['Single', 'Level', 'Array', 'Values'],
'numberItem' => 3,
'booleanItem' => true
]);
```

> **Note:** Arrays are not supported in environment files.
You can add an empty line into the environment file by using the `addEmptyLine` method. This allows you to separate groups of environment variables.

```php
$env->set('FOO', 'bar');
$env->addEmptyLine();
$env->set('BAR', 'foo');
```

To finalise all your changes, use the `write` method to write the changes to the open file.

```php
$env->write();
```

If desired, you may also write the changes to another file altogether.

```php
$env->write(base_path('.env.local'));
```

Or you can simply render the changes as a string.

```php
$env->render();
```

## License

This utility library is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

## Security vulnerabilities

Please review our [security policy](https://github.com/wintercms/winter/security/policy) on how to report security vulnerabilities.
42 changes: 24 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
{
"name": "devdojo/config-writer",
"description": "Utility to create and update Laravel config and .env files (forkd from WinterCMS)",
"type": "library",
"name": "daftspunk/laravel-config-writer",
"description": "Laravel provider to be able to rewrite configuration",
"homepage": "http://octobercms.com",
"keywords": ["october cms", "october", "laravel", "config", "write", "provider"],
"license": "MIT",
"authors": [
{
"name": "Tony Lea",
"email": "[email protected]",
"role": "Fork/Maintainer author"
"name": "Alexey Bobkov",
"email": "[email protected]"
},
{
"name": "Winter CMS Maintainers",
"homepage": "https://wintercms.com",
"role": "Maintainers"
"name": "Samuel Georges",
"email": "[email protected]"
}
],
"require": {
"php": "^7.4.0 || ^8.0",
"nikic/php-parser": "^5.0"
"php": ">=7.1.0",
"illuminate/config": "5.*|6.*|7.*|8.*|9.*|10.*|11.*|12.*|13.*|14.*|15.*|16.*|17.*|18.*|19.*|20.*"
},
"require-dev": {
"phpstan/phpstan": "^1.6",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^7.0",
"orchestra/testbench": "3.8"
},
"autoload": {
"psr-4": {
"Devdojo\\ConfigWriter\\": "src/"
"October\\Rain\\Config\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Devdojo\\ConfigWriter\\": "tests/"
"scripts": {
"test": "vendor/bin/phpunit"
},
"extra": {
"laravel": {
"providers": [
"October\\Rain\\Config\\ServiceProvider"
]
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
47 changes: 0 additions & 47 deletions phpcs.xml

This file was deleted.

5 changes: 0 additions & 5 deletions phpstan.neon

This file was deleted.

17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
28 changes: 0 additions & 28 deletions phpunit.xml.dist

This file was deleted.

Loading

0 comments on commit 3f21b3d

Please sign in to comment.