Issues (3877)

Zed/Kernel/Communication/Console/Helper.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\Kernel\Communication\Console;
9
10
use RuntimeException;
11
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * @deprecated Trait will be removed in next major version of Kernel
16
 */
17
trait Helper
18
{
19
    /**
20
     * @param string $message
21
     * @param bool $wrapInInfoTags
22
     *
23
     * @return void
24
     */
25
    public function info($message, $wrapInInfoTags = true)
26
    {
27
        if (is_array($message)) {
0 ignored issues
show
The condition is_array($message) is always false.
Loading history...
28
            $message = implode(PHP_EOL, $message);
29
        }
30
        if ($wrapInInfoTags) {
31
            $message = '<info>' . $message . '</info>';
32
        }
33
        $this->output->writeln($message);
34
    }
35
36
    /**
37
     * @param string $message
38
     *
39
     * @return void
40
     */
41
    public function error($message)
42
    {
43
        $width = $this->getApplication()->getTerminalDimensions()[0];
44
        $width = ($width) ?: 200;
45
        $width -= strlen($message);
46
        $width = max(0, $width);
47
        $subOne = false;
48
        if ($width % 2 !== 0) {
49
            $width += 1;
50
            $subOne = true;
51
        }
52
        $halfWidth = $width / 2;
53
        $message = str_repeat(' ', $halfWidth) . $message;
54
        if ($subOne) {
55
            $halfWidth -= 1;
56
        }
57
        $message .= str_repeat(' ', $halfWidth);
58
        $message = '<error>' . $message . '</error>';
59
60
        $this->output->writeln('<error>' . str_repeat(' ', $width + strlen($message)) . '</error>');
61
        $this->output->writeln($message);
62
        $this->output->writeln('<error>' . str_repeat(' ', $width + strlen($message)) . '</error>');
63
    }
64
65
    /**
66
     * @param string $message
67
     *
68
     * @return void
69
     */
70
    public function warning($message)
71
    {
72
        $width = $this->getApplication()->getTerminalDimensions()[0];
73
        $width = ($width) ?: 200;
74
        $width -= strlen($message);
75
        $subOne = false;
76
        if ($width % 2 !== 0) {
77
            $width += 1;
78
            $subOne = true;
79
        }
80
        $halfWidth = $width / 2;
81
        $message = str_repeat(' ', $halfWidth) . $message;
82
        if ($subOne) {
83
            $halfWidth -= 1;
84
        }
85
        $message .= str_repeat(' ', $halfWidth);
86
        $message = '<warning>' . $message . '</warning>';
87
88
        $style = new OutputFormatterStyle('black', 'yellow');
89
        $this->output->getFormatter()->setStyle('warning', $style);
90
91
        $this->output->writeln('<warning>' . str_repeat(' ', $width + strlen($message)) . '</warning>');
92
        $this->output->writeln($message);
93
        $this->output->writeln('<warning>' . str_repeat(' ', $width + strlen($message)) . '</warning>');
94
    }
95
96
    /**
97
     * @param string $message
98
     *
99
     * @return void
100
     */
101
    public function success($message)
102
    {
103
        $width = $this->getApplication()->getTerminalDimensions()[0];
104
        $width = ($width) ?: 200;
105
        $width -= strlen($message);
106
        $subOne = false;
107
        if ($width % 2 !== 0) {
108
            $width += 1;
109
            $subOne = true;
110
        }
111
        $halfWidth = $width / 2;
112
        $message = str_repeat(' ', $halfWidth) . $message;
113
        if ($subOne) {
114
            $halfWidth -= 1;
115
        }
116
        $message .= str_repeat(' ', $halfWidth);
117
        $message = '<success>' . $message . '</success>';
118
119
        $style = new OutputFormatterStyle('black', 'green');
120
        $this->output->getFormatter()->setStyle('success', $style);
121
122
        $this->output->writeln('<success>' . str_repeat(' ', $width + strlen($message)) . '</success>');
123
        $this->output->writeln($message);
124
        $this->output->writeln('<success>' . str_repeat(' ', $width + strlen($message)) . '</success>');
125
    }
126
127
    /**
128
     * @param string $question
129
     *
130
     * @return bool
131
     */
132
    public function askConfirmation($question)
133
    {
134
        $question = $question . '? <fg=green>[yes|no|abort]</fg=green> ';
135
136
        $result = $this->askAbortableConfirmation($this->output, $question, false);
137
138
        return $result;
139
    }
140
141
    /**
142
     * Asks a confirmation to the user.
143
     *
144
     * The question will be asked until the user answers by yes, or no.
145
     * If he answers nothing, it will use the default value. If he answers abort,
146
     * it will throw a RuntimeException.
147
     *
148
     * @param \Symfony\Component\Console\Output\OutputInterface $output An Output instance
149
     * @param string $question The question to ask
150
     * @param bool $default The default answer if the user enters nothing
151
     *
152
     * @throws \RuntimeException
153
     *
154
     * @return bool true if the user has confirmed, false otherwise
155
     */
156
    public function askAbortableConfirmation(OutputInterface $output, $question, $default = true)
157
    {
158
        $answer = 'z';
159
        while ($answer && !in_array(strtolower($answer[0]), ['y', 'n', 'a'])) {
160
            $answer = $this->ask($question, $default);
161
        }
162
163
        if (strtolower($answer[0]) === 'a') {
164
            throw new RuntimeException('Aborted');
165
        }
166
167
        if ($default === false) {
168
            return $answer && strtolower($answer[0]) === 'y';
169
        }
170
171
        return !$answer || strtolower($answer[0]) === 'y';
172
    }
173
174
    /**
175
     * @param string $question
176
     * @param string|null $default
177
     *
178
     * @return string|null
179
     */
180
    public function ask($question, $default = null)
181
    {
182
        /** @var \Symfony\Component\Console\Helper\DialogHelper $dialog */
183
        $dialog = $this->getHelperSet()->get('dialog');
184
185
        return $dialog->ask($this->output, $question, $default);
186
    }
187
188
    /**
189
     * @param string $question
190
     * @param array<string, mixed> $options
191
     * @param string $default
192
     *
193
     * @return mixed
194
     */
195
    public function select($question, array $options, $default)
196
    {
197
        /** @var \Symfony\Component\Console\Helper\DialogHelper $dialog */
198
        $dialog = $this->getHelperSet()->get('dialog');
199
        $selected = $dialog->select(
200
            $this->output,
201
            $question,
202
            $options,
203
            $default,
204
        );
205
206
        return $options[$selected];
207
    }
208
209
    /**
210
     * @param bool $wrapInInfoTags
211
     *
212
     * @return void
213
     */
214
    public function printLineSeparator($wrapInInfoTags = true)
215
    {
216
        $width = $this->getApplication()->getTerminalDimensions()[0];
217
        $width = ($width) ?: 200;
218
        $this->info(str_repeat('-', $width), $wrapInInfoTags);
219
    }
220
221
    /**
222
     * @return \Silex\Application
223
     */
224
    abstract protected function getApplication();
225
226
    /**
227
     * @return \Symfony\Component\Console\Helper\HelperSet
228
     */
229
    abstract protected function getHelperSet();
230
}
231