From 024a0b90a9af988422f103eca53b94e587aa1052 Mon Sep 17 00:00:00 2001 From: Hamish Friedlander Date: Wed, 27 Feb 2013 13:27:40 +1300 Subject: [PATCH] Add ability to create temporary Config copies --- core/Config.php | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/core/Config.php b/core/Config.php index 95a771550..6cc5aed21 100644 --- a/core/Config.php +++ b/core/Config.php @@ -163,16 +163,44 @@ class Config { $_SINGLETONS['Config'] = $instance; } + /** + * Make the newly active Config be a copy of the current active Config instance. + * + * You can then make changes to the configuration by calling update and remove on the new + * value returned by Config::inst(), and then discard those changes later by calling unnest + */ + static public function nest() { + $current = self::$instance; + + $new = clone $current; + $new->nestedFrom = $current; + self::set_instance($new); + } + + /** + * Change the active Config back to the Config instance the current active Config object + * was copied from + */ + static public function unnest() { + self::set_instance(self::$instance->nestedFrom); + } + protected $cache; /** - * Empty construction, otherwise calling singleton('Config') (not the right way to get the current active config - * instance, but people might) gives an error + * Each copy of the Config object need's it's own cache, so changes don't leak through to other instances */ public function __construct() { $this->cache = new Config_LRU(); } + public function __clone() { + $this->cache = clone $this->cache; + } + + /** @var Config - The config instance this one was copied from when Config::nest() was called */ + protected $nestedFrom = null; + /** @var [array] - Array of arrays. Each member is an nested array keyed as $class => $name => $value, * where value is a config value to treat as the highest priority item */ protected $overrides = array();