1 | <?php |
||
2 | namespace Fwolf\Config; |
||
3 | |||
4 | /** |
||
5 | * Trait for class use Config as property |
||
6 | * |
||
7 | * @see ConfigAwareInterface |
||
8 | * |
||
9 | * @copyright Copyright 2013-2017 Fwolf |
||
10 | * @license https://opensource.org/licenses/MIT MIT |
||
11 | */ |
||
12 | trait ConfigAwareTrait |
||
13 | { |
||
14 | /** |
||
15 | * Config instance |
||
16 | * |
||
17 | * This property used to named as $config, and as it implements |
||
18 | * ArrayAccess, it can used same style of array. When reform to trait, the |
||
19 | * constructor is removed, and loading of default configs also changed, from |
||
20 | * load in constructor, to trigger by getter of Config instance. If keep |
||
21 | * using Config instance directly, the loading can not be triggered. |
||
22 | * |
||
23 | * So, do NOT use this property to retrieve config value, use getConfig() |
||
24 | * instead, which will call Config instance getter and check/load default |
||
25 | * configs. |
||
26 | * |
||
27 | * @var Config |
||
28 | */ |
||
29 | protected $configInstance = null; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * @see ConfigAwareInterface::getConfig() |
||
34 | * |
||
35 | * @param string $key |
||
36 | * @return mixed |
||
37 | */ |
||
38 | public function getConfig($key) |
||
39 | { |
||
40 | $configInstance = $this->getConfigInstance(); |
||
41 | |||
42 | return $configInstance->get($key); |
||
43 | } |
||
44 | |||
45 | |||
46 | /** |
||
47 | * @return Config |
||
48 | */ |
||
49 | View Code Duplication | protected function getConfigInstance() |
|
0 ignored issues
–
show
|
|||
50 | { |
||
51 | if (is_null($this->configInstance)) { |
||
52 | $config = new Config; |
||
53 | |||
54 | $config->setMultiple($this->getDefaultConfigs()); |
||
55 | |||
56 | $this->configInstance = $config; |
||
57 | } |
||
58 | |||
59 | return $this->configInstance; |
||
60 | } |
||
61 | |||
62 | |||
63 | /** |
||
64 | * @see ConfigAwareInterface::getConfigs() |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getConfigs() |
||
69 | { |
||
70 | return $this->getConfigInstance()->getRaw(); |
||
71 | } |
||
72 | |||
73 | |||
74 | /** |
||
75 | * Get default configs |
||
76 | * |
||
77 | * Will be loaded when create config instance, child class can extend to add |
||
78 | * more default configs. |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | protected function getDefaultConfigs() |
||
83 | { |
||
84 | return []; |
||
85 | } |
||
86 | |||
87 | |||
88 | /** |
||
89 | * @see ConfigAwareInterface::setConfig() |
||
90 | * |
||
91 | * @param string $key |
||
92 | * @param mixed $val |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function setConfig($key, $val) |
||
96 | { |
||
97 | $configInstance = $this->getConfigInstance(); |
||
98 | |||
99 | $configInstance->set($key, $val); |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @see ConfigAwareInterface::setConfigs() |
||
107 | * |
||
108 | * @param array $configs |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setConfigs(array $configs) |
||
112 | { |
||
113 | $configInstance = $this->getConfigInstance(); |
||
114 | |||
115 | $configInstance->setMultiple($configs); |
||
116 | |||
117 | return $this; |
||
118 | } |
||
119 | } |
||
120 |
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.