|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Core\Config; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use SilverStripe\Config\Collections\ConfigCollectionInterface; |
|
7
|
|
|
use SilverStripe\Config\Collections\MutableConfigCollectionInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class Config |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
// -- Source options bitmask -- |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* source options bitmask value - only get configuration set for this |
|
16
|
|
|
* specific class, not any of it's parents. |
|
17
|
|
|
* |
|
18
|
|
|
* @const |
|
19
|
|
|
*/ |
|
20
|
|
|
const UNINHERITED = 1; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @const source options bitmask value - do not use additional statics |
|
24
|
|
|
* sources (such as extension) |
|
25
|
|
|
*/ |
|
26
|
|
|
const EXCLUDE_EXTRA_SOURCES = 4; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get the current active Config instance. |
|
30
|
|
|
* |
|
31
|
|
|
* In general use you will use this method to obtain the current Config |
|
32
|
|
|
* instance. It assumes the config instance has already been set. |
|
33
|
|
|
* |
|
34
|
|
|
* @return ConfigCollectionInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function inst() |
|
37
|
|
|
{ |
|
38
|
|
|
return ConfigLoader::instance()->getManifest(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Make this config available to be modified |
|
43
|
|
|
* |
|
44
|
|
|
* @return MutableConfigCollectionInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function modify() |
|
47
|
|
|
{ |
|
48
|
|
|
$instance = static::inst(); |
|
49
|
|
|
if ($instance instanceof MutableConfigCollectionInterface) { |
|
50
|
|
|
return $instance; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// By default nested configs should become mutable |
|
54
|
|
|
$instance = static::nest(); |
|
55
|
|
|
if ($instance instanceof MutableConfigCollectionInterface) { |
|
56
|
|
|
return $instance; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
throw new InvalidArgumentException("Nested config could not be made mutable"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Make the newly active {@link Config} be a copy of the current active |
|
64
|
|
|
* {@link Config} instance. |
|
65
|
|
|
* |
|
66
|
|
|
* You can then make changes to the configuration by calling update and |
|
67
|
|
|
* remove on the new value returned by {@link Config::inst()}, and then discard |
|
68
|
|
|
* those changes later by calling unnest. |
|
69
|
|
|
* |
|
70
|
|
|
* @return ConfigCollectionInterface Active config |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function nest() |
|
73
|
|
|
{ |
|
74
|
|
|
// Clone current config and nest |
|
75
|
|
|
$new = self::inst()->nest(); |
|
76
|
|
|
ConfigLoader::instance()->pushManifest($new); |
|
77
|
|
|
return $new; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Change the active Config back to the Config instance the current active |
|
82
|
|
|
* Config object was copied from. |
|
83
|
|
|
* |
|
84
|
|
|
* @return ConfigCollectionInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function unnest() |
|
87
|
|
|
{ |
|
88
|
|
|
// Unnest unless we would be left at 0 manifests |
|
89
|
|
|
$loader = ConfigLoader::instance(); |
|
90
|
|
|
if ($loader->countManifests() < 2) { |
|
91
|
|
|
user_error( |
|
92
|
|
|
"Unable to unnest root Config, please make sure you don't have mis-matched nest/unnest", |
|
93
|
|
|
E_USER_WARNING |
|
94
|
|
|
); |
|
95
|
|
|
} else { |
|
96
|
|
|
$loader->popManifest(); |
|
97
|
|
|
} |
|
98
|
|
|
return static::inst(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get an accessor that returns results by class by default. |
|
103
|
|
|
* |
|
104
|
|
|
* Shouldn't be overridden, since there might be many Config_ForClass instances already held in the wild. Each |
|
105
|
|
|
* Config_ForClass instance asks the current_instance of Config for the actual result, so override that instead |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $class |
|
108
|
|
|
* @return Config_ForClass |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function forClass($class) |
|
111
|
|
|
{ |
|
112
|
|
|
return new Config_ForClass($class); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|