Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With config functionality added. #9005

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Core/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use InvalidArgumentException;
use SilverStripe\Config\Collections\ConfigCollectionInterface;
use SilverStripe\Config\Collections\DeltaConfigCollection;
use SilverStripe\Config\Collections\MutableConfigCollectionInterface;

abstract class Config
Expand Down Expand Up @@ -119,4 +118,23 @@ public static function forClass($class)
{
return new Config_ForClass($class);
}

/**
* Perform the given operation in an isolated config state.
* On return, the config state will be restored, so any modifications are temporary.
*
* @param callable $callback Callback to run. Will be passed the nested config state as a parameter
* @return mixed Result of callback
*/
public static function withConfig($callback)
{
static::nest();
$config = static::modify();

try {
return $callback($config);
} finally {
static::unnest();
}
}
}
41 changes: 41 additions & 0 deletions tests/php/Core/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Core\Tests\Config;

use SilverStripe\Config\Collections\MutableConfigCollectionInterface;
use SilverStripe\Config\MergeStrategy\Priority;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
Expand Down Expand Up @@ -322,4 +323,44 @@ public function testForClass()
$this->assertTrue(empty($config->bar));
$this->assertNull($config->bar);
}

public function testWithConfig()
{
$oldValue = 'test_1';
$newValue1 = 'new value 1';
$newValue2 = 'new value 2';
$property = 'third';

$this->assertEquals(
$oldValue,
Config::inst()->get(ConfigTest\First::class, $property)
);

Config::withConfig(function (MutableConfigCollectionInterface $config) use ($newValue1, $newValue2, $property) {
$config->set(ConfigTest\First::class, $property, $newValue1);

$this->assertEquals(
$newValue1,
Config::inst()->get(ConfigTest\First::class, $property)
);

$resultValue = Config::withConfig(function (MutableConfigCollectionInterface $config) use ($newValue2, $property) {
$config->set(ConfigTest\First::class, $property, $newValue2);

return Config::inst()->get(ConfigTest\First::class, $property);
});

$this->assertEquals($newValue2, $resultValue);

$this->assertEquals(
$newValue1,
Config::inst()->get(ConfigTest\First::class, $property)
);
});

$this->assertEquals(
$oldValue,
Config::inst()->get(ConfigTest\First::class, $property)
);
}
}