ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
ConfigReader.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Setup\CLI;
22
27use Symfony\Component\Console\Input\InputArgument;
28use Symfony\Component\Console\Input\InputInterface;
29use Symfony\Component\Console\Output\OutputInterface;
30
35{
36 protected string $base_dir;
37
38 public function __construct(?string $base_dir = null)
39 {
40 $this->base_dir = $base_dir ?? getcwd();
41 }
42
52 public function readConfigFile(string $name, array $overwrites = []): array
53 {
54 $name = $this->getRealFilename($name);
55 if (!is_readable($name)) {
56 throw new \InvalidArgumentException(
57 "Config-file '$name' does not exist or is not readable."
58 );
59 }
60 $json = json_decode(
61 file_get_contents($name),
62 true,
63 512,
64 JSON_THROW_ON_ERROR
65 );
66
67 if (!is_array($json)) {
68 throw new \InvalidArgumentException(
69 "Could not find JSON-array in '$name'."
70 );
71 }
72 return $this->applyOverwrites($json, $overwrites);
73 }
74
75 protected function applyOverwrites(array $json, array $overwrites): array
76 {
77 $replacer = null;
78 $replacer = function ($subject, $path, $value) use (&$replacer) {
79 if (count($path) === 0) {
80 return $value;
81 }
82 $cur = array_shift($path);
83 $subject[$cur] = $replacer($subject[$cur] ?? [], $path, $value);
84 return $subject;
85 };
86
87 foreach ($overwrites as $path => $value) {
88 $path = explode(".", (string) $path);
89 $json = $replacer($json, $path, $value);
90 }
91
92 return $json;
93 }
94
95 protected function getRealFilename(string $name): string
96 {
97 if (in_array($name[0], ["/", "\\"])) {
98 return $name;
99 }
100 return $this->base_dir . "/" . $name;
101 }
102}
Read a json-formatted config from a file and overwrite some fields.
applyOverwrites(array $json, array $overwrites)
__construct(?string $base_dir=null)
readConfigFile(string $name, array $overwrites=[])
TODO: We could use the "give me a transformation and I'll give you your result" pattern from th...
Tries to enumerate all preconditions for the given objective, where the ones that can be achieved (i....
$path
Definition: ltiservices.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...