ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
HasConfigReader.php
Go to the documentation of this file.
1<?php
18namespace ILIAS\Setup\CLI;
19
24use Symfony\Component\Console\Input\InputInterface;
25
30{
34 protected ConfigReader $config_reader;
35
36 protected function readAgentConfig(Agent $agent, InputInterface $input, ?string $use_config_field = null): ?Config
37 {
38 if (!($this->config_reader instanceof ConfigReader)) {
39 throw new \LogicException("\$this->config_reader not properly initialized.");
40 }
41
42 if (!$agent->hasConfig()) {
43 return null;
44 }
45
46 $config_file = $input->getArgument("config");
47 if ($this->isConfigInRoot($config_file)) {
48 throw new \LogicException("Thou shall not put your config file in the webroot!!");
49 }
50
51 $config_overwrites_raw = $input->getOption("config");
52 $config_overwrites = [];
53 foreach ($config_overwrites_raw as $o) {
54 $vs = explode("=", $o);
55 if (count($vs) !== 2) {
56 throw new \Exception("Cannot read config-option: '$o'");
57 }
58 $config_overwrites[$vs[0]] = $vs[1];
59 }
60
61 $config_content = $this->config_reader->readConfigFile($config_file, $config_overwrites);
62 if ($use_config_field !== null) {
63 $config_content = $config_content[$use_config_field] ?? null;
64 }
65
66 $trafo = $agent->getArrayToConfigTransformation();
67 return $trafo->transform($config_content);
68 }
69
70 protected function addAgentConfigsToEnvironment(
71 Agent $agent,
72 Config $config,
73 Environment $environment
74 ): Environment {
75 if ($agent instanceof AgentCollection) {
76 foreach ($config->getKeys() as $k) {
77 $environment = $environment->withConfigFor($k, $config->getConfig($k));
78 }
79 }
80
81 return $environment;
82 }
83
84 protected function isConfigInRoot(string $config_file): bool
85 {
86 $webroot = realpath(__DIR__ . "/../../../../../");
87 $config_file = realpath($config_file);
88
89 $common_prefix = substr($config_file, 0, strlen($webroot));
90 return $webroot === $common_prefix;
91 }
92}
An agent that is just a collection of some other agents.
Read a json-formatted config from a file and overwrite some fields.
A agent is some component that performs part of the setup process.
Definition: Agent.php:30
hasConfig()
Does this agent require a configuration?
getArrayToConfigTransformation()
Agents must be able to tell how to create a configuration from a nested array.
A configuration for the setup.
Definition: Config.php:27
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
withConfigFor(string $component, $config)
Stores a config for some component in the environment.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addAgentConfigsToEnvironment(Agent $agent, Config $config, Environment $environment)
isConfigInRoot(string $config_file)
readAgentConfig(Agent $agent, InputInterface $input, ?string $use_config_field=null)
trait HasConfigReader
Add this to an Command that has an config reader.
if(!file_exists('../ilias.ini.php'))