ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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;
30use Seld\JsonLint\JsonParser;
31
36{
37 protected JsonParser $json_parser;
38 protected string $base_dir;
39
40 public function __construct(JsonParser $json_parser, ?string $base_dir = null)
41 {
42 $this->json_parser = $json_parser;
43 $this->base_dir = $base_dir ?? getcwd();
44 }
45
55 public function readConfigFile(string $name, array $overwrites = []): array
56 {
57 $name = $this->getRealFilename($name);
58 if (!is_readable($name)) {
59 throw new \InvalidArgumentException(
60 "Config-file '$name' does not exist or is not readable."
61 );
62 }
63 $json = $this->json_parser->parse(
64 file_get_contents($name),
65 JsonParser::PARSE_TO_ASSOC | JsonParser::DETECT_KEY_CONFLICTS
66 );
67
68 if (!is_array($json)) {
69 throw new \InvalidArgumentException(
70 "Could not find JSON-array in '$name'."
71 );
72 }
73 return $this->applyOverwrites($json, $overwrites);
74 }
75
76 protected function applyOverwrites(array $json, array $overwrites): array
77 {
78 $replacer = null;
79 $replacer = function ($subject, $path, $value) use (&$replacer) {
80 if (count($path) === 0) {
81 return $value;
82 }
83 $cur = array_shift($path);
84 $subject[$cur] = $replacer($subject[$cur] ?? [], $path, $value);
85 return $subject;
86 };
87
88 foreach ($overwrites as $path => $value) {
89 $path = explode(".", (string) $path);
90 $json = $replacer($json, $path, $value);
91 }
92
93 return $json;
94 }
95
96 protected function getRealFilename(string $name): string
97 {
98 if (in_array($name[0], ["/", "\\"])) {
99 return $name;
100 }
101 return $this->base_dir . "/" . $name;
102 }
103}
Read a json-formatted config from a file and overwrite some fields.
applyOverwrites(array $json, array $overwrites)
__construct(JsonParser $json_parser, ?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...