Completed
Push — master ( 60795c...e6d481 )
by Greg
01:41
created

ConfigOverlay::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
c 1
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Consolidation\Config;
3
4
/**
5
 * Overlay different configuration objects that implement ConfigInterface
6
 * to make a priority-based, merged configuration object.
7
 *
8
 * Note that using a ConfigOverlay hides the defaults stored in each
9
 * individual configuration context. When using overlays, always call
10
 * getDefault / setDefault on the ConfigOverlay object itself.
11
 */
12
class ConfigOverlay implements ConfigInterface
13
{
14
    protected $contexts = [];
15
16
    const DEFAULT_CONTEXT = 'default';
17
    const PROCESS_CONTEXT = 'process';
18
19
    public function __construct()
20
    {
21
        $this->contexts[self::DEFAULT_CONTEXT] = new Config();
22
        $this->contexts[self::PROCESS_CONTEXT] = new Config();
23
    }
24
25
    /**
26
     * Add a named configuration object to the configuration overlay.
27
     * Configuration objects added LAST have HIGHEST priority, with the
28
     * exception of the fact that the process context always has the
29
     * highest priority.
30
     *
31
     * If a context has already been added, its priority will not change.
32
     */
33
    public function addContext($name, ConfigInterface $config)
34
    {
35
        $process = $this->contexts[self::PROCESS_CONTEXT];
36
        unset($this->contexts[self::PROCESS_CONTEXT]);
37
        $this->contexts[$name] = $config;
38
        $this->contexts[self::PROCESS_CONTEXT] = $process;
39
40
        return $this;
41
    }
42
43
    /**
44
     * Add a placeholder context that will be prioritized higher than
45
     * existing contexts. This is done to ensure that contexts added
46
     * later will maintain a higher priority if the placeholder context
47
     * is later relaced with a different configuration set via addContext().
48
     *
49
     * @param string $name
50
     * @return $this
51
     */
52
    public function addPlaceholder($name)
53
    {
54
        return $this->addContext($name, new Config());
55
    }
56
57
    /**
58
     * Increase the priority of the named context such that it is higher
59
     * in priority than any existing context except for the 'process'
60
     * context.
61
     *
62
     * @param string $name
63
     * @return $this
64
     */
65
    public function increasePriority($name)
66
    {
67
        $config = $this->getContext($name);
68
        unset($this->contexts[$name]);
69
        return $this->addContext($name, $config);
70
    }
71
72
    public function hasContext($name)
73
    {
74
        return isset($this->contexts[$name]);
75
    }
76
77
    public function getContext($name)
78
    {
79
        if ($this->hasContext($name)) {
80
            return $this->contexts[$name];
81
        }
82
        return new Config();
83
    }
84
85
    public function removeContext($name)
86
    {
87
        unset($this->contexts[$name]);
88
    }
89
90
    /**
91
     * Determine if a non-default config value exists.
92
     */
93
    public function findContext($key)
94
    {
95
        foreach (array_reverse($this->contexts) as $name => $config) {
96
            if ($config->has($key)) {
97
                return $config;
98
            }
99
        }
100
        return false;
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function has($key)
107
    {
108
        return $this->findContext($key) != false;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function get($key, $default = null)
115
    {
116
        $context = $this->findContext($key);
117
        if ($context) {
118
            return $context->get($key, $default);
119
        }
120
        return $default;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function set($key, $value)
127
    {
128
        $this->contexts[self::PROCESS_CONTEXT]->set($key, $value);
129
        return $this;
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function import($data)
136
    {
137
        throw new \Exception('The method "import" is not supported for the ConfigOverlay class.');
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function export()
144
    {
145
        $export = [];
146
        foreach ($this->contexts as $name => $config) {
147
            $export = array_merge_recursive($export, $config->export());
148
        }
149
        return $export;
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155
    public function hasDefault($key)
156
    {
157
        return $this->contexts[self::DEFAULT_CONTEXT]->has($key);
158
    }
159
160
    /**
161
     * @inheritdoc
162
     */
163
    public function getDefault($key, $default = null)
164
    {
165
        return $this->contexts[self::DEFAULT_CONTEXT]->get($key, $default);
166
    }
167
168
    /**
169
     * @inheritdoc
170
     */
171
    public function setDefault($key, $value)
172
    {
173
        $this->contexts[self::DEFAULT_CONTEXT]->set($key, $value);
174
        return $this;
175
    }
176
}
177