8000 bug #41210 [Console] Fix Windows code page support (orkan) · symfony/symfony@49dd6ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 49dd6ef

Browse files
bug #41210 [Console] Fix Windows code page support (orkan)
This PR was merged into the 4.4 branch. Discussion ---------- [Console] Fix Windows code page support | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #37385, Fix #35842, Fix #36324, Fix #37495, Fix #37278 | License | MIT Corrects mojibake problem on Windows where an OEM code page was applied to an input string and then messed with PHP.internal_encoding setting used by the script. This caused strings with different encodings to be displayed on the console output. Commits ------- 4145278 [Console] Fix Windows code page support
2 parents 616b2c9 + 4145278 commit 49dd6ef

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ private function doAsk(OutputInterface $output, Question $question)
108108
$inputStream = $this->inputStream ?: \STDIN;
109109
$autocomplete = $question->getAutocompleterCallback();
110110

111-
if (\function_exists('sapi_windows_cp_set')) {
112-
// Codepage used by cmd.exe on Windows to allow special characters (éàüñ).
113-
@sapi_windows_cp_set(1252);
114-
}
115-
116111
if (null === $autocomplete || !self::$stty || !Terminal::hasSttyAvailable()) {
117112
$ret = false;
118113
if ($question->isHidden()) {
@@ -127,7 +122,9 @@ private function doAsk(OutputInterface $output, Question $question)
127122
}
128123

129124
if (false === $ret) {
125+
$cp = $this->setIOCodepage();
130126
$ret = fgets($inputStream, 4096);
127+
$ret = $this->resetIOCodepage($cp, $ret);
131128
if (false === $ret) {
132129
throw new MissingInputException('Aborted.');
133130
}
@@ -503,4 +500,41 @@ private function isInteractiveInput($inputStream): bool
503500

504501
return self::$stdinIsInteractive = 1 !== $status;
505502
}
503+
504+
/**
505+
* Sets console I/O to the host code page.
506+
*
507+
* @return int Previous code page in IBM/EBCDIC format
508+
*/
509+
private function setIOCodepage(): int
510+
{
511+
if (\function_exists('sapi_windows_cp_set')) {
512+
$cp = sapi_windows_cp_get();
513+
sapi_windows_cp_set(sapi_windows_cp_get('oem'));
514+
515+
return $cp;
516+
}
517+
518+
return 0;
519+
}
520+
521+
/**
522+
* Sets console I/O to the specified code page and converts the user input.
523+
*
524+
* @param string|false $input
525+
*
526+
* @return string|false
527+
*/
528+
private function resetIOCodepage(int $cp, $input)
529+
{
530+
if (0 !== $cp) {
531+
sapi_windows_cp_set($cp);
532+
533+
if (false !== $input && '' !== $input) {
534+
$input = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), $cp, $input);
535+
}
536+
}
537+
538+
return $input;
539+
}
506540
}

0 commit comments

Comments
 (0)
0