|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/configuration |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Configuration; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Configuration\Common\PriorityList; |
|
13
|
|
|
use Slick\Configuration\Driver\CommonDriverMethods; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* PriorityConfigurationChain |
|
17
|
|
|
* |
|
18
|
|
|
* @package Slick\Configuration |
|
19
|
|
|
*/ |
|
20
|
|
|
class PriorityConfigurationChain implements ConfigurationChainInterface |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var PriorityList|ConfigurationInterface[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $priorityList; |
|
27
|
|
|
|
|
28
|
|
|
use CommonDriverMethods; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Creates a Priority Configuration Chain |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->priorityList = new PriorityList(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns the value store with provided key or the default value. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $key The key used to store the value in configuration |
|
42
|
|
|
* @param mixed $default The default value if no value was stored. |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed The stored value or the default value if key |
|
45
|
|
|
* was not found. |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function get($key, $default = null) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$stored = static::getValue($key, false, $this->data); |
|
50
|
|
|
if ($stored !== false) { |
|
51
|
|
|
return $stored; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
foreach ($this->priorityList as $driver) { |
|
55
|
|
|
$value = $driver->get($key, false); |
|
56
|
|
|
if ($value !== false) { |
|
57
|
|
|
$default = $value; |
|
58
|
|
|
static::setValue($key, $value, $this->data); |
|
59
|
|
|
break; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
return $default; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Add a configuration driver to the chain |
|
67
|
|
|
* |
|
68
|
|
|
* The configuration driver will be placed according to its priority. |
|
69
|
|
|
* Highest priority will be verified first |
|
70
|
|
|
* |
|
71
|
|
|
* @param ConfigurationInterface $config |
|
72
|
|
|
* @param integer $priority |
|
73
|
|
|
* |
|
74
|
|
|
* @return ConfigurationChainInterface self |
|
75
|
|
|
*/ |
|
76
|
|
|
public function add(ConfigurationInterface $config, $priority = 0) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->priorityList->insert($config, $priority); |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the internal configuration driver chain |
|
84
|
|
|
* |
|
85
|
|
|
* @return ConfigurationInterface[]|PriorityList |
|
86
|
|
|
*/ |
|
87
|
|
|
public function priorityList() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->priorityList; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.