|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Hackphp\Config; |
| 4 | + |
| 5 | +use Hackphp\Config\Contracts\Parser; |
| 6 | + |
| 7 | +class Config |
| 8 | +{ |
| 9 | + /** |
| 10 | + * The parsed config. |
| 11 | + * |
| 12 | + * @var array |
| 13 | + */ |
| 14 | + protected array $config; |
| 15 | + |
| 16 | + /** |
| 17 | + * The parsers classes. |
| 18 | + * |
| 19 | + * @var Parser[] |
| 20 | + */ |
| 21 | + protected array $parsers = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * Create new Config. |
| 25 | + * |
| 26 | + * @param array $config |
| 27 | + */ |
| 28 | + public function __construct(array $config = []) |
| 29 | + { |
| 30 | + $this->config = $config; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Add Config Parser. |
| 35 | + * |
| 36 | + * @param Parser $parser |
| 37 | + * @return $this |
| 38 | + */ |
| 39 | + public function addParser(Parser $parser) |
| 40 | + { |
| 41 | + if (!in_array($parser, $this->parsers)) { |
| 42 | + $this->parsers[] = $parser; |
| 43 | + } |
| 44 | + |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Load the config loader. |
| 50 | + * |
| 51 | + * @return $this |
| 52 | + */ |
| 53 | + public function load() |
| 54 | + { |
| 55 | + foreach ($this->parsers as $parser) { |
| 56 | + $this->config = array_merge($this->config, $parser->parse()); |
| 57 | + } |
| 58 | + |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get the given config value. |
| 64 | + * |
| 65 | + * @param string $key |
| 66 | + * @param mixed|null $default |
| 67 | + * @return mixed|null |
| 68 | + */ |
| 69 | + public function get($key, $default = null) |
| 70 | + { |
| 71 | + $filtered = $this->config; |
| 72 | + |
| 73 | + if (isset($filtered[$key])) { |
| 74 | + return $filtered[$key]; |
| 75 | + } |
| 76 | + |
| 77 | + $this->catchSearchFile($filtered, $key); |
| 78 | + |
| 79 | + return $this->getFilteredValue($filtered, $key, $default); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Catch the file that has the key we search about it. |
| 84 | + * |
| 85 | + * @param array $filtered |
| 86 | + * @param string $key |
| 87 | + * @return array |
| 88 | + */ |
| 89 | + private function catchSearchFile(array &$filtered, $key) |
| 90 | + { |
| 91 | + return array_reduce(explode(".", $key), function ($carry, $segment) use (&$filtered) { |
| 92 | + if ($carry != null) { |
| 93 | + $key = $carry . "." . $segment; |
| 94 | + } else { |
| 95 | + $key = $segment; |
| 96 | + } |
| 97 | + |
| 98 | + $values = $this->filterStartsWith($filtered, $key); |
| 99 | + |
| 100 | + if ($values) { |
| 101 | + $filtered = $values; |
| 102 | + } |
| 103 | + |
| 104 | + return $key; |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get the array items that it's key starts with the given name. |
| 110 | + * |
| 111 | + * @param array $haystack |
| 112 | + * @param string $name |
| 113 | + * @return array |
| 114 | + */ |
| 115 | + private function filterStartsWith(array $haystack, string $name) |
| 116 | + { |
| 117 | + $filtered = []; |
| 118 | + |
| 119 | + foreach ($haystack as $key => $value) { |
| 120 | + if (strpos($key, $name) === 0) { |
| 121 | + $filtered[$key] = $value; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return $filtered; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Get the given key value from the filtered configs. |
| 130 | + * |
| 131 | + * @param array $filtered |
| 132 | + * @param string $key |
| 133 | + * @param mixed $default |
| 134 | + * @return mixed |
| 135 | + */ |
| 136 | + private function getFilteredValue($filtered, $key, $default) |
| 137 | + { |
| 138 | + if (empty($filtered)) { |
| 139 | + return $default; |
| 140 | + } |
| 141 | + |
| 142 | + $file = array_keys($filtered)[0]; |
| 143 | + $filtered = array_values($filtered)[0]; |
| 144 | + |
| 145 | + $key = str_replace($file . ".", "", $key); |
| 146 | + |
| 147 | + foreach (explode(".", $key) as $segment) { |
| 148 | + $filtered = $filtered[$segment] ?? $default; |
| 149 | + } |
| 150 | + |
| 151 | + return $filtered ?? $default; |
| 152 | + } |
| 153 | +} |
0 commit comments