ILIAS  release_7 Revision v7.30-3-g800a261c036
IOWrapper.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2019 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5namespace ILIAS\Setup\CLI;
6
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
10use Symfony\Component\Console\Style\SymfonyStyle;
11
17{
18 const LABEL_WIDTH = 75;
19 const ELLIPSIS = "...";
20
24 protected $in;
25
29 protected $out;
30
34 protected $style;
35
39 protected $last_objective_was_notable = false;
40
44 protected $last_objective_label = "";
45
49 protected $output_in_objective = false;
50
51 public function __construct(InputInterface $in, OutputInterface $out)
52 {
53 $this->in = $in;
54 $this->out = $out;
55 $this->style = new SymfonyStyle($in, $out);
56 }
57
58 // Implementation of AdminInteraction
59 public function startProgress(int $max) : void
60 {
61 $this->style->progressStart($max);
62 }
63
64 public function advanceProgress() : void
65 {
66 $this->style->progressAdvance();
67 }
68
69 public function stopProgress() : void
70 {
71 $this->style->progressFinish();
72 }
73
74 public function inform(string $message) : void
75 {
76 $this->outputInObjective();
77 $this->style->note($message);
78 }
79
80 public function confirmExplicit(string $message, string $type_text_to_confirm) : bool
81 {
82 $this->outputInObjective();
83 if (!$this->shouldSayYes()) {
84 return $this->style->ask(
86 null,
87 function (string $input) use ($type_text_to_confirm) : bool {
88 return $type_text_to_confirm === $input;
89 }
90 );
91 } else {
92 $this->inform("Automatically confirmed:\n\n$message");
93 return true;
94 }
95 }
96
97 public function confirmOrDeny(string $message) : bool
98 {
99 $this->outputInObjective();
100 if (!$this->shouldSayYes()) {
101 return $this->style->confirm($message, false);
102 } else {
103 $this->inform("Automatically confirmed:\n\n$message");
104 return true;
105 }
106 }
107
108 // For CLI-Setup
109
110 public function printLicenseMessage() : void
111 {
112 if ($this->shouldSayYes() || ($this->in->hasOption("no-interaction") && $this->in->getOption("no-interaction"))) {
113 return;
114 }
115 $this->text(
116 " ILIAS Copyright (C) 1998-2019 ILIAS Open Source e.V. - GPLv3\n\n" .
117 "This program comes with ABSOLUTELY NO WARRANTY. This is free software,\n" .
118 "and you are welcome to redistribute it under certain conditions. Look\n" .
119 "into the LICENSE file for details."
120 );
121 }
122
123 protected function shouldSayYes() : bool
124 {
125 return $this->in->getOption("yes") ?? false;
126 }
127
128 public function title(string $title) : void
129 {
130 $this->style->title($title);
131 }
132
133 public function success(string $text) : void
134 {
135 $this->style->success($text);
136 }
137
138 public function error(string $text) : void
139 {
140 $this->style->error($text);
141 }
142
143 public function text(string $text) : void
144 {
145 $this->style->text($text);
146 }
147
148 public function startObjective(string $label, bool $is_notable)
149 {
150 $this->last_objective_was_notable = $is_notable;
151 $this->last_objective_label = $label;
152 $this->output_in_objective = false;
153 if ($this->showLastObjectiveLabel()) {
154 $this->style->write(str_pad($label . "...", self::LABEL_WIDTH));
155 }
156 }
157
158 public function finishedLastObjective()
159 {
160 if ($this->output_in_objective) {
161 $this->startObjective($this->last_objective_label, $this->last_objective_was_notable);
162 }
163
164 if ($this->showLastObjectiveLabel()) {
165 $this->style->write("[<fg=green>OK</>]\n");
166 }
167 }
168
169 public function failedLastObjective()
170 {
171 // Always show label of failed objectives.
172 if ($this->output_in_objective || !$this->last_objective_was_notable) {
173 $this->startObjective($this->last_objective_label, true);
174 }
175
176 if ($this->showLastObjectiveLabel()) {
177 $this->style->write("[<fg=red>FAILED</>]\n");
178 }
179 }
180
181 protected function outputInObjective() : void
182 {
183 if (!$this->output_in_objective && $this->showLastObjectiveLabel()) {
184 $this->output_in_objective = true;
185 $this->style->write("[in progress]\n");
186 }
187 }
188
189 protected function showLastObjectiveLabel()
190 {
191 return $this->last_objective_was_notable
192 || $this->out->isVeryVerbose()
193 || $this->out->isDebug();
194 }
195
196 public function isVerbose() : bool
197 {
198 return $this->out->isVerbose();
199 }
200}
An exception for terminatinating execution or to throw for unit testing.
Wrapper around symfonies input and output facilities to provide just the functionality required for t...
Definition: IOWrapper.php:17
inform(string $message)
Definition: IOWrapper.php:74
confirmExplicit(string $message, string $type_text_to_confirm)
Definition: IOWrapper.php:80
__construct(InputInterface $in, OutputInterface $out)
Definition: IOWrapper.php:51
confirmOrDeny(string $message)
Definition: IOWrapper.php:97
title(string $title)
Definition: IOWrapper.php:128
startObjective(string $label, bool $is_notable)
Definition: IOWrapper.php:148
This defines ways in which objectives may interact with admins during the setup.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$message
Definition: xapiexit.php:14