|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Caridea |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Caridea\Container; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Dependency injection container that only has runtime configuration. |
|
25
|
|
|
* |
|
26
|
|
|
* This container is meant to be used as a parent to a container which has |
|
27
|
|
|
* real objects in it. Since this one is intended to hold configuration, you |
|
28
|
|
|
* should really only add scalar types and arrays. However, it's certainly |
|
29
|
|
|
* possible to add object instances. |
|
30
|
|
|
* |
|
31
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
32
|
|
|
* @license Apache-2.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
class Properties extends AbstractContainer |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array The stored properties |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $values = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates a container with static configuration properties. |
|
43
|
|
|
* |
|
44
|
|
|
* Null values are silently ignored. |
|
45
|
|
|
* |
|
46
|
|
|
* ``` |
|
47
|
|
|
* $props = new \Caridea\Container\Properties([ |
|
48
|
|
|
* 'db.host' => 'example.com', |
|
49
|
|
|
* 'db.port' => 1337, |
|
50
|
|
|
* 'db.user' => 'dba', |
|
51
|
|
|
* 'cache.holder' => new \SplObjectStorage(), |
|
52
|
|
|
* 'dates.nye' => new \DateTime('2015-12-31') |
|
53
|
|
|
* ]); |
|
54
|
|
|
* ``` |
|
55
|
|
|
* |
|
56
|
|
|
* @param array<string,mixed> $properties String keys, mixed values. |
|
57
|
|
|
* @param \Caridea\Container\Container $parent An optional parent container. |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function __construct(array $properties, Container $parent = null) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$types = []; |
|
62
|
1 |
|
foreach ($properties as $k => $v) { |
|
63
|
1 |
|
if ($v !== null) { |
|
64
|
1 |
|
$types[(string)$k] = self::typeof($v); |
|
65
|
1 |
|
$this->values[(string)$k] = $v; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
1 |
|
parent::__construct($types, $parent); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* More predictable results than `gettype`. |
|
73
|
|
|
* |
|
74
|
|
|
* @param mixed $v The value to evaluate |
|
75
|
|
|
*/ |
|
76
|
1 |
|
protected static function typeof($v): string |
|
77
|
|
|
{ |
|
78
|
1 |
|
if (is_bool($v)) { |
|
79
|
1 |
|
return 'bool'; |
|
80
|
1 |
|
} elseif (is_int($v)) { |
|
81
|
1 |
|
return 'int'; |
|
82
|
1 |
|
} elseif (is_float($v)) { |
|
83
|
1 |
|
return 'float'; |
|
84
|
1 |
|
} elseif (is_string($v)) { |
|
85
|
1 |
|
return 'string'; |
|
86
|
1 |
|
} elseif (is_array($v)) { |
|
87
|
1 |
|
return 'array'; |
|
88
|
1 |
|
} elseif (is_resource($v)) { |
|
89
|
1 |
|
return 'resource'; |
|
90
|
|
|
} |
|
91
|
1 |
|
return get_class($v); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Retrieves the value |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $name The value name |
|
98
|
|
|
*/ |
|
99
|
1 |
|
protected function doGet(string $name) |
|
100
|
|
|
{ |
|
101
|
1 |
|
return isset($this->values[$name]) ? $this->values[$name] : null; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|