Skip to content

Commit 4699e9b

Browse files
authored
Merge pull request #17 from tekreme73/master
Update for Laravel 5.4 and greater and Lumen support
2 parents d6ce7af + f0de699 commit 4699e9b

File tree

10 files changed

+197
-116
lines changed

10 files changed

+197
-116
lines changed

README.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Laravel Config Writer
1+
# Laravel Config Writer
22

33
Write to Laravel Config files and maintain file integrity.
44

@@ -8,26 +8,50 @@ You can rewrite array values inside a basic configuration file that returns a si
88

99
The following value types are supported for writing: strings, integers, booleans and single-dimension arrays.
1010

11-
### Usage Instructions
11+
Fork of `octobercms\laravel-config-writer`.
12+
13+
## Support
14+
15+
This provider is designed to be used in Laravel from `5.4` version.
16+
17+
## Setup
18+
19+
Install through composer:
20+
```
21+
composer require "tekreme73/laravel-config-writer"
22+
```
1223

1324
Add this to `app/config/app.php` under the 'providers' key:
1425

1526
```php
16-
'October\Rain\Config\ConfigServiceProvider',
27+
Tekreme73\Laravel\ConfigWriter\ServiceProvider::class,
28+
```
29+
30+
### Lumen case
31+
32+
Add this to `bootstrap/app.php` in the 'service providers' section declaration:
33+
34+
```php
35+
$app->register(Tekreme73\Laravel\ConfigWriter\ServiceProvider::class);
1736
```
1837

19-
You can now write to config files:
38+
## Usage
39+
40+
You can write to config files like this:
2041

2142
```php
22-
Config::write('app.url', 'http://octobercms.com');
43+
Config::write('app.url', 'http://domain.com');
44+
45+
app('config')->write('app.url', 'http://domain.com');
2346
```
2447

25-
### Usage outside Laravel
48+
49+
### Outside Laravel
2650

2751
The `Rewrite` class can be used anywhere.
2852

2953
```php
30-
$writeConfig = new October\Rain\Config\Rewrite;
54+
$writeConfig = new Tekreme73\Laravel\ConfigWriter\DataWriter\Rewrite;
3155
$writeConfig->toFile('path/to/config.php', [
3256
'item' => 'new value',
3357
'nested.config.item' => 'value',

composer.json

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
{
2-
"name": "october/config",
3-
"description": "Configuration extension",
4-
"homepage": "http://octobercms.com",
5-
"keywords": ["october cms", "october", "laravel", "config", "write"],
2+
"name": "tekreme73/laravel-config-writer",
3+
"description": "Laravel provider to be able to rewrite configuration",
4+
"keywords": ["laravel", "config", "write", "provider"],
65
"license": "MIT",
76
"authors": [
87
{
9-
"name": "Alexey Bobkov",
10-
"email": "[email protected]"
11-
},
12-
{
13-
"name": "Samuel Georges",
14-
"email": "[email protected]"
8+
"name": "Rémi Rebillard",
9+
"email": "[email protected]"
1510
}
1611
],
1712
"require": {
18-
"php": ">=5.4.0",
13+
"php": ">=7.1.0",
1914
"illuminate/config": "~5.0."
2015
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^7.0"
18+
},
2119
"autoload": {
2220
"psr-4": {
23-
"October\\Rain\\Config\\": "src/"
21+
"Tekreme73\\Laravel\\ConfigWriter\\": "src/"
2422
}
2523
},
26-
"minimum-stability": "dev"
24+
"minimum-stability": "dev",
25+
"prefer-stable": true
2726
}

src/ConfigServiceProvider.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/FileWriter.php renamed to src/DataWriter/FileWriter.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
<?php namespace October\Rain\Config;
1+
<?php
2+
3+
namespace Tekreme73\Laravel\ConfigWriter\DataWriter;
24

35
use Illuminate\Filesystem\Filesystem;
6+
use Tekreme73\Laravel\ConfigWriter\DataWriter\Rewrite;
47

58
class FileWriter
69
{
@@ -21,48 +24,56 @@ class FileWriter
2124
/**
2225
* The config rewriter object.
2326
*
24-
* @var \October\Rain\Config\Rewrite
27+
* @var \Tekreme73\Laravel\ConfigWriter\DataWriter\Rewrite
2528
*/
2629
protected $rewriter;
2730

2831
/**
2932
* Create a new file configuration loader.
3033
*
31-
* @param \Illuminate\Filesystem\Filesystem $files
32-
* @param string $defaultPath
34+
* @param \Illuminate\Filesystem\Filesystem $files
35+
* @param string $defaultPath
3336
* @return void
3437
*/
35-
public function __construct(Filesystem $files, $defaultPath)
38+
public function __construct(Filesystem $files, string $defaultPath)
3639
{
3740
$this->files = $files;
3841
$this->defaultPath = $defaultPath;
3942
$this->rewriter = new Rewrite;
4043
}
4144

42-
public function write($item, $value, $filename)
45+
/**
46+
* Write an item value in a file.
47+
*
48+
* @param string $item
49+
* @param mixed $value
50+
* @param string $filename
51+
* @return bool
52+
*/
53+
public function write(string $item, $value, string $filename, string $fileExtension = '.php'): bool
4354
{
44-
$path = $this->getPath($item, $filename);
45-
if (!$path)
46-
return false;
55+
$path = $this->getPath($item, $filename, $fileExtension);
56+
57+
if (!$path) return false;
4758

4859
$contents = $this->files->get($path);
4960
$contents = $this->rewriter->toContent($contents, [$item => $value]);
5061

5162
return !($this->files->put($path, $contents) === false);
5263
}
5364

54-
private function getPath($item, $filename)
65+
private function getPath(string $item, string $filename, string $ext = '.php'): string
5566
{
56-
$file = "{$this->defaultPath}/{$filename}.php";
57-
if ( $this->files->exists($file) &&
58-
$this->hasKey($file, $item)
59-
)
67+
$file = "{$this->defaultPath}/{$filename}{$ext}";
68+
69+
if ($this->files->exists($file) && $this->hasKey($file, $item)) {
6070
return $file;
71+
}
6172

6273
return null;
6374
}
6475

65-
private function hasKey($path, $key)
76+
private function hasKey(string $path, string $key): bool
6677
{
6778
$contents = file_get_contents($path);
6879
$vars = eval('?>'.$contents);
@@ -72,7 +83,7 @@ private function hasKey($path, $key)
7283
$isset = false;
7384
while ($key = array_shift($keys)) {
7485
$isset = isset($vars[$key]);
75-
if (is_array($vars[$key])) $vars = $vars[$key]; // Go down the rabbit hole
86+
if (is_array($vars[$key])) $vars = $vars[$key];
7687
}
7788

7889
return $isset;

src/Rewrite.php renamed to src/DataWriter/Rewrite.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace October\Rain\Config;
1+
<?php
2+
3+
namespace Tekreme73\Laravel\ConfigWriter\DataWriter;
24

35
use Exception;
46

@@ -26,15 +28,16 @@
2628
class Rewrite
2729
{
2830

29-
public function toFile($filePath, $newValues, $useValidation = true)
31+
public function toFile(string $filePath, array $newValues, bool $useValidation = true): string
3032
{
3133
$contents = file_get_contents($filePath);
3234
$contents = $this->toContent($contents, $newValues, $useValidation);
3335
file_put_contents($filePath, $contents);
36+
3437
return $contents;
3538
}
3639

37-
public function toContent($contents, $newValues, $useValidation = true)
40+
public function toContent(string $contents, array $newValues, bool $useValidation = true): string
3841
{
3942
$contents = $this->parseContent($contents, $newValues);
4043

@@ -65,7 +68,7 @@ public function toContent($contents, $newValues, $useValidation = true)
6568
return $contents;
6669
}
6770

68-
protected function parseContent($contents, $newValues)
71+
protected function parseContent(string $contents, array $newValues): string
6972
{
7073
$result = $contents;
7174

@@ -76,22 +79,22 @@ protected function parseContent($contents, $newValues)
7679
return $result;
7780
}
7881

79-
protected function parseContentValue($contents, $path, $value)
82+
protected function parseContentValue(string $contents, string $path, $value): string
8083
{
8184
$result = $contents;
8285
$items = explode('.', $path);
8386
$key = array_pop($items);
8487
$replaceValue = $this->writeValueToPhp($value);
8588

8689
$count = 0;
87-
$patterns = array();
90+
$patterns = [];
8891
$patterns[] = $this->buildStringExpression($key, $items);
8992
$patterns[] = $this->buildStringExpression($key, $items, '"');
9093
$patterns[] = $this->buildConstantExpression($key, $items);
9194
$patterns[] = $this->buildArrayExpression($key, $items);
9295

9396
foreach ($patterns as $pattern) {
94-
$result = preg_replace($pattern, '${1}${2}'.$replaceValue, $result, 1, $count);
97+
$result = preg_replace($pattern, '${1}${2}' . $replaceValue, $result, 1, $count);
9598

9699
if ($count > 0) {
97100
break;
@@ -101,7 +104,7 @@ protected function parseContentValue($contents, $path, $value)
101104
return $result;
102105
}
103106

104-
protected function writeValueToPhp($value)
107+
protected function writeValueToPhp($value): string
105108
{
106109
if (is_string($value) && strpos($value, "'") === false) {
107110
$replaceValue = "'".$value."'";
@@ -127,7 +130,7 @@ protected function writeValueToPhp($value)
127130
return $replaceValue;
128131
}
129132

130-
protected function writeArrayToPhp($array)
133+
protected function writeArrayToPhp(array $array): array
131134
{
132135
$result = [];
133136

@@ -142,9 +145,9 @@ protected function writeArrayToPhp($array)
142145
return $result;
143146
}
144147

145-
protected function buildStringExpression($targetKey, $arrayItems = array(), $quoteChar = "'")
148+
protected function buildStringExpression(string $targetKey, array $arrayItems = [], string $quoteChar = "'"): string
146149
{
147-
$expression = array();
150+
$expression = [];
148151

149152
// Opening expression for array items ($1)
150153
$expression[] = $this->buildArrayOpeningExpression($arrayItems);
@@ -164,9 +167,9 @@ protected function buildStringExpression($targetKey, $arrayItems = array(), $quo
164167
/**
165168
* Common constants only (true, false, null, integers)
166169
*/
167-
protected function buildConstantExpression($targetKey, $arrayItems = array())
170+
protected function buildConstantExpression(string $targetKey, array $arrayItems = []): string
168171
{
169-
$expression = array();
172+
$expression = [];
170173

171174
// Opening expression for array items ($1)
172175
$expression[] = $this->buildArrayOpeningExpression($arrayItems);
@@ -183,9 +186,9 @@ protected function buildConstantExpression($targetKey, $arrayItems = array())
183186
/**
184187
* Single level arrays only
185188
*/
186-
protected function buildArrayExpression($targetKey, $arrayItems = array())
189+
protected function buildArrayExpression(string $targetKey, array $arrayItems = []): string
187190
{
188-
$expression = array();
191+
$expression = [];
189192

190193
// Opening expression for array items ($1)
191194
$expression[] = $this->buildArrayOpeningExpression($arrayItems);
@@ -199,17 +202,17 @@ protected function buildArrayExpression($targetKey, $arrayItems = array())
199202
return '/' . implode('', $expression) . '/';
200203
}
201204

202-
protected function buildArrayOpeningExpression($arrayItems)
205+
protected function buildArrayOpeningExpression(array $arrayItems): string
203206
{
204207
if (count($arrayItems)) {
205-
$itemOpen = array();
208+
$itemOpen = [];
206209
foreach ($arrayItems as $item) {
207210
// The left hand array assignment
208211
$itemOpen[] = '[\'|"]'.$item.'[\'|"]\s*=>\s*(?:[aA][rR]{2}[aA][yY]\(|[\[])';
209212
}
210213

211214
// Capture all opening array (non greedy)
212-
$result = '(' . implode('[\s\S]*', $itemOpen) . '[\s\S]*?)';
215+
$result = '(' . implode('[\s\S]*?', $itemOpen) . '[\s\S]*?)';
213216
}
214217
else {
215218
// Gotta capture something for $1

src/LumenServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tekreme73\Laravel\ConfigWriter;
4+
5+
use Laravel\Lumen\Application;
6+
use Tekreme73\Laravel\ConfigWriter\ServiceProvider;
7+
8+
class LumenServiceProvider extends ServiceProvider
9+
{
10+
/** @var Application */
11+
protected $app;
12+
13+
protected function getConfigPath(): string
14+
{
15+
return $this->app->getConfigurationPath();
16+
}
17+
}

0 commit comments

Comments
 (0)