1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Config |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Config\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* DelegatorWritableTrait |
21
|
|
|
* |
22
|
|
|
* @package Phossa2\Config |
23
|
|
|
* @author Hong Zhang <[email protected]> |
24
|
|
|
* @see WritableInterface |
25
|
|
|
* @version 2.0.0 |
26
|
|
|
* @since 2.0.0 added |
27
|
|
|
*/ |
28
|
|
|
trait DelegatorWritableTrait |
29
|
|
|
{ |
30
|
|
|
use WritableTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Override `isWritable()` in the WritableTrait |
34
|
|
|
* |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function isWritable()/*# : bool */ |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
foreach ($this->lookup_pool as $reg) { |
|
|
|
|
40
|
|
|
if ($reg instanceof WritableInterface && |
41
|
|
|
$reg->isWritable() |
42
|
|
|
) { |
43
|
|
|
$this->setWritable($reg); |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Override `setWritable()` in the WritableTrait |
52
|
|
|
* |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function setWritable($writable) |
56
|
|
|
{ |
57
|
|
|
// only if is boolean |
58
|
|
|
if (is_bool($writable)) { |
59
|
|
|
if ($writable !== $this->isWritable()) { |
60
|
|
|
$this->setRegistryWritable($writable); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// set the writable registry |
64
|
|
|
} else { |
65
|
|
|
$this->writable = $writable; |
66
|
|
|
} |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set underlying registries's writability |
72
|
|
|
* |
73
|
|
|
* @param bool $writable |
74
|
|
|
* @return $this |
75
|
|
|
* @access protected |
76
|
|
|
*/ |
77
|
|
|
protected function setRegistryWritable(/*# bool */ $writable) |
78
|
|
|
{ |
79
|
|
|
if (false === $writable) { |
80
|
|
|
return $this->setRegistryWritableFalse(); |
81
|
|
|
} else { |
82
|
|
|
return $this->setRegistryWritableTrue(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set writable to FALSE in all registries |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
* @access protected |
91
|
|
|
*/ |
92
|
|
|
protected function setRegistryWritableFalse() |
93
|
|
|
{ |
94
|
|
|
foreach ($this->lookup_pool as $reg) { |
95
|
|
|
if ($reg instanceof WritableInterface) { |
96
|
|
|
$reg->setWritable(false); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set writable to TRUE at first matching registry |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
* @access protected |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
protected function setRegistryWritableTrue() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
foreach ($this->lookup_pool as $reg) { |
111
|
|
|
if ($reg instanceof WritableInterface) { |
112
|
|
|
$reg->setWritable(true); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.