Skip to content

Commit

Permalink
Provides ways to ignore configuration keys (and functions entirely) #11
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Feb 9, 2022
1 parent f821333 commit 63f407c
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Analyzers/ConfigAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class ConfigAnalyzer
*/
private $currentValues = [];

/**
* Indicates if function calls should be ignored.
*
* @var bool
*/
private $ignoreFunctions = false;

public function __construct()
{
$this->functionHandler = new FunctionHandler();
Expand All @@ -140,6 +147,16 @@ public function __construct()
$this->parser = new Php7($this->lexer);
}

/**
* Sets if function calls should be ignored.
*
* @param bool $ignoreFunctions
*/
public function setIgnoreFunctions($ignoreFunctions)
{
$this->ignoreFunctions = $ignoreFunctions;
}

/**
* Sets the current configuration values.
*
Expand Down Expand Up @@ -342,6 +359,10 @@ public function replaceNodeValue($key, Node $node, $completeReplace = false)
*/
private function shouldProceedWithFunctionRewrite($key, $checkValue)
{
if ($this->ignoreFunctions) {
return false;
}

if ($checkValue === null) {
return true;
}
Expand Down
47 changes: 47 additions & 0 deletions src/ConfigUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,54 @@ class ConfigUpdater
*/
private $transformer = null;

/**
* Specifies whether function calls should be ignored when updating configuration files.
*
* @var bool
*/
private $ignoreFunctions = false;

/**
* A list of configuration keys that should be preserved.
* @var array
*/
private $preserveKeys = [];

public function __construct()
{
$this->transformer = new Transformer();
$this->arrayAnalyzer = new ArrayAnalyzer();
$this->analyzer = new ConfigAnalyzer();
}

/**
* Sets whether function calls are ignored when updating the configuration.
*
* @param bool $ignore
* @return $this
*/
public function setIgnoreFunctions($ignore)
{
$this->ignoreFunctions = $ignore;

$this->analyzer->setIgnoreFunctions($ignore);

return $this;
}

/**
* Sets a list of configuration keys that should always be preserved.
*
* @param array $keys
* @return $this
*/
public function setPreserveKeys($keys)
{
$this->preserveKeys = $keys;

return $this;
}

/**
* Attempts to remove the key and its value from the configuration.
*
Expand Down Expand Up @@ -135,6 +176,12 @@ public function replaceStructure($key, $newKey, $value, $docBlock, $forceNewLine
*/
public function update(array $changes)
{
if (! empty($this->preserveKeys)) {
foreach ($this->preserveKeys as $keyToPreserve) {
unset($changes[$keyToPreserve]);
}
}

$changesToMake = $this->arrayAnalyzer->getChanges($changes);

foreach ($changesToMake->insertions as $insert) {
Expand Down
15 changes: 15 additions & 0 deletions src/Contracts/ConfigWriterContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public function getFile($key);
*/
public function guard($entry);

/**
* Indicates that all function calls should be ignored when updating configuration files.
*
* @return ConfigWriterContract
*/
public function ignoreFunctionCalls();

/**
* Sets a list of configuration keys that should always be preserved.
*
* @param array $config
* @return mixed
*/
public function preserve($config);

/**
* Attempts to update the source configuration and returns the modified document.
*
Expand Down
40 changes: 40 additions & 0 deletions src/LaravelConfigWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ class LaravelConfigWriter implements ConfigWriterContract
*/
protected $functionWriter = null;

/**
* Specifies whether function calls should be ignored when updating configuration files.
*
* @var bool
*/
protected $ignoreFunctions = false;

/**
* A list of configuration keys that should be preserved.
* @var array
*/
protected $preserveKeys = [];

public function __construct(Application $app, Repository $configRepo)
{
$this->app = $app;
Expand Down Expand Up @@ -237,6 +250,31 @@ public function edit($namespace)
return new ConfigUpdateWrapper($this, $namespace);
}

/**
* Sets a list of configuration items that will never be updated.
*
* @param array $config The configuration keys to preserve.
* @return $this
*/
public function preserve($config)
{
$this->preserveKeys = $config;

return $this;
}

/**
* Will indicate that all function calls should be ignored when updating the configuration file.
*
* @return $this
*/
public function ignoreFunctionCalls()
{
$this->ignoreFunctions = true;

return $this;
}

/**
* Attempts to apply multiple changes to a configuration namespace.
*
Expand Down Expand Up @@ -317,6 +355,8 @@ protected function getChanges($file, $namespace, array $changes)
}

$configUpdater = new ConfigUpdater();
$configUpdater->setIgnoreFunctions($this->ignoreFunctions)
->setPreserveKeys($this->preserveKeys);
$configUpdater->open($file);
$configUpdater->update($changes);

Expand Down
2 changes: 2 additions & 0 deletions src/Support/Facades/ConfigWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @method static void guard($entry)
* @method static string preview($key, $value)
* @method static string previewMany($configNamespace, array $values)
* @method static ConfigWriterContract ignoreFunctionCalls()
* @method static ConfigWriterContract preserve($config)
* @method static ConfigUpdateWrapper edit($namespace)
* @method static FunctionWriter f()
*
Expand Down
46 changes: 46 additions & 0 deletions tests/IgnoreConfigurationItemsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

require_once 'ProteusTestCase.php';

use Stillat\Proteus\ConfigUpdater;
use Stillat\Proteus\Document\Transformer;

class IgnoreConfigurationItemsTest extends ProteusTestCase
{
public function testThatFunctionCallsWereIgnored()
{
$updater = new ConfigUpdater();
$updater->setIgnoreFunctions(true);
$updater->open(__DIR__ . '/configs/withenv.php');
$expected = Transformer::normalizeLineEndings(file_get_contents(__DIR__ . '/expected/withenv.php'));
$updater->update([
'some_key' => 'new-value',
'nested.key.value' => 'inserted-value'
]);

$doc = $updater->getDocument();

$this->assertEquals($expected, $doc);
}

public function testConfigurationItemsCanBeIgnored()
{
$updater = new ConfigUpdater();
$updater->setPreserveKeys([
'some_key',
'nested.key.value'
]);

$updater->open(__DIR__ . '/configs/withenv.php');
$expected = Transformer::normalizeLineEndings(file_get_contents(__DIR__ . '/expected/withenv_preserve.php'));
$updater->update([
'some_key' => 'new-value',
'nested.key.value' => 'inserted-value',
'nested.key.append' => 'Hello, universe!'
]);

$doc = $updater->getDocument();

$this->assertEquals($expected, $doc);
}
}
12 changes: 12 additions & 0 deletions tests/configs/withenv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'some_key' => env('SOME_ENV_KEY', false),

'nested' => [
'key' => [
'value' => 'test'
],
],

];
12 changes: 12 additions & 0 deletions tests/expected/withenv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'some_key' => env('SOME_ENV_KEY', false),

'nested' => [
'key' => [
'value' => 'inserted-value'
],
],

];
12 changes: 12 additions & 0 deletions tests/expected/withenv_preserve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'some_key' => env('SOME_ENV_KEY', false),

'nested' => [
'key' => [
'value' => 'test', 'append' => 'Hello, universe!'
],
],

];

0 comments on commit 63f407c

Please sign in to comment.