2011-12-22 03:17:41 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Core\Config;
|
|
|
|
|
2017-02-22 04:12:46 +01:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use SilverStripe\Config\Collections\ConfigCollectionInterface;
|
|
|
|
use SilverStripe\Config\Collections\MutableConfigCollectionInterface;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2017-02-22 04:12:46 +01:00
|
|
|
abstract class Config
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
// -- Source options bitmask --
|
|
|
|
|
|
|
|
/**
|
|
|
|
* source options bitmask value - only get configuration set for this
|
|
|
|
* specific class, not any of it's parents.
|
|
|
|
*
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
const UNINHERITED = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const source options bitmask value - do not use additional statics
|
|
|
|
* sources (such as extension)
|
|
|
|
*/
|
|
|
|
const EXCLUDE_EXTRA_SOURCES = 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current active Config instance.
|
|
|
|
*
|
|
|
|
* In general use you will use this method to obtain the current Config
|
2017-02-22 04:12:46 +01:00
|
|
|
* instance. It assumes the config instance has already been set.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-02-22 04:12:46 +01:00
|
|
|
* @return ConfigCollectionInterface
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function inst()
|
|
|
|
{
|
2017-02-22 04:12:46 +01:00
|
|
|
return ConfigLoader::instance()->getManifest();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-22 04:12:46 +01:00
|
|
|
* Make this config available to be modified
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-02-22 04:12:46 +01:00
|
|
|
* @return MutableConfigCollectionInterface
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-02-22 04:12:46 +01:00
|
|
|
public static function modify()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-02-22 04:12:46 +01:00
|
|
|
$instance = static::inst();
|
|
|
|
if ($instance instanceof MutableConfigCollectionInterface) {
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default nested configs should become mutable
|
|
|
|
$instance = static::nest();
|
|
|
|
if ($instance instanceof MutableConfigCollectionInterface) {
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidArgumentException("Nested config could not be made mutable");
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the newly active {@link Config} be a copy of the current active
|
|
|
|
* {@link Config} instance.
|
|
|
|
*
|
|
|
|
* You can then make changes to the configuration by calling update and
|
|
|
|
* remove on the new value returned by {@link Config::inst()}, and then discard
|
|
|
|
* those changes later by calling unnest.
|
|
|
|
*
|
2017-02-22 04:12:46 +01:00
|
|
|
* @return ConfigCollectionInterface Active config
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function nest()
|
|
|
|
{
|
2017-02-22 04:12:46 +01:00
|
|
|
// Clone current config and nest
|
|
|
|
$new = self::inst()->nest();
|
|
|
|
ConfigLoader::instance()->pushManifest($new);
|
|
|
|
return $new;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the active Config back to the Config instance the current active
|
|
|
|
* Config object was copied from.
|
|
|
|
*
|
2017-02-22 04:12:46 +01:00
|
|
|
* @return ConfigCollectionInterface
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function unnest()
|
|
|
|
{
|
2017-02-22 04:12:46 +01:00
|
|
|
// Unnest unless we would be left at 0 manifests
|
|
|
|
$loader = ConfigLoader::instance();
|
|
|
|
if ($loader->countManifests() < 2) {
|
2016-11-29 00:31:16 +01:00
|
|
|
user_error(
|
|
|
|
"Unable to unnest root Config, please make sure you don't have mis-matched nest/unnest",
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
2017-02-22 04:12:46 +01:00
|
|
|
} else {
|
|
|
|
$loader->popManifest();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-02-22 04:12:46 +01:00
|
|
|
return static::inst();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an accessor that returns results by class by default.
|
|
|
|
*
|
|
|
|
* Shouldn't be overridden, since there might be many Config_ForClass instances already held in the wild. Each
|
|
|
|
* Config_ForClass instance asks the current_instance of Config for the actual result, so override that instead
|
|
|
|
*
|
2017-02-22 04:12:46 +01:00
|
|
|
* @param string $class
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return Config_ForClass
|
|
|
|
*/
|
2017-02-22 04:12:46 +01:00
|
|
|
public static function forClass($class)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-02-22 04:12:46 +01:00
|
|
|
return new Config_ForClass($class);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2011-12-22 03:17:41 +01:00
|
|
|
}
|