Skip to content

Commit

Permalink
update readme, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyMiracle committed Mar 16, 2017
1 parent 2ba6bbc commit 89e6adc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
/.idea
/vendor
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You can rewrite array values inside a basic configuration file that returns a si

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

Fork of `octobercms\laravel-config-writer`
Fork of `octobercms\laravel-config-writer`.

### Usage Instructions

Expand All @@ -20,23 +20,23 @@ composer require "sergeymiracle/config-writer"
Add this to `app/config/app.php` under the 'providers' key:

```php
'SergeyMiracle\Config\ConfigServiceProvider',
SergeyMiracle\Config\ConfigServiceProvider::class,
```

You can now write to config files:

```php
Config::write(['app.url' => 'http://domain.com']);

app('config')->write(['app.url' => 'http://octobercms.com']);
app('config')->write(['app.url' => 'http://domain.com']);
```

### Usage outside Laravel

The `Rewrite` class can be used anywhere.

```php
$writeConfig = new October\Rain\Config\Rewrite;
$writeConfig = new SergeyMiracle\Config\Rewrite;
$writeConfig->toFile('path/to/config.php', [
'item' => 'new value',
'nested.config.item' => 'value',
Expand Down
15 changes: 7 additions & 8 deletions src/FileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FileWriter
/**
* The config rewriter object.
*
* @var \October\Rain\Config\Rewrite
* @var \SergeyMiracle\Config\Rewrite
*/
protected $rewriter;

Expand All @@ -44,8 +44,8 @@ public function __construct(Filesystem $files, $defaultPath)
public function write($item, $value, $filename)
{
$path = $this->getPath($item, $filename);
if (!$path)
return false;

if (!$path) return false;

$contents = $this->files->get($path);
$contents = $this->rewriter->toContent($contents, [$item => $value]);
Expand All @@ -56,10 +56,10 @@ public function write($item, $value, $filename)
private function getPath($item, $filename)
{
$file = "{$this->defaultPath}/{$filename}.php";
if ($this->files->exists($file) &&
$this->hasKey($file, $item)
)

if ($this->files->exists($file) && $this->hasKey($file, $item)) {
return $file;
}

return null;
}
Expand All @@ -68,13 +68,12 @@ private function hasKey($path, $key)
{
$contents = file_get_contents($path);
$vars = eval('?>' . $contents);

$keys = explode('.', $key);

$isset = false;
while ($key = array_shift($keys)) {
$isset = isset($vars[$key]);
if (is_array($vars[$key])) $vars = $vars[$key]; // Go down the rabbit hole
if (is_array($vars[$key])) $vars = $vars[$key];
}

return $isset;
Expand Down
1 change: 1 addition & 0 deletions src/Rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function toFile($filePath, $newValues, $useValidation = true)
$contents = file_get_contents($filePath);
$contents = $this->toContent($contents, $newValues, $useValidation);
file_put_contents($filePath, $contents);

return $contents;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Config/RewriteTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use October\Rain\Config\Rewrite;
use SergeyMiracle\Config\Rewrite;

class RewriteTest extends TestCase
{
Expand Down Expand Up @@ -95,7 +95,7 @@ public function testToContent()
$this->assertArrayHasKey('memcached', $result);
$this->assertArrayHasKey('weight', $result['memcached']);
$this->assertFalse($result['memcached']['weight']);

$this->assertArrayHasKey('connections', $result);
$this->assertArrayHasKey('pgsql', $result['connections']);
$this->assertArrayHasKey('password', $result['connections']['pgsql']);
Expand All @@ -111,4 +111,4 @@ public function testToContent()
$this->assertEquals(69, $result['aNumber']);
}

}
}

0 comments on commit 89e6adc

Please sign in to comment.