|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Enhavo\Bundle\AppBundle\Endpoint; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
6
|
|
|
|
|
7
|
|
|
class Loader |
|
8
|
|
|
{ |
|
9
|
9 |
|
public function __construct( |
|
10
|
|
|
public string $dataPath |
|
11
|
|
|
) |
|
12
|
|
|
{ |
|
13
|
9 |
|
} |
|
14
|
|
|
|
|
15
|
5 |
|
public function merge(&$target, $source, ?bool $recursive = false, ?int $depth = null) |
|
16
|
|
|
{ |
|
17
|
5 |
|
foreach ($source as $key => $value) { |
|
18
|
5 |
|
if ($recursive && ($depth === null || $depth > 0 ) && isset($target[$key]) && $this->isArrayLike($target[$key]) && $this->isArrayLike($value)) { |
|
19
|
3 |
|
$depth = is_numeric($depth) ? $depth - 1 : null; |
|
20
|
|
|
// if we have array like data, we can't pass it directly as a reference parameter, so lets save it |
|
21
|
|
|
// temporarily in a variable and write the values back later |
|
22
|
3 |
|
$temp = $target[$key]; |
|
23
|
3 |
|
$this->merge($temp, $value, $recursive, $depth); |
|
24
|
3 |
|
$target[$key] = $temp; |
|
25
|
|
|
} else { |
|
26
|
5 |
|
$target[$key] = $value; |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
private function isArrayLike($data): bool |
|
32
|
|
|
{ |
|
33
|
3 |
|
if (is_array($data)) { |
|
34
|
3 |
|
return true; |
|
35
|
3 |
|
} elseif ($data instanceof \ArrayAccess && $data instanceof \Traversable) { |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
3 |
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
public function load($file): array |
|
43
|
|
|
{ |
|
44
|
4 |
|
$path = realpath($this->dataPath) . '/' . $file; |
|
45
|
4 |
|
if (!file_exists($path)) { |
|
46
|
|
|
throw new \Exception(sprintf('File "%s" not exists', $file)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
$ext = pathinfo($path, PATHINFO_EXTENSION); |
|
50
|
4 |
|
$fileData = match ($ext) { |
|
51
|
4 |
|
'yml', 'yaml' => $this->loadYamlFile($path), |
|
52
|
4 |
|
'json' => $this->loadJsonFile($path), |
|
53
|
4 |
|
'php' => $this->loadPHPFile($path), |
|
54
|
4 |
|
default => throw new \Exception(sprintf('Extension "%s" not supported', $ext)) |
|
|
|
|
|
|
55
|
4 |
|
}; |
|
56
|
|
|
|
|
57
|
4 |
|
if (!is_array($fileData)) { |
|
58
|
|
|
throw new \Exception(sprintf('The file "%s" must return an array', $file)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
return $fileData; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
private function loadYamlFile($path) |
|
65
|
|
|
{ |
|
66
|
1 |
|
return Yaml::parse(file_get_contents($path)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
private function loadJsonFile($path) |
|
70
|
|
|
{ |
|
71
|
1 |
|
return json_decode(file_get_contents($path), true); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
private function loadPHPFile($path) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$callable = include $path; |
|
77
|
2 |
|
if (!is_callable($callable)) { |
|
78
|
|
|
throw new \Exception(sprintf('The file "%s" must return a callable', $path)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return $callable($this); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|