1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flying\Config; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Standalone implementation of object configuration |
7
|
|
|
*/ |
8
|
|
|
class ObjectConfig extends AbstractConfig |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Owner of this configuration object |
12
|
|
|
* |
13
|
|
|
* @var object |
14
|
|
|
*/ |
15
|
|
|
private $owner = null; |
16
|
|
|
/** |
17
|
|
|
* Configuration options for to serve by this configuration object |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $options; |
22
|
|
|
/** |
23
|
|
|
* Configuration class Id for this configuration object |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $classId = null; |
28
|
|
|
/** |
29
|
|
|
* List of registered callbacks for customizing configuration object behavior |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $callbacks = [ |
34
|
|
|
'validateConfig' => null, // Custom implementation of validateConfig() |
35
|
|
|
'onConfigChange' => null, // Custom implementation of onConfigChange() |
36
|
|
|
'lazyConfigInit' => null, // Custom implementation of lazyConfigInit() |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class constructor |
41
|
|
|
* |
42
|
|
|
* @param object $owner Owner of this configuration object |
43
|
|
|
* @param array $options List of configuration options to serve (@see AbstractConfig::initConfig for description) |
44
|
|
|
* @param array $callbacks OPTIONAL List of callbacks to customize configuration object behavior |
45
|
|
|
* @param array $config OPTIONAL Configuration options to initialize class with |
46
|
|
|
* @throws \InvalidArgumentException |
47
|
|
|
*/ |
48
|
24 |
|
public function __construct($owner, array $options, array $callbacks = [], array $config = []) |
49
|
|
|
{ |
50
|
24 |
|
$this->setOwner($owner); |
51
|
23 |
|
$this->options = $options; |
52
|
23 |
|
foreach ($callbacks as $type => $callback) { |
53
|
22 |
|
if (!array_key_exists($type, $this->callbacks)) { |
54
|
1 |
|
throw new \InvalidArgumentException('Unknown customization callback type: ' . $type); |
55
|
|
|
} |
56
|
|
|
// If method name is passed instead of callback - create callback from it |
57
|
21 |
|
if (is_string($callback) && method_exists($this->owner, $callback)) { |
58
|
|
|
$callback = [$this->owner, $callback]; |
59
|
|
|
} |
60
|
21 |
|
if (!is_callable($callback)) { |
61
|
2 |
|
throw new \InvalidArgumentException('Non-callable callback is given for customization callback type: ' . $type); |
62
|
|
|
} |
63
|
19 |
|
$this->callbacks[$type] = $callback; |
64
|
20 |
|
} |
65
|
20 |
|
$this->bootstrapConfig(); |
66
|
20 |
|
$this->setConfig($config); |
67
|
20 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set owner of this configuration object |
71
|
|
|
* |
72
|
|
|
* @param object $owner Owner of this configuration object |
73
|
|
|
* @throws \InvalidArgumentException |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
24 |
|
protected function setOwner($owner) |
77
|
|
|
{ |
78
|
24 |
|
if (!is_object($owner)) { |
79
|
1 |
|
throw new \InvalidArgumentException('Given owner of configuration object is not an object'); |
80
|
|
|
} |
81
|
23 |
|
$this->owner = $owner; |
82
|
23 |
|
$this->classId = null; // Reset class Id because we have new owner |
83
|
23 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
20 |
|
protected function getConfigClassId() |
89
|
|
|
{ |
90
|
20 |
|
if (!$this->classId) { |
91
|
20 |
|
$this->classId = get_class($this->owner); |
92
|
20 |
|
} |
93
|
20 |
|
return $this->classId; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
20 |
|
protected function initConfig() |
100
|
|
|
{ |
101
|
20 |
|
parent::initConfig(); |
102
|
20 |
|
$this->mergeConfig($this->options); |
103
|
20 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
19 |
|
protected function validateConfig($name, &$value) |
109
|
|
|
{ |
110
|
19 |
|
if ($this->callbacks['validateConfig'] !== null) { |
111
|
|
|
/** @noinspection VariableFunctionsUsageInspection */ |
112
|
18 |
|
return call_user_func_array( |
113
|
18 |
|
$this->callbacks['validateConfig'], |
114
|
18 |
|
[$name, &$value] |
115
|
18 |
|
); |
116
|
|
|
} |
117
|
1 |
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
6 |
|
protected function onConfigChange($name, $value) |
124
|
|
|
{ |
125
|
6 |
|
if ($this->callbacks['onConfigChange'] !== null) { |
126
|
5 |
|
call_user_func($this->callbacks['onConfigChange'], $name, $value); |
127
|
5 |
|
} |
128
|
6 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
14 |
|
protected function lazyConfigInit($name) |
134
|
|
|
{ |
135
|
14 |
|
if ($this->callbacks['lazyConfigInit'] !== null) { |
136
|
14 |
|
return call_user_func($this->callbacks['lazyConfigInit'], $name); |
137
|
|
|
} |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|