ILIAS  release_7 Revision v7.30-3-g800a261c036
ConfigReader.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3
4namespace ILIAS\Setup\CLI;
5
9use Symfony\Component\Console\Command\Command;
10use Symfony\Component\Console\Input\InputArgument;
11use Symfony\Component\Console\Input\InputInterface;
12use Symfony\Component\Console\Output\OutputInterface;
13
18{
22 protected $base_dir;
23
24 public function __construct($base_dir = null)
25 {
26 $this->base_dir = $base_dir ?? getcwd();
27 }
28
38 public function readConfigFile(string $name, array $overwrites = []) : array
39 {
40 $name = $this->getRealFilename($name);
41 if (!file_exists($name) || !is_readable($name)) {
42 throw new \InvalidArgumentException(
43 "Config-file '$name' does not exist or is not readable."
44 );
45 }
46 $json = json_decode(file_get_contents($name), JSON_OBJECT_AS_ARRAY);
47 if (!is_array($json)) {
48 throw new \InvalidArgumentException(
49 "Could not find JSON-array in '$name'."
50 );
51 }
52 return $this->applyOverwrites($json, $overwrites);
53 }
54
55 protected function applyOverwrites(array $json, array $overwrites) : array
56 {
57 $replacer = null;
58 $replacer = function ($subject, $path, $value) use (&$replacer) {
59 if (count($path) === 0) {
60 return $value;
61 }
62 $cur = array_shift($path);
63 $subject[$cur] = $replacer($subject[$cur] ?? [], $path, $value);
64 return $subject;
65 };
66
67 foreach ($overwrites as $path => $value) {
68 $path = explode(".", $path);
69 $json = $replacer($json, $path, $value);
70 }
71
72 return $json;
73 }
74
75 protected function getRealFilename(string $name) : string
76 {
77 if (in_array($name[0], ["/", "\\"])) {
78 return $name;
79 }
80 return $this->base_dir . "/" . $name;
81 }
82}
An exception for terminatinating execution or to throw for unit testing.
Read a json-formatted config from a file and overwrite some fields.
applyOverwrites(array $json, array $overwrites)
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....
if($format !==null) $name
Definition: metadata.php:230
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...