Skip to content

Commit 2ae0580

Browse files
committed
bug symfony#31860 [HttpFoundation] work around PHP 7.3 bug related to json_encode() (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpFoundation] work around PHP 7.3 bug related to json_encode() | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#31447 | License | MIT | Doc PR | - I know, this doesn't make any sense. `json_encode` embeds global state behind the scene :( For reference, I asked on php-internals what they think about this: https://externals.io/message/105653#105838 Commits ------- e6e6301 [HttpFoundation] work around PHP 7.3 bug related to json_encode()
2 parents 5498cf5 + e6e6301 commit 2ae0580

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ protected function describeContainerParameter($parameter, array $options = [])
185185
private function writeData(array $data, array $options)
186186
{
187187
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
188+
189+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
190+
// Work around https://bugs.php.net/77997
191+
json_encode(null);
192+
}
193+
188194
$this->write(json_encode($data, $flags | JSON_PRETTY_PRINT)."\n");
189195
}
190196

src/Symfony/Component/Console/Descriptor/JsonDescriptor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ protected function describeApplication(Application $application, array $options
9797
*/
9898
private function writeData(array $data, array $options)
9999
{
100-
$this->write(json_encode($data, isset($options['json_encoding']) ? $options['json_encoding'] : 0));
100+
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
101+
102+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
103+
// Work around https://bugs.php.net/77997
104+
json_encode(null);
105+
}
106+
107+
$this->write(json_encode($data, $flags));
101108
}
102109

103110
/**

src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
8282
private function writeData(array $data, array $options)
8383
{
8484
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
85+
86+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
87+
// Work around https://bugs.php.net/77997
88+
json_encode(null);
89+
}
90+
8591
$this->output->write(json_encode($data, $flags | JSON_PRETTY_PRINT)."\n");
8692
}
8793

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ public function setData($data = [])
153153
restore_error_handler();
154154
}
155155
} else {
156+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $this->encodingOptions)) {
157+
// Work around https://bugs.php.net/77997
158+
json_encode(null);
159+
}
160+
156161
try {
157162
$data = json_encode($data, $this->encodingOptions);
158163
} catch (\Exception $e) {

src/Symfony/Component/Serializer/Encoder/JsonEncode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function encode($data, $format, array $context = [])
3636
{
3737
$context = $this->resolveContext($context);
3838

39+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $context['json_encode_options'])) {
40+
// Work around https://bugs.php.net/77997
41+
json_encode(null);
42+
}
43+
3944
$encodedJson = json_encode($data, $context['json_encode_options']);
4045

4146
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {

src/Symfony/Component/Translation/Dumper/JsonFileDumper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
3131
$flags = \defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
3232
}
3333

34+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
35+
// Work around https://bugs.php.net/77997
36+
json_encode(null);
37+
}
38+
3439
return json_encode($messages->all($domain), $flags);
3540
}
3641

0 commit comments

Comments
 (0)