From abf150a357de23d17a1958c4a655fa7f332c08e7 Mon Sep 17 00:00:00 2001 From: Marek Zajac Date: Wed, 5 May 2021 17:24:58 +0200 Subject: [PATCH 1/4] [Console] Fix Windows code page support --- Helper/QuestionHelper.php | 43 ++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/Helper/QuestionHelper.php b/Helper/QuestionHelper.php index 72880f6a6..92a4c7d0f 100644 --- a/Helper/QuestionHelper.php +++ b/Helper/QuestionHelper.php @@ -110,11 +110,6 @@ private function doAsk(OutputInterface $output, Question $question) $inputStream = $this->inputStream ?: \STDIN; $autocomplete = $question->getAutocompleterCallback(); - if (\function_exists('sapi_windows_cp_set')) { - // Codepage used by cmd.exe on Windows to allow special characters (éàüñ). - @sapi_windows_cp_set(1252); - } - if (null === $autocomplete || !self::$stty || !Terminal::hasSttyAvailable()) { $ret = false; if ($question->isHidden()) { @@ -514,7 +509,10 @@ private function isInteractiveInput($inputStream): bool private function readInput($inputStream, Question $question) { if (!$question->isMultiline()) { - return fgets($inputStream, 4096); + $cp = $this->setIOCodepage(); + $ret = fgets($inputStream, 4096); + + return $this->resetIOCodepage($cp, $ret); } $multiLineStreamReader = $this->cloneInputStream($inputStream); @@ -523,6 +521,7 @@ private function readInput($inputStream, Question $question) } $ret = ''; + $cp = $this->setIOCodepage(); while (false !== ($char = fgetc($multiLineStreamReader))) { if (\PHP_EOL === "{$ret}{$char}") { break; @@ -530,7 +529,37 @@ private function readInput($inputStream, Question $question) $ret .= $char; } - return $ret; + return $this->resetIOCodepage($cp, $ret); + } + + /** + * Set console I/O to the host code page. + * + * @return int Previous code page in IBM/EBCDIC format + */ + private function setIOCodepage(): int + { + if (\function_exists('sapi_windows_cp_set')) { + $cp = sapi_windows_cp_get(); + sapi_windows_cp_set(sapi_windows_cp_get('oem')); + + return $cp; + } + + return 0; + } + + /** + * Set console I/O to the specified code page and convert the user input. + */ + private function resetIOCodepage(int $cp, string $input): string + { + if (\function_exists('sapi_windows_cp_set') && 0 < $cp) { + sapi_windows_cp_set($cp); + $input = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), $cp, $input); + } + + return $input; } /** From 5de9fbaac1fd11eb46311cbff3d457061598ba3e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 7 May 2021 16:48:34 +0200 Subject: [PATCH 2/4] [Console] hotfix --- Helper/QuestionHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Helper/QuestionHelper.php b/Helper/QuestionHelper.php index 92a4c7d0f..a28fa4ee0 100644 --- a/Helper/QuestionHelper.php +++ b/Helper/QuestionHelper.php @@ -512,7 +512,7 @@ private function readInput($inputStream, Question $question) $cp = $this->setIOCodepage(); $ret = fgets($inputStream, 4096); - return $this->resetIOCodepage($cp, $ret); + return false !== $ret ? $this->resetIOCodepage($cp, $ret) : false; } $multiLineStreamReader = $this->cloneInputStream($inputStream); From 1ab187ac21d41d7d34a4f529091a1f5d0bb2924f Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Mon, 10 May 2021 14:53:15 +0200 Subject: [PATCH 3/4] [Console] Fixes for PHP 8.1 deprecations --- Descriptor/TextDescriptor.php | 2 +- Descriptor/XmlDescriptor.php | 2 +- Helper/ProgressIndicator.php | 2 +- Helper/Table.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Descriptor/TextDescriptor.php b/Descriptor/TextDescriptor.php index 220bd2e68..7d4d5f0bb 100644 --- a/Descriptor/TextDescriptor.php +++ b/Descriptor/TextDescriptor.php @@ -117,7 +117,7 @@ protected function describeInputDefinition(InputDefinition $definition, array $o $this->writeText('Options:', $options); foreach ($definition->getOptions() as $option) { - if (\strlen($option->getShortcut()) > 1) { + if (\strlen($option->getShortcut() ?? '') > 1) { $laterOptions[] = $option; continue; } diff --git a/Descriptor/XmlDescriptor.php b/Descriptor/XmlDescriptor.php index a6288d44f..e0ed53a38 100644 --- a/Descriptor/XmlDescriptor.php +++ b/Descriptor/XmlDescriptor.php @@ -201,7 +201,7 @@ private function getInputOptionDocument(InputOption $option): \DOMDocument $dom->appendChild($objectXML = $dom->createElement('option')); $objectXML->setAttribute('name', '--'.$option->getName()); - $pos = strpos($option->getShortcut(), '|'); + $pos = strpos($option->getShortcut() ?? '', '|'); if (false !== $pos) { $objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos)); $objectXML->setAttribute('shortcuts', '-'.str_replace('|', '|-', $option->getShortcut())); diff --git a/Helper/ProgressIndicator.php b/Helper/ProgressIndicator.php index c189ba804..dc37148ed 100644 --- a/Helper/ProgressIndicator.php +++ b/Helper/ProgressIndicator.php @@ -197,7 +197,7 @@ private function display() } return $matches[0]; - }, $this->format)); + }, $this->format ?? '')); } private function determineBestFormat(): string diff --git a/Helper/Table.php b/Helper/Table.php index e7a9b6ccb..329f24082 100644 --- a/Helper/Table.php +++ b/Helper/Table.php @@ -561,7 +561,7 @@ private function buildTableRows(array $rows): TableRows if (isset($this->columnMaxWidths[$column]) && Helper::strlenWithoutDecoration($formatter, $cell) > $this->columnMaxWidths[$column]) { $cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column] * $colspan); } - if (!strstr($cell, "\n")) { + if (!strstr($cell ?? '', "\n")) { continue; } $escaped = implode("\n", array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode("\n", $cell))); From 864568fdc0208b3eba3638b6000b69d2386e6768 Mon Sep 17 00:00:00 2001 From: Marek Zajac Date: Tue, 11 May 2021 15:45:56 +0200 Subject: [PATCH 4/4] [Console] Fix Windows code page support --- Helper/QuestionHelper.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Helper/QuestionHelper.php b/Helper/QuestionHelper.php index a28fa4ee0..340b552aa 100644 --- a/Helper/QuestionHelper.php +++ b/Helper/QuestionHelper.php @@ -512,7 +512,7 @@ private function readInput($inputStream, Question $question) $cp = $this->setIOCodepage(); $ret = fgets($inputStream, 4096); - return false !== $ret ? $this->resetIOCodepage($cp, $ret) : false; + return $this->resetIOCodepage($cp, $ret); } $multiLineStreamReader = $this->cloneInputStream($inputStream); @@ -533,7 +533,7 @@ private function readInput($inputStream, Question $question) } /** - * Set console I/O to the host code page. + * Sets console I/O to the host code page. * * @return int Previous code page in IBM/EBCDIC format */ @@ -550,13 +550,20 @@ private function setIOCodepage(): int } /** - * Set console I/O to the specified code page and convert the user input. + * Sets console I/O to the specified code page and converts the user input. + * + * @param string|false $input + * + * @return string|false */ - private function resetIOCodepage(int $cp, string $input): string + private function resetIOCodepage(int $cp, $input) { - if (\function_exists('sapi_windows_cp_set') && 0 < $cp) { + if (0 !== $cp) { sapi_windows_cp_set($cp); - $input = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), $cp, $input); + + if (false !== $input && '' !== $input) { + $input = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), $cp, $input); + } } return $input;